Skip to content

Commit 5a3deba

Browse files
authoredDec 4, 2022
fix: tab effect vi.mock (#2402)
* fix: tab effect vi.mock * test: tab effect
1 parent 6d75630 commit 5a3deba

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed
 

‎packages/vitest/src/node/plugins/mock.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Plugin } from 'vite'
22
import MagicString from 'magic-string'
33
import { getCallLastIndex } from '../../utils'
44

5-
const hoistRegexp = /^ *\b((?:vitest|vi)\s*.\s*(mock|unmock)\(["`'\s]+(.*[@\w_-]+)["`'\s]+)[),]{1};?/gm
5+
const hoistRegexp = /^[ \t]*\b((?:vitest|vi)\s*.\s*(mock|unmock)\(["`'\s]+(.*[@\w_-]+)["`'\s]+)[),]{1};?/gm
66
const vitestRegexp = /import {[^}]*}.*(?=["'`]vitest["`']).*/gm
77

88
export function hoistMocks(code: string) {

‎test/core/test/tab-effect.spec.mjs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-disable eslint-comments/no-unlimited-disable */
2+
/* eslint-disable */
3+
import { expect, test, vi } from 'vitest'
4+
import { join as joinPath } from 'node:path'
5+
6+
const helloWorld = () => {
7+
return joinPath('hello', 'world')
8+
}
9+
10+
test('Are you mocking me?', () => {
11+
// note there are NO indents in this file
12+
// except the next line
13+
// test pass with spaces, test fails with tab
14+
vi.mock('node:path', () => {
15+
return {
16+
join: vi.fn().mockReturnValue('goodbye world')
17+
}
18+
})
19+
expect(helloWorld()).toBe('goodbye world')
20+
})

0 commit comments

Comments
 (0)
Please sign in to comment.