From 5a3deba562cf950aa3c70df23c378094ae67a862 Mon Sep 17 00:00:00 2001 From: yoho Date: Mon, 5 Dec 2022 02:48:54 +0800 Subject: [PATCH] fix: tab effect vi.mock (#2402) * fix: tab effect vi.mock * test: tab effect --- packages/vitest/src/node/plugins/mock.ts | 2 +- test/core/test/tab-effect.spec.mjs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/core/test/tab-effect.spec.mjs 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') +})