@@ -13,18 +13,13 @@ import { IntakeRegistry } from './intakeRegistry'
1313import { flushEvents } from './flushEvents'
1414import type { Servers } from './httpServers'
1515import { getTestServers , waitForServersIdle } from './httpServers'
16- import type { SetupFactory , SetupOptions } from './pageSetups'
17- import { html , DEFAULT_SETUPS , npmSetup , reactSetup } from './pageSetups'
16+ import type { SetupFactory , SetupOptions , WorkerImplementationFactory } from './pageSetups'
17+ import { workerSetup , html , DEFAULT_SETUPS , npmSetup , reactSetup } from './pageSetups'
1818import { createIntakeServerApp } from './serverApps/intake'
1919import { createMockServerApp } from './serverApps/mock'
2020import type { Extension } from './createExtension'
2121import { isBrowserStack } from './environment'
2222
23- interface LogsWorkerOptions {
24- importScript ?: boolean
25- nativeLog ?: boolean
26- }
27-
2823export function createTest ( title : string ) {
2924 return new TestBuilder ( title )
3025}
@@ -62,8 +57,8 @@ class TestBuilder {
6257 rumConfiguration ?: RumInitConfiguration
6358 logsConfiguration ?: LogsInitConfiguration
6459 } = { }
65- private useServiceWorker : boolean = false
6660 private hostName ?: string
61+ private workerImplementationFactory ?: ( setup : string ) => string
6762
6863 constructor ( private title : string ) { }
6964
@@ -135,37 +130,23 @@ class TestBuilder {
135130 return this
136131 }
137132
138- withWorker ( { importScript = false , nativeLog = false } : LogsWorkerOptions = { } ) {
139- if ( ! this . useServiceWorker ) {
140- this . useServiceWorker = true
133+ withWorker ( implementation : WorkerImplementationFactory , isModule = false ) {
134+ implementation . isModule = isModule
135+ this . workerImplementationFactory = implementation
141136
142- const isModule = ! importScript
143-
144- const params = [ ]
145- if ( importScript ) {
146- params . push ( 'importScripts=true' )
147- }
148- if ( nativeLog ) {
149- params . push ( 'nativeLog=true' )
150- }
137+ const options = isModule ? '{ type: "module" }' : '{}'
151138
152- const query = params . length > 0 ? `?${ params . join ( '&' ) } ` : ''
153- const url = `/sw.js${ query } `
154-
155- const options = isModule ? '{ type: "module" }' : '{}'
156-
157- // Service workers require HTTPS or localhost due to browser security restrictions
158- this . hostName = 'localhost'
159- this . withBody ( html `
160- < script >
161- if ( ! window . myServiceWorker && 'serviceWorker' in navigator ) {
162- navigator . serviceWorker . register ( '${ url } ' , ${ options } ) . then ( ( registration ) => {
163- window . myServiceWorker = registration
164- } )
165- }
166- </ script >
167- ` )
168- }
139+ // Service workers require HTTPS or localhost due to browser security restrictions
140+ this . withHostName ( 'localhost' )
141+ this . withBody ( html `
142+ < script >
143+ if ( ! window . myServiceWorker && 'serviceWorker' in navigator ) {
144+ navigator . serviceWorker . register ( '/sw.js' , ${ options } ) . then ( ( registration ) => {
145+ window . myServiceWorker = registration
146+ } )
147+ }
148+ </ script >
149+ ` )
169150
170151 return this
171152 }
@@ -194,6 +175,7 @@ class TestBuilder {
194175 testFixture : this . testFixture ,
195176 extension : this . extension ,
196177 hostName : this . hostName ,
178+ workerImplementation : this . workerImplementationFactory ,
197179 }
198180
199181 if ( this . alsoRunWithRumSlim ) {
@@ -270,7 +252,12 @@ function declareTest(title: string, setupOptions: SetupOptions, factory: SetupFa
270252 servers . intake . bindServerApp ( createIntakeServerApp ( testContext . intakeRegistry ) )
271253
272254 const setup = factory ( setupOptions , servers )
273- servers . base . bindServerApp ( createMockServerApp ( servers , setup , setupOptions . remoteConfiguration ) )
255+ servers . base . bindServerApp (
256+ createMockServerApp ( servers , setup , {
257+ remoteConfiguration : setupOptions . remoteConfiguration ,
258+ workerImplementation : setupOptions . workerImplementation && workerSetup ( setupOptions , servers ) ,
259+ } )
260+ )
274261 servers . crossOrigin . bindServerApp ( createMockServerApp ( servers , setup ) )
275262
276263 await setUpTest ( browserLogs , testContext )
0 commit comments