diff --git a/packages/vitest/src/node/plugins/mock.ts b/packages/vitest/src/node/plugins/mock.ts index c41c7c65c420..f05598b4f0ae 100644 --- a/packages/vitest/src/node/plugins/mock.ts +++ b/packages/vitest/src/node/plugins/mock.ts @@ -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) { diff --git a/test/core/test/tab-effect.spec.mjs b/test/core/test/tab-effect.spec.mjs new file mode 100644 index 000000000000..840a43864fdc --- /dev/null +++ b/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') +})