/
injector-mock.test.ts
73 lines (67 loc) · 2.34 KB
/
injector-mock.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Parser } from 'acorn'
import { hoistMocks } from 'vitest/src/node/hoistMocks'
import { expect, test } from 'vitest'
function parse(code: string, options: any) {
return Parser.parse(code, options)
}
function hoistSimpleCode(code: string) {
return hoistMocks(code, '/test.js', parse)?.code.trim()
}
test('hoists mock, unmock, hoisted', () => {
expect(hoistSimpleCode(`
vi.mock('path', () => {})
vi.unmock('path')
vi.hoisted(() => {})
`)).toMatchInlineSnapshot(`
"if (typeof globalThis.vi === \\"undefined\\" && typeof globalThis.vitest === \\"undefined\\") { throw new Error(\\"There are some problems in resolving the mocks API.\\\\nYou may encounter this issue when importing the mocks API from another module other than 'vitest'.\\\\nTo fix this issue you can either:\\\\n- import the mocks API directly from 'vitest'\\\\n- enable the 'globals' options\\") }
vi.mock('path', () => {})
vi.unmock('path')
vi.hoisted(() => {})"
`)
})
test('always hoists import from vitest', () => {
expect(hoistSimpleCode(`
import { vi } from 'vitest'
vi.mock('path', () => {})
vi.unmock('path')
vi.hoisted(() => {})
import { test } from 'vitest'
`)).toMatchInlineSnapshot(`
"const { vi } = await import('vitest')
const { test } = await import('vitest')
vi.mock('path', () => {})
vi.unmock('path')
vi.hoisted(() => {})"
`)
})
test('always hoists all imports but they are under mocks', () => {
expect(hoistSimpleCode(`
import { vi } from 'vitest'
import { someValue } from './path.js'
import { someValue2 } from './path2.js'
vi.mock('path', () => {})
vi.unmock('path')
vi.hoisted(() => {})
import { test } from 'vitest'
`)).toMatchInlineSnapshot(`
"const { vi } = await import('vitest')
const { test } = await import('vitest')
vi.mock('path', () => {})
vi.unmock('path')
vi.hoisted(() => {})
const { someValue } = await import('./path.js')
const { someValue2 } = await import('./path2.js')"
`)
})
test('correctly mocks namespaced', () => {
expect(hoistSimpleCode(`
import { vi } from 'vitest'
import add, * as AddModule from '../src/add'
vi.mock('../src/add', () => {})
`)).toMatchInlineSnapshot(`
"const { vi } = await import('vitest')
vi.mock('../src/add', () => {})
const AddModule = await import('../src/add')
const { default: add } = AddModule"
`)
})