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: one-line environment options #5105

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/vitest/src/utils/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ export async function groupFilesByEnv(
file,
)

const envOptions = JSON.parse(
code.match(/@(?:vitest|jest)-environment-options\s+?(.+)/)?.[1]
|| 'null',
)
let envOptionsJson = code.match(/@(?:vitest|jest)-environment-options\s+(.+)/)?.[1]
if (envOptionsJson?.endsWith('*/')) {
// Trim closing Docblock characters the above regex might have captured
envOptionsJson = envOptionsJson.slice(0, -2)
}

const envOptions = JSON.parse(envOptionsJson || 'null')
const envKey = env === 'happy-dom' ? 'happyDOM' : env
const environment: ContextTestEnvironment = {
name: env as VitestEnvironment,
Expand Down
7 changes: 7 additions & 0 deletions test/core/test/dom-single-line-options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @vitest-environment jsdom */

/** @vitest-environment-options { "url": "https://example.com/" } */

import { expect, it } from 'vitest'

it('parse single line environment options', () => expect(location.href).toBe('https://example.com/'))