MCPcopy
hub / github.com/vitest-dev/vitest / setupBaseEnvironment

Function setupBaseEnvironment

packages/vitest/src/runtime/workers/base.ts:72–131  ·  view source on GitHub ↗
(context: WorkerSetupContext)

Source from the content-addressed store, hash-verified

70
71/** @experimental */
72export async function setupBaseEnvironment(context: WorkerSetupContext): Promise<() => Promise<void>> {
73 if (context.config.experimental.viteModuleRunner === false) {
74 const { setupNodeLoaderHooks } = await import('./native')
75 await setupNodeLoaderHooks(context)
76 }
77
78 const startTime = performance.now()
79 const {
80 environment: { name: environmentName, options: environmentOptions },
81 rpc,
82 config,
83 } = context
84
85 // we could load @vite/env, but it would take ~8ms, while this takes ~0,02ms
86 if (context.config.serializedDefines) {
87 try {
88 runInThisContext(`(() =>{\n${context.config.serializedDefines}})()`, {
89 lineOffset: 1,
90 filename: 'virtual:load-defines.js',
91 })
92 }
93 catch (error: any) {
94 throw new Error(`Failed to load custom "defines": ${error.message}`)
95 }
96 }
97 const otel = context.traces
98
99 const { environment, loader } = await loadEnvironment(
100 environmentName,
101 config.root,
102 rpc,
103 otel,
104 context.config.experimental.viteModuleRunner,
105 )
106 _currentEnvironment = environment
107 const env = await otel.$(
108 'vitest.runtime.environment.setup',
109 {
110 attributes: {
111 'vitest.environment': environment.name,
112 'vitest.environment.vite_environment': environment.viteEnvironment || environment.name,
113 },
114 },
115 () => environment.setup(globalThis, environmentOptions || config.environmentOptions || {}),
116 )
117
118 _environmentTime = performance.now() - startTime
119
120 if (config.chaiConfig) {
121 setupChaiConfig(config.chaiConfig)
122 }
123
124 return async () => {
125 await otel.$(
126 'vitest.runtime.environment.teardown',
127 () => env.teardown(globalThis),
128 )
129 await loader?.close()

Callers

nothing calls this directly

Calls 6

loadEnvironmentFunction · 0.90
setupChaiConfigFunction · 0.90
setupNodeLoaderHooksFunction · 0.85
$Method · 0.80
setupMethod · 0.80
closeMethod · 0.45

Tested by

no test coverage detected