/
fixture.test.ts
62 lines (54 loc) · 1.8 KB
/
fixture.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
import { resolve } from 'node:path'
import { promises as fs } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { transformGlobImport } from '../../../plugins/importMetaGlob'
import { transformWithEsbuild } from '../../../plugins/esbuild'
const __dirname = resolve(fileURLToPath(import.meta.url), '..')
describe('fixture', async () => {
const resolveId = (id: string) => id
const root = resolve(__dirname, '..')
it('transform', async () => {
const id = resolve(__dirname, './fixture-a/index.ts')
const code = (
await transformWithEsbuild(await fs.readFile(id, 'utf-8'), id)
).code
expect(
(await transformGlobImport(code, id, root, resolveId))?.s.toString()
).toMatchSnapshot()
})
it('virtual modules', async () => {
const root = resolve(__dirname, './fixture-a')
const code = [
"import.meta.glob('/modules/*.ts')",
"import.meta.glob(['/../fixture-b/*.ts'])"
].join('\n')
expect(
(
await transformGlobImport(code, 'virtual:module', root, resolveId)
)?.s.toString()
).toMatchSnapshot()
try {
await transformGlobImport(
"import.meta.glob('./modules/*.ts')",
'virtual:module',
root,
resolveId
)
expect('no error').toBe('should throw an error')
} catch (err) {
expect(err).toMatchInlineSnapshot(
"[Error: In virtual modules, all globs must start with '/']"
)
}
})
it('transform with restoreQueryExtension', async () => {
const id = resolve(__dirname, './fixture-a/index.ts')
const code = (
await transformWithEsbuild(await fs.readFile(id, 'utf-8'), id)
).code
expect(
(await transformGlobImport(code, id, root, resolveId, true))?.s.toString()
).toMatchSnapshot()
})
})