Skip to content

Commit

Permalink
fix: one-line environment options (#5105)
Browse files Browse the repository at this point in the history
  • Loading branch information
hahanein authored Jul 15, 2024
1 parent 81b8d67 commit 3826941
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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/'))

0 comments on commit 3826941

Please sign in to comment.