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(vitest): vi.hoisted syntax errors on instrumented code #6184

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/vitest/src/node/hoistMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ export function hoistMocks(
if (
init
&& (init === node
|| (init.type === 'AwaitExpression' && init.argument === node))
|| (init.type === 'AwaitExpression' && init.argument === node)
|| (init.type === 'SequenceExpression' && init.expressions.includes(node)))
) {
return declarationNode
}
Expand Down Expand Up @@ -432,6 +433,8 @@ export function hoistMocks(
const hoistedCode = hoistedNodes
.map((node) => {
const end = getBetterEnd(code, node)
let replaceNodesCode = ''

/**
* In the following case, we need to change the `user` to user: __vi_import_x__.user
* So we should get the latest code from `s`.
Expand Down Expand Up @@ -474,7 +477,36 @@ export function hoistMocks(
}
}

if (
node.type === 'VariableDeclaration'
&& node.declarations[0]?.init?.type === 'SequenceExpression'
) {
const sequence = node.declarations[0].init
const callExpression = sequence.expressions.find(exp =>
exp.type === 'CallExpression'
&& exp.callee.type === 'MemberExpression'
&& isIdentifier(exp.callee.object)
&& (exp.callee.object.name === 'vi'),
)!

nodeCode
= s.slice(node.start, sequence.start)
+ s.slice(callExpression.start, callExpression.end)
+ s.slice(sequence.end, node.end)

for (const exp of sequence.expressions) {
if (exp !== callExpression) {
replaceNodesCode += `${s.slice(exp.start as number, exp.end as number)}\n`
}
}
}

s.remove(node.start, end)

if (replaceNodesCode.length > 0) {
s.prependLeft(node.start, replaceNodesCode)
}

return `${nodeCode}${nodeCode.endsWith('\n') ? '' : '\n'}`
})
.join('')
Expand Down
10 changes: 10 additions & 0 deletions test/core/test/hoisted-simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const globalValue = vi.hoisted(() => {
return 'globalValue'
})

let coverageCounter = 0
const nestedHoist = (coverageCounter++, vi.hoisted(() => {
return 'Nested hoist'
}))

afterAll(() => {
// @ts-expect-error not typed global
delete globalThis.someGlobalValue
Expand All @@ -17,3 +22,8 @@ afterAll(() => {
it('imported value is equal to returned from hoisted', () => {
expect(value).toBe(globalValue)
})

it('nesting vi.hoisted doesnt cause syntax errors', () => {
expect(nestedHoist).toBe('Nested hoist')
expect(coverageCounter).toBe(1)
})
Loading