From e78a449d3845e0e88fea896bdd05174fd1ed303e Mon Sep 17 00:00:00 2001 From: Vladimir Date: Fri, 25 Aug 2023 12:51:18 +0200 Subject: [PATCH] fix(web-worker): don't rely on browser API when it's not provided (#4014) --- packages/expect/src/jest-utils.ts | 9 +++++-- packages/web-worker/src/utils.ts | 8 +++++- test/web-worker/test/init.test.ts | 15 ----------- test/web-worker/test/jsdom.test.ts | 33 +++++++++++++++++++++++ test/web-worker/test/sharedWorker.spec.ts | 15 ----------- test/web-worker/vitest.config.ts | 1 - 6 files changed, 47 insertions(+), 34 deletions(-) create mode 100644 test/web-worker/test/jsdom.test.ts diff --git a/packages/expect/src/jest-utils.ts b/packages/expect/src/jest-utils.ts index ce73d598aafd..f49e37bf0dc8 100644 --- a/packages/expect/src/jest-utils.ts +++ b/packages/expect/src/jest-utils.ts @@ -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 diff --git a/packages/web-worker/src/utils.ts b/packages/web-worker/src/utils.ts index 75cc7ab48b16..937cf14276fa 100644 --- a/packages/web-worker/src/utils.ts +++ b/packages/web-worker/src/utils.ts @@ -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) } diff --git a/test/web-worker/test/init.test.ts b/test/web-worker/test/init.test.ts index d2a02bb68aa8..166cb2353ff2 100644 --- a/test/web-worker/test/init.test.ts +++ b/test/web-worker/test/init.test.ts @@ -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((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() diff --git a/test/web-worker/test/jsdom.test.ts b/test/web-worker/test/jsdom.test.ts new file mode 100644 index 000000000000..cd2427691fa7 --- /dev/null +++ b/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((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((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') +}) diff --git a/test/web-worker/test/sharedWorker.spec.ts b/test/web-worker/test/sharedWorker.spec.ts index c245d4b5976c..8c82a1b06b2b 100644 --- a/test/web-worker/test/sharedWorker.spec.ts +++ b/test/web-worker/test/sharedWorker.spec.ts @@ -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((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() diff --git a/test/web-worker/vitest.config.ts b/test/web-worker/vitest.config.ts index dbb48563fd62..c6de0b5c72d0 100644 --- a/test/web-worker/vitest.config.ts +++ b/test/web-worker/vitest.config.ts @@ -2,7 +2,6 @@ import { defineConfig } from 'vite' export default defineConfig({ test: { - environment: 'jsdom', setupFiles: [ './setup.ts', ],