-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.test.ts
106 lines (88 loc) · 3.41 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { describe, expect, it } from 'vitest'
import { formatWhitespace, transformDev } from '../../../test/utils'
describe('import globals', () => {
describe('buffer', () => {
it('injects Buffer', async () => {
const result = await transformDev(`Buffer.from('test')`)
expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
Buffer.from("test");
`))
})
it('injects Buffer only', async () => {
const result = await transformDev(`Buffer.from('test')`, {
globals: {
Buffer: true,
global: false,
process: false,
},
})
expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
Buffer.from("test");
`))
})
})
describe('global', () => {
it('injects global', async () => {
const result = await transformDev(`console.log(global)`)
expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
console.log(global);
`))
})
it('injects global only', async () => {
const result = await transformDev(`console.log(global)`, {
globals: {
Buffer: false,
global: true,
process: false,
},
})
expect(result?.code).toEqual(formatWhitespace(`
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
console.log(global);
`))
})
})
describe('process', () => {
it('injects process', async () => {
const result = await transformDev(`console.log(process)`)
expect(result?.code).toEqual(formatWhitespace(`
import __buffer_polyfill from "/shims/buffer/dist/index.js"
globalThis.Buffer = globalThis.Buffer || __buffer_polyfill
import __global_polyfill from "/shims/global/dist/index.js"
globalThis.global = globalThis.global || __global_polyfill
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
console.log(process);
`))
})
it('injects process only', async () => {
const result = await transformDev(`console.log(process)`, {
globals: {
Buffer: false,
global: false,
process: true,
},
})
expect(result?.code).toEqual(formatWhitespace(`
import __process_polyfill from "/shims/process/dist/index.js"
globalThis.process = globalThis.process || __process_polyfill
console.log(process);
`))
})
})
})