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: tab effect vi.mock #2402

Merged
merged 2 commits into from Dec 4, 2022
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
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')
})