/
jsdom.test.ts
33 lines (30 loc) · 1.14 KB
/
jsdom.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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')
})