Repository navigation
Expand file tree
/
Copy pathresolve-id.test.ts
More file actions
167 lines (140 loc) · 5.06 KB
/
Copy pathresolve-id.test.ts
File metadata and controls
167 lines (140 loc) · 5.06 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import type { UnpluginBuildContext, UnpluginContext, UnpluginOptions, VitePlugin } from 'unplugin'
import type { Mock } from 'vitest'
import * as path from 'node:path'
import { createUnplugin } from 'unplugin'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { onlyBun } from '../../utils'
import { build, toArray } from '../utils'
function createUnpluginWithCallback(resolveIdCallback: UnpluginOptions['resolveId']) {
return createUnplugin(() => ({
name: 'test-plugin',
resolveId: resolveIdCallback,
}))
}
// We extract this check because all bundlers should behave the same
const propsToTest: (keyof (UnpluginContext & UnpluginBuildContext))[] = ['addWatchFile', 'emitFile', 'getWatchFiles', 'parse', 'error', 'warn', 'fs']
function createResolveIdHook(): Mock {
const mockResolveIdHook = vi.fn(function (this: UnpluginContext & UnpluginBuildContext) {
for (const prop of propsToTest) {
expect(this).toHaveProperty(prop)
if (prop === 'fs') {
expect(this.fs).toBeTruthy()
expect(typeof this.fs).toBe('object')
expect(this.fs.readFile).toBeInstanceOf(Function)
expect(this.fs.stat).toBeInstanceOf(Function)
expect(this.fs.lstat).toBeInstanceOf(Function)
}
else {
expect(this[prop]).toBeInstanceOf(Function)
}
}
})
return mockResolveIdHook
}
function checkResolveIdHook(resolveIdCallback: Mock): void {
const fsAssertionsPerHookCall = 6 // `toHaveProperty('fs')` + 5 assertions (`toBeTruthy`, `typeof`, `readFile`, `stat`, `lstat`)
const nonFsAssertionsPerHookCall = (propsToTest.length - 1) * 2
const calledWithAssertionPerHookCall = 1
expect.assertions(4 * (calledWithAssertionPerHookCall + nonFsAssertionsPerHookCall + fsAssertionsPerHookCall))
expect(resolveIdCallback).toHaveBeenCalledWith(
expect.stringMatching(/(?:\/|\\)entry\.js$/),
undefined,
expect.objectContaining({ isEntry: true }),
)
expect(resolveIdCallback).toHaveBeenCalledWith(
'./proxy-export',
expect.stringMatching(/(?:\/|\\)entry\.js$/),
expect.objectContaining({ isEntry: false }),
)
expect(resolveIdCallback).toHaveBeenCalledWith(
'./default-export',
expect.stringMatching(/(?:\/|\\)proxy-export\.js$/),
expect.objectContaining({ isEntry: false }),
)
expect(resolveIdCallback).toHaveBeenCalledWith(
'./named-export',
expect.stringMatching(/(?:\/|\\)proxy-export\.js$/),
expect.objectContaining({ isEntry: false }),
)
}
describe('resolveId hook', () => {
afterEach(() => {
vi.restoreAllMocks()
})
it('vite', async () => {
const mockResolveIdHook = createResolveIdHook()
const plugin = createUnpluginWithCallback(mockResolveIdHook).vite
// we need to define `enforce` here for the plugin to be run
const plugins = toArray(plugin()).map((plugin): VitePlugin => ({ ...plugin, enforce: 'pre' }))
await build.vite({
clearScreen: false,
plugins: [plugins],
build: {
lib: {
entry: path.resolve(__dirname, 'test-src/entry.js'),
name: 'TestLib',
},
write: false, // don't output anything
},
})
checkResolveIdHook(mockResolveIdHook)
})
it('rollup', async () => {
const mockResolveIdHook = createResolveIdHook()
const plugin = createUnpluginWithCallback(mockResolveIdHook).rollup
await build.rollup({
input: path.resolve(__dirname, 'test-src/entry.js'),
plugins: [plugin()],
})
checkResolveIdHook(mockResolveIdHook)
})
it('webpack', async () => {
const mockResolveIdHook = createResolveIdHook()
const plugin = createUnpluginWithCallback(mockResolveIdHook).webpack
await new Promise((resolve) => {
build.webpack(
{
entry: path.resolve(__dirname, 'test-src/entry.js'),
plugins: [plugin()],
},
resolve,
)
})
checkResolveIdHook(mockResolveIdHook)
})
it('rspack', async () => {
const mockResolveIdHook = createResolveIdHook()
const plugin = createUnpluginWithCallback(mockResolveIdHook).rspack
await new Promise((resolve) => {
build.rspack(
{
entry: path.resolve(__dirname, 'test-src/entry.js'),
plugins: [plugin()],
},
resolve,
)
})
checkResolveIdHook(mockResolveIdHook)
})
it('esbuild', async () => {
const mockResolveIdHook = createResolveIdHook()
const plugin = createUnpluginWithCallback(mockResolveIdHook).esbuild
await build.esbuild({
entryPoints: [path.resolve(__dirname, 'test-src/entry.js')],
plugins: [plugin()],
bundle: true, // actually traverse imports
write: false, // don't pollute console
})
checkResolveIdHook(mockResolveIdHook)
})
onlyBun('bun', async () => {
const mockResolveIdHook = createResolveIdHook()
const plugin = createUnpluginWithCallback(mockResolveIdHook).bun
await build.bun({
entrypoints: [path.resolve(__dirname, 'test-src/entry.js')],
plugins: [plugin()],
outdir: path.resolve(__dirname, 'test-out/bun'), // Bun requires outdir
})
checkResolveIdHook(mockResolveIdHook)
})
})