diff --git a/packages/vitest/src/utils/test-helpers.ts b/packages/vitest/src/utils/test-helpers.ts index b7aacae339c4..1b37c5155887 100644 --- a/packages/vitest/src/utils/test-helpers.ts +++ b/packages/vitest/src/utils/test-helpers.ts @@ -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, diff --git a/test/core/test/dom-single-line-options.test.ts b/test/core/test/dom-single-line-options.test.ts new file mode 100644 index 000000000000..d3338fd39026 --- /dev/null +++ b/test/core/test/dom-single-line-options.test.ts @@ -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/'))