Skip to content

Commit

Permalink
fix(web-worker): don't rely on browser API when it's not provided (#4014
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sheremet-va committed Aug 25, 2023
1 parent bf83936 commit e78a449
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
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

0 comments on commit e78a449

Please sign in to comment.