Skip to content

Commit

Permalink
feat: inject executor directly into runner (#2858)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 13, 2023
1 parent c420cb7 commit 62c4342
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
6 changes: 4 additions & 2 deletions docs/advanced/runner.md
Expand Up @@ -73,7 +73,7 @@ export interface VitestRunner {
*/
extendTestContext?(context: TestContext): TestContext
/**
* Called, when files are imported. Can be called in two situations: when collecting tests and when importing setup files.
* Called, when certain files are imported. Can be called in two situations: when collecting tests and when importing setup files.
*/
importFile(filepath: string, source: VitestRunnerImportSource): unknown
/**
Expand All @@ -86,7 +86,9 @@ export interface VitestRunner {
When initiating this class, Vitest passes down Vitest config, - you should expose it as a `config` property.

::: warning
`importFile` method in your custom runner must be inlined in `deps.inline` config option, if you call Node `import` inside.
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`).

`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.
:::

::: tip
Expand Down
6 changes: 0 additions & 6 deletions packages/vitest/src/node/config.ts
Expand Up @@ -17,12 +17,6 @@ const extraInlineDeps = [
/^(?!.*(?:node_modules)).*\.cjs\.js$/,
// Vite client
/vite\w*\/dist\/client\/env.mjs/,
// Vitest
/\/vitest\/dist\/runners\.js/,
// yarn's .store folder
/vitest-virtual-\w+\/dist\/runners\.js/,
// cnpm
/@vitest\/dist\/runners\.js/,
// Nuxt
'@nuxt/test-utils',
]
Expand Down
11 changes: 10 additions & 1 deletion packages/vitest/src/runtime/entry.ts
Expand Up @@ -13,6 +13,8 @@ import { setupGlobalEnv, withEnv } from './setup.node'
import { rpc } from './rpc'
import type { VitestExecutor } from './execute'

const runnersFile = resolve(distDir, 'runners.js')

function groupBy<T, K extends string | number | symbol>(collection: T[], iteratee: (item: T) => K) {
return collection.reduce((acc, item) => {
const key = iteratee(item)
Expand All @@ -24,7 +26,7 @@ function groupBy<T, K extends string | number | symbol>(collection: T[], iterate

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

// inject private executor to every runner
Object.defineProperty(testRunner, '__vitest_executor', {
value: executor,
enumerable: false,
configurable: false,
})

if (!testRunner.config)
testRunner.config = config

Expand Down
5 changes: 4 additions & 1 deletion packages/vitest/src/runtime/runners/benchmark.ts
Expand Up @@ -5,6 +5,7 @@ import { getBenchFn, getBenchOptions } from '../benchmark'
import { getWorkerState } from '../../utils'
import type { BenchTask, Benchmark, BenchmarkResult } from '../../types/benchmark'
import type { ResolvedConfig } from '../../types/config'
import type { VitestExecutor } from '../execute'

async function importTinybench() {
if (!globalThis.EventTarget)
Expand Down Expand Up @@ -121,12 +122,14 @@ async function runBenchmarkSuite(suite: Suite, runner: VitestRunner) {
}

export class NodeBenchmarkRunner implements VitestRunner {
private __vitest_executor!: VitestExecutor

constructor(public config: ResolvedConfig) {}

importFile(filepath: string, source: VitestRunnerImportSource): unknown {
if (source === 'setup')
getWorkerState().moduleCache.delete(filepath)
return import(filepath)
return this.__vitest_executor.executeId(filepath)
}

async runSuite(suite: Suite): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion packages/vitest/src/runtime/runners/test.ts
Expand Up @@ -5,17 +5,19 @@ import { vi } from '../../integrations/vi'
import { getFullName, getWorkerState } from '../../utils'
import { createExpect } from '../../integrations/chai/index'
import type { ResolvedConfig } from '../../types/config'
import type { VitestExecutor } from '../execute'

export class VitestTestRunner implements VitestRunner {
private snapshotClient = getSnapshotClient()
private workerState = getWorkerState()
private __vitest_executor!: VitestExecutor

constructor(public config: ResolvedConfig) {}

importFile(filepath: string, source: VitestRunnerImportSource): unknown {
if (source === 'setup')
this.workerState.moduleCache.delete(filepath)
return import(filepath)
return this.__vitest_executor.executeId(filepath)
}

onBeforeRun() {
Expand Down

0 comments on commit 62c4342

Please sign in to comment.