Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear mocks between tests #2857

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vitest/src/runtime/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
}
Expand Down
1 change: 1 addition & 0 deletions test/single-thread/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@vitest/test-single-thread",
"type": "module",
"private": true,
"scripts": {
"test": "vitest",
Expand Down
9 changes: 8 additions & 1 deletion test/single-thread/test/a.test.ts
Original file line number Diff line number Diff line change
@@ -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)))
16 changes: 15 additions & 1 deletion test/single-thread/test/b.test.ts
Original file line number Diff line number Diff line change
@@ -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)))
3 changes: 3 additions & 0 deletions test/single-thread/test/timeout.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const timeout = 200
export const mockedFn = function () {
return 'original'
}
8 changes: 8 additions & 0 deletions test/single-thread/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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()
}
},
},
},
})