fix: clear mocks between tests (#2857)

This commit is contained in:
Vladimir 2023-02-13 10:33:44 +01:00 committed by GitHub
parent 4ea1f1d974
commit c420cb7b38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 2 deletions

View File

@ -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()
}
})
}

View File

@ -1,5 +1,6 @@
{
"name": "@vitest/test-single-thread",
"type": "module",
"private": true,
"scripts": {
"test": "vitest",

View File

@ -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)))

View File

@ -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)))

View File

@ -1 +1,4 @@
export const timeout = 200
export const mockedFn = function () {
return 'original'
}

View File

@ -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()
}
},
},
},
})