-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: always run separate environments in isolation (#2860)
- Loading branch information
1 parent
f817618
commit 1f858e0
Showing
6 changed files
with
120 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { promises as fs } from 'node:fs' | ||
import mm from 'micromatch' | ||
import type { EnvironmentOptions, ResolvedConfig, VitestEnvironment } from '../types' | ||
import { groupBy } from './base' | ||
|
||
export const envsOrder = [ | ||
'node', | ||
'jsdom', | ||
'happy-dom', | ||
'edge-runtime', | ||
] | ||
|
||
export interface FileByEnv { | ||
file: string | ||
env: VitestEnvironment | ||
envOptions: EnvironmentOptions | null | ||
} | ||
|
||
export async function groupFilesByEnv(files: string[], config: ResolvedConfig) { | ||
const filesWithEnv = await Promise.all(files.map(async (file) => { | ||
const code = await fs.readFile(file, 'utf-8') | ||
|
||
// 1. Check for control comments in the file | ||
let env = code.match(/@(?:vitest|jest)-environment\s+?([\w-]+)\b/)?.[1] | ||
// 2. Check for globals | ||
if (!env) { | ||
for (const [glob, target] of config.environmentMatchGlobs || []) { | ||
if (mm.isMatch(file, glob)) { | ||
env = target | ||
break | ||
} | ||
} | ||
} | ||
// 3. Fallback to global env | ||
env ||= config.environment || 'node' | ||
|
||
const envOptions = JSON.parse(code.match(/@(?:vitest|jest)-environment-options\s+?(.+)/)?.[1] || 'null') | ||
return { | ||
file, | ||
environment: { | ||
name: env as VitestEnvironment, | ||
options: envOptions ? { [env]: envOptions } as EnvironmentOptions : null, | ||
}, | ||
} | ||
})) | ||
|
||
return groupBy(filesWithEnv, ({ environment }) => environment.name) | ||
} |