-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: clear mocks between tests (#2857)
- Loading branch information
1 parent
4ea1f1d
commit c420cb7
Showing
6 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export const timeout = 200 | ||
export const mockedFn = function () { | ||
return 'original' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}, | ||
}, | ||
}, | ||
}) |