Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web-worker): don't rely on browser API when it's not provided #4014

Merged
merged 2 commits into from Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/expect/src/jest-utils.ts
Expand Up @@ -485,8 +485,13 @@ export function arrayBufferEquality(a: unknown,
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer))
return undefined

dataViewA = new DataView(a)
dataViewB = new DataView(b)
try {
dataViewA = new DataView(a)
dataViewB = new DataView(b)
}
catch {
return undefined
}
}

// Buffers are not equal when they do not have the same byte length
Expand Down
8 changes: 7 additions & 1 deletion packages/web-worker/src/utils.ts
Expand Up @@ -81,10 +81,16 @@ export function getRunnerOptions(): any {
}
}

function stripProtocol(url: string | URL) {
return url.toString().replace(/^file:\/+/, '/')
}

export function getFileIdFromUrl(url: URL | string) {
if (typeof self === 'undefined')
return stripProtocol(url)
if (!(url instanceof URL))
url = new URL(url, self.location.origin)
if (url.protocol === 'http:' || url.protocol === 'https:')
return url.pathname
return url.toString().replace(/^file:\/+/, '/')
return stripProtocol(url)
}
15 changes: 0 additions & 15 deletions test/web-worker/test/init.test.ts
Expand Up @@ -56,21 +56,6 @@ it('worker with url', async () => {
await testWorker(new Worker(new URL('../src/worker.ts', url)))
})

it('worker with invalid url throws an error', async () => {
const url = import.meta.url
const worker = new Worker(new URL('../src/workerInvalid-path.ts', url))
const event = await new Promise<ErrorEvent>((resolve) => {
worker.onerror = (e) => {
resolve(e)
}
})
expect(event).toBeInstanceOf(ErrorEvent)
// Error is in different context when running in VM. This is consistent with jest.
if (!import.meta.env.VITEST_VM_POOL)
expect(event.error).toBeInstanceOf(Error)
expect(event.error.message).toContain('Failed to load')
})

it('self injected into worker and its deps should be equal', async () => {
expect.assertions(4)
expect(await testSelfWorker(new MySelfWorker())).toBeTruthy()
Expand Down
33 changes: 33 additions & 0 deletions test/web-worker/test/jsdom.test.ts
@@ -0,0 +1,33 @@
// @vitest-environment jsdom

import { expect, it } from 'vitest'

it('worker with invalid url throws an error', async () => {
const url = import.meta.url
const worker = new Worker(new URL('../src/workerInvalid-path.ts', url))
const event = await new Promise<ErrorEvent>((resolve) => {
worker.onerror = (e) => {
resolve(e)
}
})
expect(event).toBeInstanceOf(ErrorEvent)
// Error is in different context when running in VM. This is consistent with jest.
if (!import.meta.env.VITEST_VM_POOL)
expect(event.error).toBeInstanceOf(Error)
expect(event.error.message).toContain('Failed to load')
})

it('throws an error on invalid path', async () => {
expect(SharedWorker).toBeDefined()
const worker = new SharedWorker('./some-invalid-path')
const event = await new Promise<ErrorEvent>((resolve) => {
worker.onerror = (e) => {
resolve(e)
}
})
expect(event).toBeInstanceOf(ErrorEvent)
// Error is in different context when running in VM. This is consistent with jest.
if (!import.meta.env.VITEST_VM_POOL)
expect(event.error).toBeInstanceOf(Error)
expect(event.error.message).toContain('Failed to load')
})
15 changes: 0 additions & 15 deletions test/web-worker/test/sharedWorker.spec.ts
Expand Up @@ -40,21 +40,6 @@ it('shared worker with path works', async () => {
await expect(sendOnMessage(worker, 'event')).resolves.toBe('event')
})

it('throws an error on invalid path', async () => {
expect(SharedWorker).toBeDefined()
const worker = new SharedWorker('./some-invalid-path')
const event = await new Promise<ErrorEvent>((resolve) => {
worker.onerror = (e) => {
resolve(e)
}
})
expect(event).toBeInstanceOf(ErrorEvent)
// Error is in different context when running in VM. This is consistent with jest.
if (!import.meta.env.VITEST_VM_POOL)
expect(event.error).toBeInstanceOf(Error)
expect(event.error.message).toContain('Failed to load')
})

it('doesn\'t trigger events, if closed', async () => {
const worker = new MySharedWorker()
worker.port.close()
Expand Down
1 change: 0 additions & 1 deletion test/web-worker/vitest.config.ts
Expand Up @@ -2,7 +2,6 @@ import { defineConfig } from 'vite'

export default defineConfig({
test: {
environment: 'jsdom',
setupFiles: [
'./setup.ts',
],
Expand Down