Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse env options from comments (fix #1962) #1984

Merged
merged 10 commits into from Oct 10, 2022
47 changes: 15 additions & 32 deletions packages/vitest/src/runtime/entry.ts
@@ -1,7 +1,6 @@
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'

Expand All @@ -22,45 +21,29 @@ 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 as EnvironmentOptions | undefined,
}
}))

const filesByEnv = filesWithEnv.reduce((acc, { file, env }) => {
nickmccurdy marked this conversation as resolved.
Show resolved Hide resolved
acc[env] ||= []
acc[env].push(file)
return acc
}, {} as Record<VitestEnvironment, string[]>)

const orderedEnvs = envs.concat(
Object.keys(filesByEnv).filter(env => !envs.includes(env)),
)

for (const env of orderedEnvs) {
const environment = env as VitestEnvironment
const files = filesByEnv[environment]

if (!files || !files.length)
continue

await withEnv(environment, 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)
}
for (const { file, env, envOptions } of filesWithEnv) {
await withEnv(env, envOptions || config.environmentOptions, async () => {
// 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)
}

workerState.filepath = file
workerState.filepath = file

await startTests([file], config)
await startTests([file], config)

workerState.filepath = undefined
}
workerState.filepath = undefined
})
}
}
2 changes: 2 additions & 0 deletions test/core/test/dom.test.ts
@@ -1,5 +1,6 @@
/**
* @vitest-environment jsdom
* @vitest-environment-options { "url": "https://example.com/" }
*/

/* eslint-disable vars-on-top */
Expand All @@ -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'
Expand Down