diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 06eb1d2037da..29a2b1d467f0 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -103,7 +103,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t isolate: options.poolOptions?.threads?.isolate ?? options.isolate ?? testConfig.poolOptions?.threads?.isolate ?? viteConfig.test?.isolate, }, forks: { - isolate: options.poolOptions?.threads?.isolate ?? options.isolate ?? testConfig.poolOptions?.threads?.isolate ?? viteConfig.test?.isolate, + isolate: options.poolOptions?.forks?.isolate ?? options.isolate ?? testConfig.poolOptions?.forks?.isolate ?? viteConfig.test?.isolate, }, }, }, diff --git a/test/config/fixtures/pool-isolation/isolated.test.ts b/test/config/fixtures/pool-isolation/isolated.test.ts new file mode 100644 index 000000000000..c0a0ab9148b6 --- /dev/null +++ b/test/config/fixtures/pool-isolation/isolated.test.ts @@ -0,0 +1,17 @@ +import { expect, test } from 'vitest' +import type { UserConfig } from 'vitest/config' + +const pool = process.env.TESTED_POOL as "forks" | "threads"; + +test('is isolated', () => { + // @ts-expect-error -- internal + const config: NonNullable = globalThis.__vitest_worker__.config + + if (pool === 'forks') { + expect(config.poolOptions?.forks?.isolate).toBe(true) + } + else { + expect(pool).toBe('threads') + expect(config.poolOptions?.threads?.isolate).toBe(true) + } +}) diff --git a/test/config/fixtures/pool-isolation/non-isolated.test.ts b/test/config/fixtures/pool-isolation/non-isolated.test.ts new file mode 100644 index 000000000000..3a6471b9e657 --- /dev/null +++ b/test/config/fixtures/pool-isolation/non-isolated.test.ts @@ -0,0 +1,17 @@ +import { expect, test } from 'vitest' +import type { UserConfig } from 'vitest/config' + +const pool = process.env.TESTED_POOL as "forks" | "threads"; + +test('is isolated', () => { + // @ts-expect-error -- internal + const config: NonNullable = globalThis.__vitest_worker__.config + + if (pool === 'forks') { + expect(config.poolOptions?.forks?.isolate).toBe(false) + } + else { + expect(pool).toBe('threads') + expect(config.poolOptions?.threads?.isolate).toBe(false) + } +}) diff --git a/test/config/test/pool-isolation.test.ts b/test/config/test/pool-isolation.test.ts new file mode 100644 index 000000000000..1d72f4d9e91c --- /dev/null +++ b/test/config/test/pool-isolation.test.ts @@ -0,0 +1,72 @@ +import { describe, expect, test } from 'vitest' +import { runVitest } from '../../test-utils' + +describe.each(['forks', 'threads'] as const)('%s', async (pool) => { + test('is isolated', async () => { + const { stderr, exitCode } = await runVitest({ + root: './fixtures/pool-isolation', + include: ['isolated.test.ts'], + pool, + poolOptions: { + // Use default value on the tested pool and disable on the other one + [invertPool(pool)]: { isolate: false }, + }, + env: { TESTED_POOL: pool }, + }) + + expect(stderr).toBe('') + expect(exitCode).toBe(0) + }) + + test('is isolated + poolMatchGlobs', async () => { + const { stderr, exitCode } = await runVitest({ + root: './fixtures/pool-isolation', + include: ['isolated.test.ts'], + pool, + poolMatchGlobs: [['**', pool]], + poolOptions: { + // Use default value on the tested pool and disable on the other one + [invertPool(pool)]: { isolate: false }, + }, + env: { TESTED_POOL: pool }, + }) + + expect(stderr).toBe('') + expect(exitCode).toBe(0) + }) + + test('is not isolated', async () => { + const { stderr, exitCode } = await runVitest({ + root: './fixtures/pool-isolation', + include: ['non-isolated.test.ts'], + pool, + poolOptions: { + [pool]: { isolate: false }, + }, + env: { TESTED_POOL: pool }, + }) + + expect(stderr).toBe('') + expect(exitCode).toBe(0) + }) + + test('is not isolated + poolMatchGlobs', async () => { + const { stderr, exitCode } = await runVitest({ + root: './fixtures/pool-isolation', + include: ['non-isolated.test.ts'], + pool: invertPool(pool), + poolMatchGlobs: [['**/**.test.ts', pool]], + poolOptions: { + [pool]: { isolate: false }, + }, + env: { TESTED_POOL: pool }, + }) + + expect(stderr).toBe('') + expect(exitCode).toBe(0) + }) +}) + +function invertPool(pool: 'threads' | 'forks') { + return pool === 'threads' ? 'forks' : 'threads' +}