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

feat: inject executor directly into runner #2858

Merged
merged 4 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: inject executor
  • Loading branch information
sheremet-va committed Feb 13, 2023
commit c033b2bad49546caf9727c14322acba5b9549b78
4 changes: 3 additions & 1 deletion docs/advanced/runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ export interface VitestRunner {
}
```

When initiating this class, Vitest passes down Vitest config, - you should expose it as a `config` property, and test executor, which is an instance of `ViteNodeRunner`.
When initiating this class, Vitest passes down Vitest config, - you should expose it as a `config` property.

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

Expand Down
3 changes: 0 additions & 3 deletions packages/runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
"dev": "rollup -c --watch",
"prepublishOnly": "pnpm build"
},
"peerDependencies": {
"vite-node": "*"
},
"dependencies": {
"@vitest/utils": "workspace:*",
"p-limit": "^4.0.0",
Expand Down
1 change: 0 additions & 1 deletion packages/runner/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const external = [
...builtinModules,
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
'vite-node/client',
]

const entries = {
Expand Down
3 changes: 1 addition & 2 deletions packages/runner/src/types/runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { ViteNodeRunner } from 'vite-node/client'
import type { File, SequenceHooks, Suite, TaskResult, Test, TestContext } from './tasks'

export interface VitestRunnerConfig {
Expand All @@ -21,7 +20,7 @@ export interface VitestRunnerConfig {
export type VitestRunnerImportSource = 'collect' | 'setup'

export interface VitestRunnerConstructor {
new (config: VitestRunnerConfig, executor: ViteNodeRunner): VitestRunner
new (config: VitestRunnerConfig): VitestRunner
}

export interface VitestRunner {
Expand Down
13 changes: 11 additions & 2 deletions packages/vitest/src/runtime/entry.ts
Original file line number Diff line number Diff line change
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 @@ -35,7 +37,14 @@ async function getTestRunnerConstructor(config: ResolvedConfig, executor: Vitest

async function getTestRunner(config: ResolvedConfig, executor: VitestExecutor): Promise<VitestRunner> {
const TestRunner = await getTestRunnerConstructor(config, executor)
const testRunner = new TestRunner(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
6 changes: 4 additions & 2 deletions packages/vitest/src/runtime/runners/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ async function runBenchmarkSuite(suite: Suite, runner: VitestRunner) {
}

export class NodeBenchmarkRunner implements VitestRunner {
constructor(public config: ResolvedConfig, private executor: VitestExecutor) {}
private __vitest_executor!: VitestExecutor

constructor(public config: ResolvedConfig) {}

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

async runSuite(suite: Suite): Promise<void> {
Expand Down
5 changes: 3 additions & 2 deletions packages/vitest/src/runtime/runners/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import type { VitestExecutor } from '../execute'
export class VitestTestRunner implements VitestRunner {
private snapshotClient = getSnapshotClient()
private workerState = getWorkerState()
private __vitest_executor!: VitestExecutor

constructor(public config: ResolvedConfig, private executor: VitestExecutor) {}
constructor(public config: ResolvedConfig) {}

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

onBeforeRun() {
Expand Down