mirror of
https://github.com/vitest-dev/vitest.git
synced 2026-02-01 17:36:51 +00:00
fix: clear mocks between tests (#2857)
This commit is contained in:
parent
4ea1f1d974
commit
c420cb7b38
@ -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()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
{
|
||||
"name": "@vitest/test-single-thread",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"test": "vitest",
|
||||
|
||||
@ -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)))
|
||||
|
||||
@ -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)))
|
||||
|
||||
@ -1 +1,4 @@
|
||||
export const timeout = 200
|
||||
export const mockedFn = function () {
|
||||
return 'original'
|
||||
}
|
||||
|
||||
@ -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<string[]> {
|
||||
return files.sort()
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user