Skip to content

Commit

Permalink
refactor: rename vm to experimental-vm-threads
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Apr 20, 2023
1 parent 286e9d8 commit 79c19c2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
3 changes: 3 additions & 0 deletions packages/vitest/src/node/cli-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export async function startVitest(
if (typeof options.browser === 'object' && !('enabled' in options.browser))
options.browser.enabled = true

if ('threads' in options && options.experimentalVmThreads)
throw new Error('Cannot use both "threads" (or "no-threads") and "experimentalVmThreads" at the same time.')

const ctx = await createVitest(mode, options, viteOverrides)

if (mode === 'test' && ctx.config.coverage.enabled) {
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cli
.option('--api [api]', 'Serve API, available options: --api.port <port>, --api.host [host] and --api.strictPort')
.option('--threads', 'Enabled threads (default: true)')
.option('--single-thread', 'Run tests inside a single thread, requires --threads (default: false)')
.option('--experimental-vm-threads', 'Run tests in a worker pool using VM isolation (default: false)')
.option('--silent', 'Silent console output from tests')
.option('--isolate', 'Isolate environment for each test file (default: true)')
.option('--reporter <name>', 'Specify reporters')
Expand Down
16 changes: 9 additions & 7 deletions packages/vitest/src/node/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Vitest } from './core'
import { createChildProcessPool } from './pools/child'
import { createThreadsPool } from './pools/threads'
import { createBrowserPool } from './pools/browser'
import { createVmPool } from './pools/vm'
import { createVmThreadsPool } from './pools/vm-threads'
import type { WorkspaceProject } from './workspace'

export type WorkspaceSpec = [project: WorkspaceProject, testFile: string]
Expand All @@ -31,12 +31,14 @@ export function createPool(ctx: Vitest): ProcessPool {
child_process: null,
threads: null,
browser: null,
vm: null,
experimentalVmThreads: null,
}

function getDefaultPoolName(project: WorkspaceProject) {
function getDefaultPoolName(project: WorkspaceProject): VitestPool {
if (project.config.browser.enabled)
return 'browser'
if (project.config.experimentalVmThreads)
return 'experimentalVmThreads'
if (project.config.threads)
return 'threads'
return 'child_process'
Expand Down Expand Up @@ -87,7 +89,7 @@ export function createPool(ctx: Vitest): ProcessPool {
child_process: [] as WorkspaceSpec[],
threads: [] as WorkspaceSpec[],
browser: [] as WorkspaceSpec[],
vm: [] as WorkspaceSpec[],
experimentalVmThreads: [] as WorkspaceSpec[],
}

for (const spec of files) {
Expand All @@ -104,9 +106,9 @@ export function createPool(ctx: Vitest): ProcessPool {
return pools.browser.runTests(files, invalidate)
}

if (pool === 'vm') {
pools.vm ??= createVmPool(ctx, options)
return pools.vm.runTests(files, invalidate)
if (pool === 'experimentalVmThreads') {
pools.experimentalVmThreads ??= createVmThreadsPool(ctx, options)
return pools.experimentalVmThreads.runTests(files, invalidate)
}

if (pool === 'threads') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function createWorkerChannel(project: WorkspaceProject) {
return { workerPort, port }
}

export function createVmPool(ctx: Vitest, { execArgv, env }: PoolProcessOptions): ProcessPool {
export function createVmThreadsPool(ctx: Vitest, { execArgv, env }: PoolProcessOptions): ProcessPool {
const threadsCount = ctx.config.watch
? Math.max(Math.floor(cpus().length / 2), 1)
: Math.max(cpus().length - 1, 1)
Expand Down Expand Up @@ -117,14 +117,6 @@ export function createVmPool(ctx: Vitest, { execArgv, env }: PoolProcessOptions)
return config
}

const workspaceMap = new Map<string, WorkspaceProject[]>()
for (const [project, file] of specs) {
const workspaceFiles = workspaceMap.get(file) ?? []
workspaceFiles.push(project)
workspaceMap.set(file, workspaceFiles)
}

// it's possible that project defines a file that is also defined by another project
const { shard } = ctx.config

if (shard)
Expand Down
12 changes: 11 additions & 1 deletion packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type { SequenceHooks, SequenceSetupFiles } from '@vitest/runner'
export type BuiltinEnvironment = 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime'
// Record is used, so user can get intellisense for builtin environments, but still allow custom environments
export type VitestEnvironment = BuiltinEnvironment | (string & Record<never, never>)
export type VitestPool = 'browser' | 'threads' | 'child_process' | 'vm'
export type VitestPool = 'browser' | 'threads' | 'child_process' | 'experimentalVmThreads'
export type CSSModuleScopeStrategy = 'stable' | 'scoped' | 'non-scoped'

export type ApiConfig = Pick<CommonServerOptions, 'port' | 'strictPort' | 'host'>
Expand Down Expand Up @@ -250,6 +250,16 @@ export interface InlineConfig {
*/
outputFile?: string | (Partial<Record<BuiltinReporters, string>> & Record<string, string>)

/**
* Run tests using VM context in a worker pool.
*
* This makes tests run faster, but VM module is unstable. Your tests might leak memory.
*/
experimentalVmThreads?: boolean

// TODO: document that "--no-isolate" has no effect on experimentalVmThreads
// TODO: workerIdleMemoryLimit for experimentalVmThreads, so they don't leak

/**
* Enable multi-threading
*
Expand Down
2 changes: 1 addition & 1 deletion test/core/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default defineConfig({
'./test/setup.ts',
],
poolMatchGlobs: [
['**/*', 'vm'],
['**/*', 'experimentalVmThreads'],
],
testNamePattern: '^((?!does not include test that).)*$',
coverage: {
Expand Down

0 comments on commit 79c19c2

Please sign in to comment.