11jest . mock ( 'lighthouse' )
2+
3+ const mockKill = jest . fn ( )
24jest . 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')
1616const { Audit } = require ( '../index' )
1717
1818describe ( '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} )
0 commit comments