/
parse.test.ts
40 lines (32 loc) · 1.21 KB
/
parse.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
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { transformDynamicImport } from '../../../plugins/dynamicImportVars'
const __dirname = resolve(fileURLToPath(import.meta.url), '..')
async function run(input: string) {
const { glob, rawPattern } =
(await transformDynamicImport(input, resolve(__dirname, 'index.js'), (id) =>
id.replace('@', resolve(__dirname, './mods/'))
)) || {}
return `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`
}
describe('parse positives', () => {
it('basic', async () => {
expect(await run('`./mods/${base}.js`')).toMatchSnapshot()
})
it('alias path', async () => {
expect(await run('`@/${base}.js`')).toMatchSnapshot()
})
it('with query raw', async () => {
expect(await run('`./mods/${base}.js?raw`')).toMatchSnapshot()
})
it('with query url', async () => {
expect(await run('`./mods/${base}.js?url`')).toMatchSnapshot()
})
it('? in variables', async () => {
expect(await run('`./mods/${base ?? foo}.js?raw`')).toMatchSnapshot()
})
it('? in url', async () => {
expect(await run('`./mo?ds/${base ?? foo}.js?raw`')).toMatchSnapshot()
})
})