Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions __tests__/Audit.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
jest.mock('lighthouse')

const mockKill = jest.fn()
jest.mock('chrome-launcher', () => {
return {
launch() {
return {
port: '1234',
kill() {
return null
}
kill: mockKill
}
}
}
Expand All @@ -16,6 +16,16 @@ const lighthouse = require('lighthouse')
const { Audit } = require('../index')

describe('Audit', () => {
beforeEach(() => {
jest.clearAllMocks()
mockKill.mockResolvedValue(null)
lighthouse.mockResolvedValue({
lhr: {
audits: {}
}
})
})

test('Instantiation of audit with no settings should default to reasonable scan', async () => {
const audit = new Audit()
expect(audit.settings).toEqual({
Expand Down Expand Up @@ -50,6 +60,22 @@ describe('Audit', () => {
await audit.scanUrl(url, opts)

expect(lighthouse.mock.calls[0][0]).toBe(url)
expect(lighthouse.mock.calls[1][1]).toHaveProperty('emulatedFormFactor', 'mobile')
// The device option is passed as the second parameter to lighthouse
expect(lighthouse.mock.calls[0][1]).toHaveProperty('emulatedFormFactor', 'mobile')
})

test('should handle Chrome kill errors gracefully', async () => {
const url = 'https://abc.com'

// Mock chrome kill to throw an error (like on Windows 11)
mockKill.mockRejectedValueOnce(new Error('Chrome could not be killed Command failed: taskkill /pid 15132 /T /F'))

const audit = new Audit()

// This should not throw an error even when chrome kill fails
await expect(audit.scanUrl(url)).resolves.toBeDefined()

// Verify that kill was attempted
expect(mockKill).toHaveBeenCalled()
})
})
6 changes: 6 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ module.exports = class Audit {
)}`
)

await chromeInstance.kill()
try {
await chromeInstance.kill()
} catch (error) {
// Chrome process may have already exited, which is the desired outcome
debug(`Chrome kill warning: ${error.message}`)
}
return scanResult
}
}
Loading