11import fs from "fs" ;
22import path from "path" ;
33import os from "os" ;
4- import {
5- test as base ,
6- expect ,
7- chromium ,
8- type BrowserContext ,
9- } from "@playwright/test" ;
4+ import { test as base , expect , chromium , type BrowserContext } from "@playwright/test" ;
105import { installScriptByCode } from "./utils" ;
116
127const test = base . extend < {
@@ -17,15 +12,12 @@ const test = base.extend<{
1712 context : async ( { } , use ) => {
1813 const pathToExtension = path . resolve ( __dirname , "../dist/ext" ) ;
1914 const userDataDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "pw-ext-" ) ) ;
20- const chromeArgs = [
21- `--disable-extensions-except=${ pathToExtension } ` ,
22- `--load-extension=${ pathToExtension } ` ,
23- ] ;
15+ const chromeArgs = [ `--disable-extensions-except=${ pathToExtension } ` , `--load-extension=${ pathToExtension } ` ] ;
2416
2517 // Phase 1: Enable user scripts permission
2618 const ctx1 = await chromium . launchPersistentContext ( userDataDir , {
2719 headless : false ,
28- args : chromeArgs ,
20+ args : [ "--headless=new" , ... chromeArgs ] ,
2921 } ) ;
3022 let [ bg ] = ctx1 . serviceWorkers ( ) ;
3123 if ( ! bg ) bg = await ctx1 . waitForEvent ( "serviceworker" ) ;
@@ -48,6 +40,7 @@ const test = base.extend<{
4840 headless : false ,
4941 args : [ "--headless=new" , ...chromeArgs ] ,
5042 } ) ;
43+ // eslint-disable-next-line react-hooks/rules-of-hooks
5144 await use ( context ) ;
5245 await context . close ( ) ;
5346 fs . rmSync ( userDataDir , { recursive : true , force : true } ) ;
@@ -61,21 +54,16 @@ const test = base.extend<{
6154 await initPage . waitForLoadState ( "domcontentloaded" ) ;
6255 await initPage . evaluate ( ( ) => localStorage . setItem ( "firstUse" , "false" ) ) ;
6356 await initPage . close ( ) ;
57+ // eslint-disable-next-line react-hooks/rules-of-hooks
6458 await use ( extensionId ) ;
6559 } ,
6660} ) ;
6761
6862/** Strip SRI hashes and replace slow CDN with faster alternative */
6963function patchScriptCode ( code : string ) : string {
7064 return code
71- . replace (
72- / ^ ( \/ \/ \s * @ (?: r e q u i r e | r e s o u r c e ) \s + .* ?) # s h a (?: 2 5 6 | 3 8 4 | 5 1 2 ) [ = - ] [ ^ \s ] + / gm,
73- "$1"
74- )
75- . replace (
76- / h t t p s : \/ \/ c d n \. j s d e l i v r \. n e t \/ n p m \/ / g,
77- "https://unpkg.com/"
78- ) ;
65+ . replace ( / ^ ( \/ \/ \s * @ (?: r e q u i r e | r e s o u r c e ) \s + .* ?) # s h a (?: 2 5 6 | 3 8 4 | 5 1 2 ) [ = - ] [ ^ \s ] + / gm, "$1" )
66+ . replace ( / h t t p s : \/ \/ c d n \. j s d e l i v r \. n e t \/ n p m \/ / g, "https://unpkg.com/" ) ;
7967}
8068
8169/**
@@ -121,10 +109,7 @@ async function runTestScript(
121109 targetUrl : string ,
122110 timeoutMs : number
123111) : Promise < { passed : number ; failed : number ; logs : string [ ] } > {
124- let code = fs . readFileSync (
125- path . join ( __dirname , `../example/tests/${ scriptFile } ` ) ,
126- "utf-8"
127- ) ;
112+ let code = fs . readFileSync ( path . join ( __dirname , `../example/tests/${ scriptFile } ` ) , "utf-8" ) ;
128113 code = patchScriptCode ( code ) ;
129114
130115 await installScriptByCode ( context , extensionId , code ) ;
@@ -163,17 +148,8 @@ test.describe("GM API", () => {
163148 // Two-phase launch + script install + network fetches + permission dialogs
164149 test . setTimeout ( 300_000 ) ;
165150
166- test ( "GM_ sync API tests (gm_api_test.js)" , async ( {
167- context,
168- extensionId,
169- } ) => {
170- const { passed, failed, logs } = await runTestScript (
171- context ,
172- extensionId ,
173- "gm_api_test.js" ,
174- TARGET_URL ,
175- 90_000
176- ) ;
151+ test ( "GM_ sync API tests (gm_api_test.js)" , async ( { context, extensionId } ) => {
152+ const { passed, failed, logs } = await runTestScript ( context , extensionId , "gm_api_test.js" , TARGET_URL , 90_000 ) ;
177153
178154 console . log ( `[gm_api_test] passed=${ passed } , failed=${ failed } ` ) ;
179155 if ( failed !== 0 ) {
@@ -183,10 +159,7 @@ test.describe("GM API", () => {
183159 expect ( passed , "No test results found - script may not have run" ) . toBeGreaterThan ( 0 ) ;
184160 } ) ;
185161
186- test ( "GM.* async API tests (gm_api_async_test.js)" , async ( {
187- context,
188- extensionId,
189- } ) => {
162+ test ( "GM.* async API tests (gm_api_async_test.js)" , async ( { context, extensionId } ) => {
190163 const { passed, failed, logs } = await runTestScript (
191164 context ,
192165 extensionId ,
@@ -203,10 +176,7 @@ test.describe("GM API", () => {
203176 expect ( passed , "No test results found - script may not have run" ) . toBeGreaterThan ( 0 ) ;
204177 } ) ;
205178
206- test ( "Content inject tests (inject_content_test.js)" , async ( {
207- context,
208- extensionId,
209- } ) => {
179+ test ( "Content inject tests (inject_content_test.js)" , async ( { context, extensionId } ) => {
210180 const { passed, failed, logs } = await runTestScript (
211181 context ,
212182 extensionId ,
0 commit comments