Skip to content

Commit

Permalink
test(detect-regex): add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Sun79 committed Jan 14, 2025
1 parent 4a8b49a commit 463a671
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
35 changes: 35 additions & 0 deletions test/addon-match-imports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it } from 'vitest'
import { createUnimport } from '../src'
import { resolverAddon } from './share'

describe('addon match imports', () => {
const ctx = createUnimport({
presets: ['vue'],
addons: [
resolverAddon(),
],
})

it('inject', async () => {
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)"
`)
})
})
20 changes: 3 additions & 17 deletions test/detect-acorn.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { parse } from 'acorn'
import { describe, expect, it } from 'vitest'
import { createUnimport, type Import } from '../src'
import { createUnimport } from '../src'
import { traveseScopes } from '../src/detect-acorn'
import { resolverAddon } from './share'

describe('detect-acorn', () => {
it('scopes', async () => {
Expand Down Expand Up @@ -135,22 +136,7 @@ describe('detect-acorn', () => {
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]
},
},
resolverAddon(),
],
})

Expand Down
21 changes: 20 additions & 1 deletion test/share.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Addon } from '../src'
import type { Addon, Import } from '../src'

export function functionWrapAddon(): Addon {
return {
Expand Down Expand Up @@ -30,3 +30,22 @@ export function functionWrapAddon(): Addon {
},
}
}

export function resolverAddon(): Addon {
return {
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]
},
}
}

0 comments on commit 463a671

Please sign in to comment.