Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mocker): respect namespace import when hoisting vi.mock #3547

Merged
merged 1 commit into from Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/vitest/src/node/hoistMocks.ts
@@ -1,5 +1,5 @@
import MagicString from 'magic-string'
import type { CallExpression, Identifier, ImportDeclaration, VariableDeclaration, Node as _Node } from 'estree'
import type { CallExpression, Identifier, ImportDeclaration, ImportNamespaceSpecifier, VariableDeclaration, Node as _Node } from 'estree'
import { findNodeAround, simple as simpleWalk } from 'acorn-walk'
import type { AcornNode } from 'rollup'

Expand All @@ -24,11 +24,6 @@ function isIdentifier(node: any): node is Positioned<Identifier> {
}

function transformImportSpecifiers(node: ImportDeclaration) {
const specifiers = node.specifiers

if (specifiers.length === 1 && specifiers[0].type === 'ImportNamespaceSpecifier')
return specifiers[0].local.name

const dynamicImports = node.specifiers.map((specifier) => {
if (specifier.type === 'ImportDefaultSpecifier')
return `default: ${specifier.local.name}`
Expand Down Expand Up @@ -86,12 +81,25 @@ export function hoistMocks(code: string, id: string, parse: (code: string, optio
const transformImportDeclaration = (node: ImportDeclaration) => {
const source = node.source.value as string

const namespace = node.specifiers.find(specifier => specifier.type === 'ImportNamespaceSpecifier') as ImportNamespaceSpecifier | undefined

let code = ''
if (namespace)
code += `const ${namespace.local.name} = await import('${source}')\n`

// if we don't hijack ESM and process this file, then we definetly have mocks,
// so we need to transform imports into dynamic ones, so "vi.mock" can be executed before
const specifiers = transformImportSpecifiers(node)
const code = specifiers
? `const ${specifiers} = await import('${source}')\n`
: `await import('${source}')\n`

if (specifiers) {
if (namespace)
code += `const ${specifiers} = ${namespace.local.name}\n`
else
code += `const ${specifiers} = await import('${source}')\n`
}
else if (!namespace) {
code += `await import('${source}')\n`
}
return code
}

Expand Down
13 changes: 13 additions & 0 deletions test/core/test/injector-mock.test.ts
Expand Up @@ -58,3 +58,16 @@ test('always hoists all imports but they are under mocks', () => {
const { someValue2 } = await import('./path2.js')"
`)
})

test('correctly mocks namespaced', () => {
expect(hoistSimpleCode(`
import { vi } from 'vitest'
import add, * as AddModule from '../src/add'
vi.mock('../src/add', () => {})
`)).toMatchInlineSnapshot(`
"const { vi } = await import('vitest')
vi.mock('../src/add', () => {})
const AddModule = await import('../src/add')
const { default: add } = AddModule"
`)
})