( specifications: Array<TestSpecification>, )
| 4 | import { promises as fs } from 'node:fs' |
| 5 | |
| 6 | export 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 | |
| 50 | export function detectCodeBlock(content: string): { |
| 51 | env?: string |
no test coverage detected