mirror of
https://github.com/vitest-dev/vitest.git
synced 2026-02-01 17:36:51 +00:00
* chore: add failing test for #1962 * fix: parse env options from comments (fix #1962) * fix: define options before destructuring * fix: order envs with similar options together * fix: check if files are empty * fix: add missing env key to envOptions * fix: better conditional logic for envOptions * fix: change undefined to null * fix: remove extra parsing overhead
This commit is contained in:
parent
3d5f50232c
commit
99b7d9e37c
@ -1,10 +1,19 @@
|
||||
import { promises as fs } from 'fs'
|
||||
import type { ResolvedConfig, VitestEnvironment } from '../types'
|
||||
import type { EnvironmentOptions, ResolvedConfig, VitestEnvironment } from '../types'
|
||||
import { getWorkerState, resetModules } from '../utils'
|
||||
import { envs } from '../integrations/env'
|
||||
import { setupGlobalEnv, withEnv } from './setup'
|
||||
import { startTests } from './run'
|
||||
|
||||
function groupBy<T, K extends string | number | symbol >(collection: T[], iteratee: (item: T) => K) {
|
||||
return collection.reduce((acc, item) => {
|
||||
const key = iteratee(item)
|
||||
acc[key] ||= []
|
||||
acc[key].push(item)
|
||||
return acc
|
||||
}, {} as Record<K, T[]>)
|
||||
}
|
||||
|
||||
export async function run(files: string[], config: ResolvedConfig): Promise<void> {
|
||||
await setupGlobalEnv(config)
|
||||
|
||||
@ -22,17 +31,15 @@ export async function run(files: string[], config: ResolvedConfig): Promise<void
|
||||
const filesWithEnv = await Promise.all(files.map(async (file) => {
|
||||
const code = await fs.readFile(file, 'utf-8')
|
||||
const env = code.match(/@(?:vitest|jest)-environment\s+?([\w-]+)\b/)?.[1] || config.environment || 'node'
|
||||
const envOptions = JSON.parse(code.match(/@(?:vitest|jest)-environment-options\s+?(.+)/)?.[1] || 'null')
|
||||
return {
|
||||
file,
|
||||
env: env as VitestEnvironment,
|
||||
envOptions: envOptions ? { [env]: envOptions } as EnvironmentOptions : null,
|
||||
}
|
||||
}))
|
||||
|
||||
const filesByEnv = filesWithEnv.reduce((acc, { file, env }) => {
|
||||
acc[env] ||= []
|
||||
acc[env].push(file)
|
||||
return acc
|
||||
}, {} as Record<VitestEnvironment, string[]>)
|
||||
const filesByEnv = groupBy(filesWithEnv, ({ env }) => env)
|
||||
|
||||
const orderedEnvs = envs.concat(
|
||||
Object.keys(filesByEnv).filter(env => !envs.includes(env)),
|
||||
@ -45,22 +52,31 @@ export async function run(files: string[], config: ResolvedConfig): Promise<void
|
||||
if (!files || !files.length)
|
||||
continue
|
||||
|
||||
await withEnv(environment, config.environmentOptions || {}, async () => {
|
||||
for (const file of files) {
|
||||
const filesByOptions = groupBy(files, ({ envOptions }) => JSON.stringify(envOptions))
|
||||
|
||||
for (const options of Object.keys(filesByOptions)) {
|
||||
const files = filesByOptions[options]
|
||||
|
||||
if (!files || !files.length)
|
||||
continue
|
||||
|
||||
await withEnv(environment, files[0].envOptions || config.environmentOptions || {}, async () => {
|
||||
for (const { file } of files) {
|
||||
// it doesn't matter if running with --threads
|
||||
// if running with --no-threads, we usually want to reset everything before running a test
|
||||
// but we have --isolate option to disable this
|
||||
if (config.isolate) {
|
||||
workerState.mockMap.clear()
|
||||
resetModules(workerState.moduleCache, true)
|
||||
if (config.isolate) {
|
||||
workerState.mockMap.clear()
|
||||
resetModules(workerState.moduleCache, true)
|
||||
}
|
||||
|
||||
workerState.filepath = file
|
||||
|
||||
await startTests([file], config)
|
||||
|
||||
workerState.filepath = undefined
|
||||
}
|
||||
|
||||
workerState.filepath = file
|
||||
|
||||
await startTests([file], config)
|
||||
|
||||
workerState.filepath = undefined
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
/**
|
||||
* @vitest-environment jsdom
|
||||
* @vitest-environment-options { "url": "https://example.com/" }
|
||||
*/
|
||||
|
||||
/* eslint-disable vars-on-top */
|
||||
@ -16,6 +17,7 @@ it('jsdom', () => {
|
||||
expect(top).toBeDefined()
|
||||
expect(parent).toBeDefined()
|
||||
expect(self).toBeDefined()
|
||||
expect(location.href).toBe('https://example.com/')
|
||||
|
||||
const dom = document.createElement('a')
|
||||
dom.href = 'https://vitest.dev'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user