Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
91 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type { Awaitable } from '../types' | ||
import { getWorkerState } from '../utils' | ||
|
||
const filesCount = new Map<string, number>() | ||
const cache = new Map<string, any>() | ||
|
||
/** | ||
* This utils allows computational intensive tasks to only be ran once | ||
* across test reruns to improve the watch mode performance. | ||
* | ||
* Currently only works with `isolate: false` | ||
* | ||
* @experimental | ||
*/ | ||
export async function runOnce<T>(fn: (() => Awaitable<T>), key?: string): Promise<T> { | ||
if (!key) { | ||
const filepath = getWorkerState().filepath || '__unknown_files__' | ||
filesCount.set(filepath, (filesCount.get(filepath) || 0) + 1) | ||
const count = filesCount.get(filepath)! | ||
key = `${filepath}:${count}` | ||
} | ||
|
||
if (!cache.has(key)) | ||
cache.set(key, fn()) | ||
|
||
return await cache.get(key) | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function resetRunOnceCounter() { | ||
filesCount.clear() | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@vitest/test-run-once", | ||
"private": true, | ||
"scripts": { | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage" | ||
}, | ||
"devDependencies": { | ||
"vitest": "workspace:*" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, it, runOnce } from 'vitest' | ||
|
||
let dummy = 0 | ||
|
||
const one = await runOnce(() => { | ||
dummy += 1 | ||
return 1 | ||
}) | ||
|
||
const two = await runOnce(async() => { | ||
dummy += 1 | ||
return 2 | ||
}) | ||
|
||
it('run once', async() => { | ||
expect(one).toBe(1) | ||
expect(two).toBe(2) | ||
|
||
// edit the file to trigger the hmr and dummy should be 0 | ||
expect(dummy).toBe(2) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from 'vite' | ||
|
||
export default defineConfig({ | ||
test: { | ||
isolate: false, | ||
}, | ||
}) |