Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: do not interfere with shared workers (#1448)
* feat: add support for shared workers * test: touch-up shared worker test * test: rename the shared worker test Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
- Loading branch information
1 parent
41798eb
commit 670dda7
Showing
4 changed files
with
46 additions
and
1 deletion.
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
5 changes: 5 additions & 0 deletions
5
test/msw-api/setup-worker/scenarios/shared-worker/shared-worker.mocks.ts
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,5 @@ | ||
import { setupWorker } from 'msw' | ||
|
||
const worker = setupWorker() | ||
|
||
worker.start() |
33 changes: 33 additions & 0 deletions
33
test/msw-api/setup-worker/scenarios/shared-worker/shared-worker.test.ts
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,33 @@ | ||
import * as path from 'path' | ||
import { pageWith } from 'page-with' | ||
import { waitFor } from '../../../../support/waitFor' | ||
|
||
function createRuntime() { | ||
return pageWith({ | ||
example: path.resolve(__dirname, 'shared-worker.mocks.ts'), | ||
contentBase: path.resolve(__dirname), | ||
}) | ||
} | ||
|
||
test('does not interfere with a shared worker', async () => { | ||
const { page, consoleSpy } = await createRuntime() | ||
|
||
await page.evaluate(() => { | ||
const worker = new SharedWorker('/worker.js') | ||
|
||
worker.addEventListener('error', () => | ||
console.error('There is an error with worker'), | ||
) | ||
|
||
worker.port.onmessage = (event) => { | ||
console.log(event.data) | ||
} | ||
|
||
worker.port.postMessage('john') | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(consoleSpy.get('error')).toBeUndefined() | ||
expect(consoleSpy.get('log')).toContain('hello, john') | ||
}) | ||
}) |
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,7 @@ | ||
onconnect = (event) => { | ||
const port = event.ports[0] | ||
|
||
port.onmessage = (event) => { | ||
port.postMessage(`hello, ${event.data}`) | ||
} | ||
} |