Skip to content

Commit e78a449

Browse files
authored
fix(web-worker): don't rely on browser API when it's not provided (#4014)
1 parent bf83936 commit e78a449

File tree

6 files changed

+47
-34
lines changed

6 files changed

+47
-34
lines changed

packages/expect/src/jest-utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,13 @@ export function arrayBufferEquality(a: unknown,
485485
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer))
486486
return undefined
487487

488-
dataViewA = new DataView(a)
489-
dataViewB = new DataView(b)
488+
try {
489+
dataViewA = new DataView(a)
490+
dataViewB = new DataView(b)
491+
}
492+
catch {
493+
return undefined
494+
}
490495
}
491496

492497
// Buffers are not equal when they do not have the same byte length

packages/web-worker/src/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,16 @@ export function getRunnerOptions(): any {
8181
}
8282
}
8383

84+
function stripProtocol(url: string | URL) {
85+
return url.toString().replace(/^file:\/+/, '/')
86+
}
87+
8488
export function getFileIdFromUrl(url: URL | string) {
89+
if (typeof self === 'undefined')
90+
return stripProtocol(url)
8591
if (!(url instanceof URL))
8692
url = new URL(url, self.location.origin)
8793
if (url.protocol === 'http:' || url.protocol === 'https:')
8894
return url.pathname
89-
return url.toString().replace(/^file:\/+/, '/')
95+
return stripProtocol(url)
9096
}

test/web-worker/test/init.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,6 @@ it('worker with url', async () => {
5656
await testWorker(new Worker(new URL('../src/worker.ts', url)))
5757
})
5858

59-
it('worker with invalid url throws an error', async () => {
60-
const url = import.meta.url
61-
const worker = new Worker(new URL('../src/workerInvalid-path.ts', url))
62-
const event = await new Promise<ErrorEvent>((resolve) => {
63-
worker.onerror = (e) => {
64-
resolve(e)
65-
}
66-
})
67-
expect(event).toBeInstanceOf(ErrorEvent)
68-
// Error is in different context when running in VM. This is consistent with jest.
69-
if (!import.meta.env.VITEST_VM_POOL)
70-
expect(event.error).toBeInstanceOf(Error)
71-
expect(event.error.message).toContain('Failed to load')
72-
})
73-
7459
it('self injected into worker and its deps should be equal', async () => {
7560
expect.assertions(4)
7661
expect(await testSelfWorker(new MySelfWorker())).toBeTruthy()

test/web-worker/test/jsdom.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @vitest-environment jsdom
2+
3+
import { expect, it } from 'vitest'
4+
5+
it('worker with invalid url throws an error', async () => {
6+
const url = import.meta.url
7+
const worker = new Worker(new URL('../src/workerInvalid-path.ts', url))
8+
const event = await new Promise<ErrorEvent>((resolve) => {
9+
worker.onerror = (e) => {
10+
resolve(e)
11+
}
12+
})
13+
expect(event).toBeInstanceOf(ErrorEvent)
14+
// Error is in different context when running in VM. This is consistent with jest.
15+
if (!import.meta.env.VITEST_VM_POOL)
16+
expect(event.error).toBeInstanceOf(Error)
17+
expect(event.error.message).toContain('Failed to load')
18+
})
19+
20+
it('throws an error on invalid path', async () => {
21+
expect(SharedWorker).toBeDefined()
22+
const worker = new SharedWorker('./some-invalid-path')
23+
const event = await new Promise<ErrorEvent>((resolve) => {
24+
worker.onerror = (e) => {
25+
resolve(e)
26+
}
27+
})
28+
expect(event).toBeInstanceOf(ErrorEvent)
29+
// Error is in different context when running in VM. This is consistent with jest.
30+
if (!import.meta.env.VITEST_VM_POOL)
31+
expect(event.error).toBeInstanceOf(Error)
32+
expect(event.error.message).toContain('Failed to load')
33+
})

test/web-worker/test/sharedWorker.spec.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ it('shared worker with path works', async () => {
4040
await expect(sendOnMessage(worker, 'event')).resolves.toBe('event')
4141
})
4242

43-
it('throws an error on invalid path', async () => {
44-
expect(SharedWorker).toBeDefined()
45-
const worker = new SharedWorker('./some-invalid-path')
46-
const event = await new Promise<ErrorEvent>((resolve) => {
47-
worker.onerror = (e) => {
48-
resolve(e)
49-
}
50-
})
51-
expect(event).toBeInstanceOf(ErrorEvent)
52-
// Error is in different context when running in VM. This is consistent with jest.
53-
if (!import.meta.env.VITEST_VM_POOL)
54-
expect(event.error).toBeInstanceOf(Error)
55-
expect(event.error.message).toContain('Failed to load')
56-
})
57-
5843
it('doesn\'t trigger events, if closed', async () => {
5944
const worker = new MySharedWorker()
6045
worker.port.close()

test/web-worker/vitest.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { defineConfig } from 'vite'
22

33
export default defineConfig({
44
test: {
5-
environment: 'jsdom',
65
setupFiles: [
76
'./setup.ts',
87
],

0 commit comments

Comments
 (0)