/
fixture.test.ts
141 lines (127 loc) · 3.49 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
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
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'
import type { Logger } from '../../../logger'
import { createLogger } from '../../../logger'
const __dirname = resolve(fileURLToPath(import.meta.url), '..')
describe('fixture', async () => {
const resolveId = (id: string) => id
const root = resolve(__dirname, '..')
const logger = createLogger()
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, logger)
)?.s.toString()
).toMatchSnapshot()
})
it('preserve line count', async () => {
const getTransformedLineCount = async (code: string) =>
(
await transformGlobImport(
code,
'virtual:module',
root,
resolveId,
logger
)
)?.s
.toString()
.split('\n').length
expect(await getTransformedLineCount("import.meta.glob('./*.js')")).toBe(1)
expect(
await getTransformedLineCount(
`
import.meta.glob(
'./*.js'
)
`.trim()
)
).toBe(3)
})
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,
logger
)
)?.s.toString()
).toMatchSnapshot()
try {
await transformGlobImport(
"import.meta.glob('./modules/*.ts')",
'virtual:module',
root,
resolveId,
logger
)
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, logger, true)
)?.s.toString()
).toMatchSnapshot()
})
it('warn when glob css without ?inline', async () => {
const logs: string[] = []
const logger = {
warn(msg: string) {
logs.push(msg)
}
} as Logger
await transformGlobImport(
"import.meta.glob('./fixture-c/*.css', { query: '?inline' })",
fileURLToPath(import.meta.url),
root,
resolveId,
logger
)
expect(logs).toHaveLength(0)
await transformGlobImport(
"import.meta.glob('./fixture-c/*.module.css')",
fileURLToPath(import.meta.url),
root,
resolveId,
logger
)
expect(logs).toHaveLength(0)
await transformGlobImport(
"import.meta.glob('./fixture-c/*.css')",
fileURLToPath(import.meta.url),
root,
resolveId,
logger
)
expect(logs).toHaveLength(1)
expect(logs[0]).to.include(
'Globbing CSS files without the ?inline query is deprecated'
)
})
})