From c420cb7b38cb194af4efaa3a1a1c9bdfb6aff5ea Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 13 Feb 2023 10:33:44 +0100 Subject: [PATCH] fix: clear mocks between tests (#2857) --- packages/vitest/src/runtime/entry.ts | 2 ++ test/single-thread/package.json | 1 + test/single-thread/test/a.test.ts | 9 ++++++++- test/single-thread/test/b.test.ts | 16 +++++++++++++++- test/single-thread/test/timeout.ts | 3 +++ test/single-thread/vitest.config.ts | 8 ++++++++ 6 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/runtime/entry.ts b/packages/vitest/src/runtime/entry.ts index 97cc6efd7..2b129796b 100644 --- a/packages/vitest/src/runtime/entry.ts +++ b/packages/vitest/src/runtime/entry.ts @@ -144,6 +144,8 @@ export async function run(files: string[], config: ResolvedConfig, executor: Vit // reset after tests, because user might call `vi.setConfig` in setupFile vi.resetConfig() + // mocks should not affect different files + vi.restoreAllMocks() } }) } diff --git a/test/single-thread/package.json b/test/single-thread/package.json index 25ceb3cbe..efc8e44b2 100644 --- a/test/single-thread/package.json +++ b/test/single-thread/package.json @@ -1,5 +1,6 @@ { "name": "@vitest/test-single-thread", + "type": "module", "private": true, "scripts": { "test": "vitest", diff --git a/test/single-thread/test/a.test.ts b/test/single-thread/test/a.test.ts index 52a107225..3324f2317 100644 --- a/test/single-thread/test/a.test.ts +++ b/test/single-thread/test/a.test.ts @@ -1,4 +1,11 @@ -import { it } from 'vitest' +import fs from 'node:fs' +import { expect, it, vi } from 'vitest' import { timeout } from './timeout' +// this file is running first, it should not affect file "b.test.ts" +it('mock is mocked', () => { + vi.spyOn(fs, 'readFileSync').mockReturnValue('mocked') + expect(fs.readFileSync('')).toBe('mocked') +}) + it('timeout', () => new Promise(resolve => setTimeout(resolve, timeout))) diff --git a/test/single-thread/test/b.test.ts b/test/single-thread/test/b.test.ts index 52a107225..e4b82b89b 100644 --- a/test/single-thread/test/b.test.ts +++ b/test/single-thread/test/b.test.ts @@ -1,4 +1,18 @@ -import { it } from 'vitest' +import fs from 'node:fs' +import { fileURLToPath } from 'node:url' +import { dirname, resolve } from 'pathe' +import { expect, it } from 'vitest' import { timeout } from './timeout' +// this file is running second, it should not be affected by mock in "a.test.ts" +it('mock is mocked', () => { + expect(fs.readFileSync(resolve(dirname(fileURLToPath(import.meta.url)), './timeout.ts'), 'utf-8')).toMatchInlineSnapshot(` + "export const timeout = 200 + export const mockedFn = function () { + return 'original' + } + " + `) +}) + it('timeout', () => new Promise(resolve => setTimeout(resolve, timeout))) diff --git a/test/single-thread/test/timeout.ts b/test/single-thread/test/timeout.ts index 67d0ace7f..980598ac3 100644 --- a/test/single-thread/test/timeout.ts +++ b/test/single-thread/test/timeout.ts @@ -1 +1,4 @@ export const timeout = 200 +export const mockedFn = function () { + return 'original' +} diff --git a/test/single-thread/vitest.config.ts b/test/single-thread/vitest.config.ts index c345a582e..686b625d3 100644 --- a/test/single-thread/vitest.config.ts +++ b/test/single-thread/vitest.config.ts @@ -1,7 +1,15 @@ import { defineConfig } from 'vite' +import { BaseSequencer } from 'vitest/node' export default defineConfig({ test: { threads: false, + sequence: { + sequencer: class Sequences extends BaseSequencer { + public async sort(files: string[]): Promise { + return files.sort() + } + }, + }, }, })