Skip to content

Commit

Permalink
test(detect-acorn): add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Sun79 committed Jan 14, 2025
1 parent 8e84120 commit 4a8b49a
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion test/detect-acorn.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse } from 'acorn'
import { describe, expect, it } from 'vitest'
import { createUnimport } from '../src'
import { createUnimport, type Import } from '../src'
import { traveseScopes } from '../src/detect-acorn'

describe('detect-acorn', () => {
Expand Down Expand Up @@ -129,6 +129,52 @@ describe('detect-acorn', () => {
]
`)
})

it('matchedImports', async () => {
const ctx = createUnimport({
parser: 'acorn',
presets: ['vue'],
addons: [
{
name: 'resolver',
async matchImports(names, matched) {
const dynamic: Import[] = []
for (const name of names) {
if (name === 'foo') {
dynamic.push({
from: 'bar',
name: 'foo',
as: 'foo',
})
}
}
return [...matched, ...dynamic]
},
},
],
})

const code = `
import otherModule from 'otherModule'
const count = ref(0)
const bar = foo
const test = notDefined
console.log(otherModule)
`.trim()

expect((await ctx.injectImports(code)).code)
.toMatchInlineSnapshot(`
"import { ref } from 'vue';
import { foo } from 'bar';
import otherModule from 'otherModule'
const count = ref(0)
const bar = foo
const test = notDefined
console.log(otherModule)"
`)
})
})

describe('virtual imports', () => {
Expand Down

0 comments on commit 4a8b49a

Please sign in to comment.