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

Function getSpecificationsOptions

packages/vitest/src/utils/test-helpers.ts:6–48  ·  view source on GitHub ↗
(
  specifications: Array<TestSpecification>,
)

Source from the content-addressed store, hash-verified

4import { promises as fs } from 'node:fs'
5
6export async function getSpecificationsOptions(
7 specifications: Array<TestSpecification>,
8): Promise<{
9 environments: WeakMap<TestSpecification, ContextTestEnvironment>
10 tags: WeakMap<TestSpecification, string[]>
11}> {
12 const environments = new WeakMap<TestSpecification, ContextTestEnvironment>()
13 const cache = new Map<string, string>()
14 const tags = new WeakMap<TestSpecification, string[]>()
15 await Promise.all(
16 specifications.map(async (spec) => {
17 const { moduleId: filepath, project, pool } = spec
18 // browser pool handles its own environment
19 if (pool === 'browser') {
20 return
21 }
22
23 // reuse if projects have the same test files
24 let code = cache.get(filepath)
25 if (!code) {
26 code = await fs.readFile(filepath, 'utf-8').catch(() => '')
27 cache.set(filepath, code)
28 }
29
30 const {
31 env = project.config.environment || 'node',
32 envOptions,
33 tags: specTags = [],
34 } = detectCodeBlock(code)
35 tags.set(spec, specTags)
36
37 const envKey = env === 'happy-dom' ? 'happyDOM' : env
38 const environment: ContextTestEnvironment = {
39 name: env as VitestEnvironment,
40 options: envOptions
41 ? ({ [envKey]: envOptions } as EnvironmentOptions)
42 : null,
43 }
44 environments.set(spec, environment)
45 }),
46 )
47 return { environments, tags }
48}
49
50export function detectCodeBlock(content: string): {
51 env?: string

Callers 1

executeTestsFunction · 0.90

Calls 5

detectCodeBlockFunction · 0.85
allMethod · 0.80
readFileMethod · 0.80
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected