Skip to content

Commit

Permalink
refactor(vitest): reimplement hoistMocks to fix incorrect dynamic imp…
Browse files Browse the repository at this point in the history
…orts (#4664)
  • Loading branch information
Dunqing authored Jan 3, 2024
1 parent c95be72 commit e0b8c54
Show file tree
Hide file tree
Showing 13 changed files with 1,546 additions and 69 deletions.
12 changes: 12 additions & 0 deletions examples/mocks/src/set-foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { afterEach, beforeEach } from 'vitest'

// eslint-disable-next-line import/no-mutable-exports
export let foo: number

beforeEach(() => {
foo = 1
})

afterEach(() => {
foo = 2
})
3 changes: 3 additions & 0 deletions examples/mocks/src/squared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function squared() {

}
14 changes: 14 additions & 0 deletions examples/mocks/test/destructured.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as squaredModule from '../src/squared.js'
import { squared } from '../src/squared.js'
import { foo } from '../src/set-foo.js'

vi.mock('any')

test('spyOn entire module', () => {
vi.spyOn(squaredModule, 'squared')
expect(squared).not.toHaveBeenCalled()
})

test('foo should be 1', () => {
expect(foo).toBe(1)
})
3 changes: 1 addition & 2 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@
}
},
"dependencies": {
"estree-walker": "^3.0.3",
"@vitest/utils": "workspace:*",
"magic-string": "^0.30.5",
"sirv": "^2.0.4"
},
"devDependencies": {
"@types/estree": "^1.0.5",
"@types/ws": "^8.5.9",
"@vitest/runner": "workspace:*",
"@vitest/ui": "workspace:*",
Expand Down
25 changes: 7 additions & 18 deletions packages/browser/src/node/esmInjector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import MagicString from 'magic-string'
import { extract_names as extractNames } from 'periscopic'
import type { Expression, ImportDeclaration } from 'estree'
import type { PluginContext } from 'rollup'
import type { Node, Positioned } from './esmWalker'
import { esmWalker, isInDestructuringAssignment, isNodeInPattern, isStaticProperty } from './esmWalker'
import { esmWalker } from '@vitest/utils/ast'
import type { Expression, ImportDeclaration, Node, Positioned } from '@vitest/utils/ast'

const viInjectedKey = '__vi_inject__'
// const viImportMetaKey = '__vi_import_meta__' // to allow overwrite
Expand Down Expand Up @@ -209,26 +208,16 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex

// 3. convert references to import bindings & import.meta references
esmWalker(ast, {
onIdentifier(id, parent, parentStack) {
const grandparent = parentStack[1]
onIdentifier(id, info, parentStack) {
const binding = idToImportMap.get(id.name)
if (!binding)
return

if (isStaticProperty(parent) && parent.shorthand) {
// let binding used in a property shorthand
// { foo } -> { foo: __import_x__.foo }
// skip for destructuring patterns
if (
!isNodeInPattern(parent)
|| isInDestructuringAssignment(parent, parentStack)
)
s.appendLeft(id.end, `: ${binding}`)
if (info.hasBindingShortcut) {
s.appendLeft(id.end, `: ${binding}`)
}
else if (
(parent.type === 'PropertyDefinition'
&& grandparent?.type === 'ClassBody')
|| (parent.type === 'ClassDeclaration' && id === parent.superClass)
info.classDeclaration
) {
if (!declaredConst.has(id.name)) {
declaredConst.add(id.name)
Expand All @@ -239,7 +228,7 @@ export function injectVitestModule(code: string, id: string, parse: PluginContex
}
else if (
// don't transform class name identifier
!(parent.type === 'ClassExpression' && id === parent.id)
!info.classExpression
) {
s.update(id.start, id.end, binding)
}
Expand Down
8 changes: 7 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
"types": "./dist/diff.d.ts",
"default": "./dist/diff.js"
},
"./ast": {
"types": "./dist/ast.d.ts",
"default": "./dist/ast.js"
},
"./error": {
"types": "./dist/error.d.ts",
"default": "./dist/error.js"
Expand Down Expand Up @@ -59,10 +63,12 @@
},
"dependencies": {
"diff-sequences": "^29.6.3",
"estree-walker": "^3.0.3",
"loupe": "^2.3.7",
"pretty-format": "^29.7.0"
},
"devDependencies": {
"@jridgewell/trace-mapping": "^0.3.20"
"@jridgewell/trace-mapping": "^0.3.20",
"@types/estree": "^1.0.5"
}
}
1 change: 1 addition & 0 deletions packages/utils/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const entries = {
'index': 'src/index.ts',
'helpers': 'src/helpers.ts',
'diff': 'src/diff/index.ts',
'ast': 'src/ast/index.ts',
'error': 'src/error.ts',
'source-map': 'src/source-map.ts',
'types': 'src/types.ts',
Expand Down
File renamed without changes.
Loading

0 comments on commit e0b8c54

Please sign in to comment.