Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(setupWorker): resolve the "start" promise after the worker has ac…
…tivated (#1146)
- Loading branch information
1 parent
d7bdeec
commit f6e709c
Showing
4 changed files
with
87 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,59 @@ | ||
import * as path from 'path' | ||
import { pageWith } from 'page-with' | ||
import { SetupWorkerApi } from 'msw' | ||
import { waitFor } from '../../../support/waitFor' | ||
|
||
declare namespace window { | ||
export const msw: { | ||
registration: ReturnType<SetupWorkerApi['start']> | ||
startWorker(): ReturnType<SetupWorkerApi['start']> | ||
} | ||
} | ||
|
||
test('resolves the "start" Promise after the worker has been activated', async () => { | ||
const { page, consoleSpy } = await pageWith({ | ||
test('resolves the "start" Promise when the worker has been activated', async () => { | ||
const runtime = await pageWith({ | ||
example: path.resolve(__dirname, 'start.mocks.ts'), | ||
routes(app) { | ||
app.get('/worker.js', (req, res) => { | ||
res.sendFile(path.resolve(__dirname, 'start.worker.js')) | ||
}) | ||
}, | ||
}) | ||
const resolvedPayload = await page.evaluate(() => { | ||
return window.msw.registration | ||
}) | ||
|
||
expect(resolvedPayload).toBe('ServiceWorkerRegistration') | ||
const events: string[] = [] | ||
|
||
const untilWorkerActivated = runtime.page | ||
.evaluate(() => { | ||
return new Promise((resolve) => | ||
navigator.serviceWorker.addEventListener('controllerchange', resolve), | ||
) | ||
}) | ||
.then(() => { | ||
events.push('worker activated') | ||
}) | ||
|
||
const activationMessageIndex = consoleSpy | ||
.get('startGroupCollapsed') | ||
.findIndex((text) => { | ||
return text.includes('[MSW] Mocking enabled') | ||
const untilStartResolved = runtime.page | ||
.evaluate(() => { | ||
return window.msw.startWorker() | ||
}) | ||
.then(() => { | ||
events.push('start resolved') | ||
}) | ||
|
||
const customMessageIndex = consoleSpy.get('log').findIndex((text) => { | ||
return text.includes('Registration Promise resolved') | ||
const untilActivationMessage = waitFor(() => { | ||
expect(runtime.consoleSpy.get('startGroupCollapsed')).toContain( | ||
'[MSW] Mocking enabled.', | ||
) | ||
events.push('enabled message') | ||
}) | ||
|
||
expect(activationMessageIndex).toBeGreaterThan(-1) | ||
expect(customMessageIndex).toBeGreaterThan(-1) | ||
expect(customMessageIndex).toBeGreaterThan(activationMessageIndex) | ||
await Promise.all([ | ||
untilActivationMessage, | ||
untilWorkerActivated, | ||
untilStartResolved, | ||
]) | ||
|
||
expect(events[0]).toEqual('worker activated') | ||
expect(events[1]).toEqual('start resolved') | ||
expect(events[2]).toEqual('enabled message') | ||
expect(events).toHaveLength(3) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
importScripts('/mockServiceWorker.js') | ||
|
||
self.addEventListener('install', (event) => { | ||
event.waitUntil( | ||
new Promise((resolve) => { | ||
// Emulate long worker installation. | ||
setTimeout(resolve, 500) | ||
}), | ||
) | ||
}) |