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
50 changes: 33 additions & 17 deletions packages/vitest/src/runtime/entry.ts
@@ -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)

Expand All @@ -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 }) => {
nickmccurdy marked this conversation as resolved.
Show resolved Hide resolved
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)),
Expand All @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this case?

// file1.ts
// @vitest-environment custom
// @vitest-environment-options {"option1":1,"option2":2}

// file2.ts
// @vitest-environment custom
// @vitest-environment-options {"option2":2,"option1":1}

These files should run in the same environment, because they have the same values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I'm not sure that's beneficial enough to be worth the downsides. We would have to make the equality logic much more complex. Also from what I understand, the JSON and ECMAScript spec do not specify whether parsers need to be ordered, so we would need to hard code different logic for JavaScript runtimes and environments. Even if we only supported V8, we would have to either ban numerical keys (technically a breaking change since custom environments can have any keys), or introduce undefined runtime behavior (possibly a breaking change).

If you wanted to ensure better performance as a user, you could simply sort your keys with a linter. Worst case, we get an O(n) performance in exchange for respecting the ECMAScript spec, which I think is worth it.

Would something break if we made the wrong assumption about option equality for grouping envs? If not, then this is just a performance issue and maybe that argument doesn't hold much weight. However, in that case I think adding too much time complexity for edge cases like this could make tests slower than if they had some extra env instances. Also JSON parsing is synchronous, but we already parallelize envs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also from what I understand, the JSON and ECMAScript spec do not specify whether parsers need to be ordered, so we would need to hard code different logic for JavaScript runtimes and environments.

I think you overcomplicate things here, I'm sure we can create a string key that doesn't depend on key order. Sorting keys seems like an obvious solution.

If you wanted to ensure better performance as a user, you could simply sort your keys with a linter.

I don't think you can store them with linter in our docblock?

Would something break if we made the wrong assumption about option equality for grouping envs?

Most things that expect single JSOM instance will break, because we don't clear ESM cache in --no-threads: Vue, for example, stores "document" globally on import, so next tests with different "options" will fail, because global "document" will be different. This is actually why we have grouping.

I am sure this usecase will emerge, if people will use @vitet-environment-options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also from what I understand, the JSON and ECMAScript spec do not specify whether parsers need to be ordered, so we would need to hard code different logic for JavaScript runtimes and environments.

I think you overcomplicate things here, I'm sure we can create a string key that doesn't depend on key order. Sorting keys seems like an obvious solution.

That sounds like a good place to start, but I still feel like that's a lot of time complexity. I'll try some benchmarks.


for (const options of Object.keys(filesByOptions)) {
const files = filesByOptions[options]

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

await withEnv(environment, JSON.parse(options) || config.environmentOptions || {}, async () => {
nickmccurdy marked this conversation as resolved.
Show resolved Hide resolved
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
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