Skip to content

Commit 9c2cf88

Browse files
Copilotlirantal
andauthored
fix: Chrome kill failure on Windows 11 by adding graceful error handling (#111)
* Initial plan * Initial plan for fixing Chrome kill issue on Windows 11 Co-authored-by: lirantal <316371+lirantal@users.noreply.github.com> * Add graceful error handling for Chrome kill failures on Windows 11 Co-authored-by: lirantal <316371+lirantal@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lirantal <316371+lirantal@users.noreply.github.com>
1 parent 6542b08 commit 9c2cf88

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

__tests__/Audit.test.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
jest.mock('lighthouse')
2+
3+
const mockKill = jest.fn()
24
jest.mock('chrome-launcher', () => {
35
return {
46
launch() {
57
return {
68
port: '1234',
7-
kill() {
8-
return null
9-
}
9+
kill: mockKill
1010
}
1111
}
1212
}
@@ -16,6 +16,16 @@ const lighthouse = require('lighthouse')
1616
const { Audit } = require('../index')
1717

1818
describe('Audit', () => {
19+
beforeEach(() => {
20+
jest.clearAllMocks()
21+
mockKill.mockResolvedValue(null)
22+
lighthouse.mockResolvedValue({
23+
lhr: {
24+
audits: {}
25+
}
26+
})
27+
})
28+
1929
test('Instantiation of audit with no settings should default to reasonable scan', async () => {
2030
const audit = new Audit()
2131
expect(audit.settings).toEqual({
@@ -50,6 +60,22 @@ describe('Audit', () => {
5060
await audit.scanUrl(url, opts)
5161

5262
expect(lighthouse.mock.calls[0][0]).toBe(url)
53-
expect(lighthouse.mock.calls[1][1]).toHaveProperty('emulatedFormFactor', 'mobile')
63+
// The device option is passed as the second parameter to lighthouse
64+
expect(lighthouse.mock.calls[0][1]).toHaveProperty('emulatedFormFactor', 'mobile')
65+
})
66+
67+
test('should handle Chrome kill errors gracefully', async () => {
68+
const url = 'https://abc.com'
69+
70+
// Mock chrome kill to throw an error (like on Windows 11)
71+
mockKill.mockRejectedValueOnce(new Error('Chrome could not be killed Command failed: taskkill /pid 15132 /T /F'))
72+
73+
const audit = new Audit()
74+
75+
// This should not throw an error even when chrome kill fails
76+
await expect(audit.scanUrl(url)).resolves.toBeDefined()
77+
78+
// Verify that kill was attempted
79+
expect(mockKill).toHaveBeenCalled()
5480
})
5581
})

npm-shrinkwrap.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Audit.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ module.exports = class Audit {
9898
)}`
9999
)
100100

101-
await chromeInstance.kill()
101+
try {
102+
await chromeInstance.kill()
103+
} catch (error) {
104+
// Chrome process may have already exited, which is the desired outcome
105+
debug(`Chrome kill warning: ${error.message}`)
106+
}
102107
return scanResult
103108
}
104109
}

0 commit comments

Comments
 (0)