Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 11, 2023
1 parent c0467b7 commit 0fc32ed
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 134 deletions.
19 changes: 12 additions & 7 deletions packages/vitest/src/runtime/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import type { CancelReason } from '@vitest/runner'
import type { ResolvedConfig, WorkerGlobalState } from '../types'
import type { RunnerRPC, RuntimeRPC } from '../types/rpc'
import type { ChildContext } from '../types/child'
import { loadEnvironment } from '../integrations/env'
import { mockMap, moduleCache, startViteNode } from './execute'
import { createSafeRpc, rpcDone } from './rpc'
import { setupInspect } from './inspector'

function init(ctx: ChildContext) {
const { config, environment } = ctx
async function init(ctx: ChildContext) {
const { config } = ctx

process.env.VITEST_WORKER_ID = '1'
process.env.VITEST_POOL_ID = '1'
Expand All @@ -36,13 +37,17 @@ function init(ctx: ChildContext) {
},
)

const state = {
const environment = await loadEnvironment(ctx.environment.name, ctx.config.root)
if (ctx.environment.transformMode)
environment.transformMode = ctx.environment.transformMode

const state: WorkerGlobalState = {
ctx,
moduleCache,
config,
mockMap,
onCancel,
environment: config.environment,
environment,
durations: {
environment: 0,
prepare: performance.now(),
Expand All @@ -61,7 +66,7 @@ function init(ctx: ChildContext) {
}
ctx.files.forEach(i => moduleCache.delete(i))

return state as unknown as WorkerGlobalState
return state
}

function parsePossibleRegexp(str: string | RegExp) {
Expand All @@ -81,11 +86,11 @@ export async function run(ctx: ChildContext) {
const inspectorCleanup = setupInspect(ctx.config)

try {
const state = init(ctx)
const state = await init(ctx)
const { run, executor } = await startViteNode({
state,
})
await run(ctx.files, ctx.config, ctx.environment, executor)
await run(ctx.files, ctx.config, { environment: state.environment, options: ctx.environment.options }, executor)
await rpcDone()
}
finally {
Expand Down
3 changes: 1 addition & 2 deletions packages/vitest/src/runtime/entry-vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export async function run(files: string[], config: ResolvedConfig, executor: Vit

setupColors(createColors(isatty(1)))

// TODO: look at environment.transformMode when #3491 is merged
if (workerState.environment !== 'node') {
if (workerState.environment.transformMode === 'web') {
const _require = createRequire(import.meta.url)
// always mock "required" `css` files, because we cannot process them
_require.extensions['.css'] = () => ({})
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface AfterSuiteRunMeta {
export type WorkerRPC = BirpcReturn<RuntimeRPC>

export interface WorkerGlobalState {
ctx: WorkerContext
ctx: ContextRPC
config: ResolvedConfig
rpc: WorkerRPC
current?: Test
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/utils/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export function getWorkerState(): WorkerGlobalState {

export function getCurrentEnvironment(): string {
const state = getWorkerState()
return state?.environment
return state?.environment.name
}
Loading

0 comments on commit 0fc32ed

Please sign in to comment.