From f751de37953d15859235577349825d588a2b01ad Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Tue, 19 Dec 2023 12:17:12 +0100 Subject: [PATCH] refactor: correctly override isolate flag --- packages/vitest/src/defaults.ts | 1 + packages/vitest/src/node/config.ts | 9 -- packages/vitest/src/node/plugins/index.ts | 10 +++ test/config/test/resolution.test.ts | 104 ++++++++++++++++++++++ 4 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 test/config/test/resolution.test.ts diff --git a/packages/vitest/src/defaults.ts b/packages/vitest/src/defaults.ts index 2d1b2ad57e8a..cbc5570ffdd9 100644 --- a/packages/vitest/src/defaults.ts +++ b/packages/vitest/src/defaults.ts @@ -62,6 +62,7 @@ export const fakeTimersDefaults = { const config = { allowOnly: !isCI, + isolate: true, watch: !isCI, globals: false, environment: 'node' as const, diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index b10f00654c51..674b80dc1d0e 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -123,15 +123,6 @@ export function resolveConfig( } } - if (resolved.isolate) { - resolved.poolOptions ??= {} - resolved.poolOptions.threads ??= {} - // prefer CLI arguments - resolved.poolOptions.threads.isolate = options.poolOptions?.threads?.isolate ?? options.isolate ?? resolved.poolOptions.threads.isolate ?? resolved.isolate - resolved.poolOptions.forks ??= {} - resolved.poolOptions.forks.isolate ??= options.poolOptions?.forks?.isolate ?? options.isolate ?? resolved.poolOptions.forks.isolate ?? resolved.isolate - } - // @ts-expect-error -- check for removed API option if (resolved.coverage.provider === 'c8') throw new Error('"coverage.provider: c8" is not supported anymore. Use "coverage.provider: v8" instead') diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 6ffeabc06572..df0d95783ced 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -51,6 +51,13 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t ) testConfig.api = resolveApiServerConfig(testConfig) + testConfig.poolOptions ??= {} + testConfig.poolOptions.threads ??= {} + testConfig.poolOptions.forks ??= {} + // prefer --poolOptions.{name}.isolate CLI arguments over --isolate, but still respect config value + testConfig.poolOptions.threads.isolate = options.poolOptions?.threads?.isolate ?? options.isolate ?? testConfig.poolOptions.threads.isolate ?? viteConfig.test?.isolate + testConfig.poolOptions.forks.isolate = options.poolOptions?.forks?.isolate ?? options.isolate ?? testConfig.poolOptions.forks.isolate ?? viteConfig.test?.isolate + // store defines for globalThis to make them // reassignable when running in worker in src/runtime/setup.ts const defines: Record = deleteDefineConfig(viteConfig) @@ -91,6 +98,9 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t allow: resolveFsAllow(getRoot(), testConfig.config), }, }, + test: { + poolOptions: testConfig.poolOptions, + }, } // chokidar fsevents is unstable on macos when emitting "ready" event diff --git a/test/config/test/resolution.test.ts b/test/config/test/resolution.test.ts new file mode 100644 index 000000000000..a222e7812692 --- /dev/null +++ b/test/config/test/resolution.test.ts @@ -0,0 +1,104 @@ +import type { UserConfig } from 'vitest' +import { describe, expect, it } from 'vitest' +import { createVitest } from 'vitest/node' + +async function config(cliOptions: UserConfig, configValue: UserConfig = {}) { + const vitest = await createVitest('test', { ...cliOptions, watch: false }, { test: configValue }) + return vitest.config +} + +describe('correctly defines isolated flags', async () => { + it('prefers CLI poolOptions flags over config', async () => { + const c = await config({ + isolate: true, + poolOptions: { + threads: { + isolate: false, + }, + forks: { + isolate: false, + }, + }, + }) + expect(c.poolOptions?.threads?.isolate).toBe(false) + expect(c.poolOptions?.forks?.isolate).toBe(false) + expect(c.isolate).toBe(true) + }) + + it('override CLI poolOptions flags over isolate', async () => { + const c = await config({ + isolate: false, + poolOptions: { + threads: { + isolate: true, + }, + forks: { + isolate: true, + }, + }, + }, { + poolOptions: { + threads: { + isolate: false, + }, + forks: { + isolate: false, + }, + }, + }) + expect(c.poolOptions?.threads?.isolate).toBe(true) + expect(c.poolOptions?.forks?.isolate).toBe(true) + expect(c.isolate).toBe(false) + }) + + it('override CLI isolate flag if poolOptions is not set via CLI', async () => { + const c = await config({ + isolate: true, + }, { + poolOptions: { + threads: { + isolate: false, + }, + forks: { + isolate: false, + }, + }, + }) + expect(c.poolOptions?.threads?.isolate).toBe(true) + expect(c.poolOptions?.forks?.isolate).toBe(true) + expect(c.isolate).toBe(true) + }) + + it('keeps user configured poolOptions if no CLI flag is provided', async () => { + const c = await config({}, { + poolOptions: { + threads: { + isolate: false, + }, + forks: { + isolate: false, + }, + }, + }) + expect(c.poolOptions?.threads?.isolate).toBe(false) + expect(c.poolOptions?.forks?.isolate).toBe(false) + expect(c.isolate).toBe(true) + }) + + it('isolate config value overrides poolOptions defaults', async () => { + const c = await config({}, { + isolate: false, + }) + expect(c.poolOptions?.threads?.isolate).toBe(false) + expect(c.poolOptions?.forks?.isolate).toBe(false) + expect(c.isolate).toBe(false) + }) + + it('if no isolation is defined in the config, fallback ot undefined', async () => { + const c = await config({}, {}) + expect(c.poolOptions?.threads?.isolate).toBe(undefined) + expect(c.poolOptions?.forks?.isolate).toBe(undefined) + // set in configDefaults, so it's always defined + expect(c.isolate).toBe(true) + }) +})