Skip to content

Commit 62c4342

Browse files
authoredFeb 13, 2023
feat: inject executor directly into runner (#2858)
1 parent c420cb7 commit 62c4342

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed
 

‎docs/advanced/runner.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface VitestRunner {
7373
*/
7474
extendTestContext?(context: TestContext): TestContext
7575
/**
76-
* Called, when files are imported. Can be called in two situations: when collecting tests and when importing setup files.
76+
* Called, when certain files are imported. Can be called in two situations: when collecting tests and when importing setup files.
7777
*/
7878
importFile(filepath: string, source: VitestRunnerImportSource): unknown
7979
/**
@@ -86,7 +86,9 @@ export interface VitestRunner {
8686
When initiating this class, Vitest passes down Vitest config, - you should expose it as a `config` property.
8787

8888
::: warning
89-
`importFile` method in your custom runner must be inlined in `deps.inline` config option, if you call Node `import` inside.
89+
Vitest also injects an instance of `ViteNodeRunner` as `__vitest_executor` property. You can use it to process files in `importFile` method (this is default behavior of `TestRunner`` and `BenchmarkRunner`).
90+
91+
`ViteNodeRunner` exposes `executeId` method, which is used to import test files in a Vite-friendly environment. Meaning, it will resolve imports and transform file content at runtime so that Node can understand it.
9092
:::
9193

9294
::: tip

‎packages/vitest/src/node/config.ts

-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ const extraInlineDeps = [
1717
/^(?!.*(?:node_modules)).*\.cjs\.js$/,
1818
// Vite client
1919
/vite\w*\/dist\/client\/env.mjs/,
20-
// Vitest
21-
/\/vitest\/dist\/runners\.js/,
22-
// yarn's .store folder
23-
/vitest-virtual-\w+\/dist\/runners\.js/,
24-
// cnpm
25-
/@vitest\/dist\/runners\.js/,
2620
// Nuxt
2721
'@nuxt/test-utils',
2822
]

‎packages/vitest/src/runtime/entry.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { setupGlobalEnv, withEnv } from './setup.node'
1313
import { rpc } from './rpc'
1414
import type { VitestExecutor } from './execute'
1515

16+
const runnersFile = resolve(distDir, 'runners.js')
17+
1618
function groupBy<T, K extends string | number | symbol>(collection: T[], iteratee: (item: T) => K) {
1719
return collection.reduce((acc, item) => {
1820
const key = iteratee(item)
@@ -24,7 +26,7 @@ function groupBy<T, K extends string | number | symbol>(collection: T[], iterate
2426

2527
async function getTestRunnerConstructor(config: ResolvedConfig, executor: VitestExecutor): Promise<VitestRunnerConstructor> {
2628
if (!config.runner) {
27-
const { VitestTestRunner, NodeBenchmarkRunner } = await executor.executeFile(resolve(distDir, 'runners.js'))
29+
const { VitestTestRunner, NodeBenchmarkRunner } = await executor.executeFile(runnersFile)
2830
return (config.mode === 'test' ? VitestTestRunner : NodeBenchmarkRunner) as VitestRunnerConstructor
2931
}
3032
const mod = await executor.executeId(config.runner)
@@ -37,6 +39,13 @@ async function getTestRunner(config: ResolvedConfig, executor: VitestExecutor):
3739
const TestRunner = await getTestRunnerConstructor(config, executor)
3840
const testRunner = new TestRunner(config)
3941

42+
// inject private executor to every runner
43+
Object.defineProperty(testRunner, '__vitest_executor', {
44+
value: executor,
45+
enumerable: false,
46+
configurable: false,
47+
})
48+
4049
if (!testRunner.config)
4150
testRunner.config = config
4251

‎packages/vitest/src/runtime/runners/benchmark.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getBenchFn, getBenchOptions } from '../benchmark'
55
import { getWorkerState } from '../../utils'
66
import type { BenchTask, Benchmark, BenchmarkResult } from '../../types/benchmark'
77
import type { ResolvedConfig } from '../../types/config'
8+
import type { VitestExecutor } from '../execute'
89

910
async function importTinybench() {
1011
if (!globalThis.EventTarget)
@@ -121,12 +122,14 @@ async function runBenchmarkSuite(suite: Suite, runner: VitestRunner) {
121122
}
122123

123124
export class NodeBenchmarkRunner implements VitestRunner {
125+
private __vitest_executor!: VitestExecutor
126+
124127
constructor(public config: ResolvedConfig) {}
125128

126129
importFile(filepath: string, source: VitestRunnerImportSource): unknown {
127130
if (source === 'setup')
128131
getWorkerState().moduleCache.delete(filepath)
129-
return import(filepath)
132+
return this.__vitest_executor.executeId(filepath)
130133
}
131134

132135
async runSuite(suite: Suite): Promise<void> {

‎packages/vitest/src/runtime/runners/test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ import { vi } from '../../integrations/vi'
55
import { getFullName, getWorkerState } from '../../utils'
66
import { createExpect } from '../../integrations/chai/index'
77
import type { ResolvedConfig } from '../../types/config'
8+
import type { VitestExecutor } from '../execute'
89

910
export class VitestTestRunner implements VitestRunner {
1011
private snapshotClient = getSnapshotClient()
1112
private workerState = getWorkerState()
13+
private __vitest_executor!: VitestExecutor
1214

1315
constructor(public config: ResolvedConfig) {}
1416

1517
importFile(filepath: string, source: VitestRunnerImportSource): unknown {
1618
if (source === 'setup')
1719
this.workerState.moduleCache.delete(filepath)
18-
return import(filepath)
20+
return this.__vitest_executor.executeId(filepath)
1921
}
2022

2123
onBeforeRun() {

0 commit comments

Comments
 (0)
Please sign in to comment.