Skip to content

Commit

Permalink
fix: tab effect vi.mock (#2402)
Browse files Browse the repository at this point in the history
* fix: tab effect vi.mock

* test: tab effect
  • Loading branch information
poyoho committed Dec 4, 2022
1 parent 6d75630 commit 5a3deba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/vitest/src/node/plugins/mock.ts
Expand Up @@ -2,7 +2,7 @@ import type { Plugin } from 'vite'
import MagicString from 'magic-string'
import { getCallLastIndex } from '../../utils'

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

export function hoistMocks(code: string) {
Expand Down
20 changes: 20 additions & 0 deletions test/core/test/tab-effect.spec.mjs
@@ -0,0 +1,20 @@
/* eslint-disable eslint-comments/no-unlimited-disable */
/* eslint-disable */
import { expect, test, vi } from 'vitest'
import { join as joinPath } from 'node:path'

const helloWorld = () => {
return joinPath('hello', 'world')
}

test('Are you mocking me?', () => {
// note there are NO indents in this file
// except the next line
// test pass with spaces, test fails with tab
vi.mock('node:path', () => {
return {
join: vi.fn().mockReturnValue('goodbye world')
}
})
expect(helloWorld()).toBe('goodbye world')
})

0 comments on commit 5a3deba

Please sign in to comment.