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: Prevent merging of poolOptions #5221

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ 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<string, any> = deleteDefineConfig(viteConfig)
Expand Down Expand Up @@ -98,7 +91,14 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
},
},
test: {
poolOptions: testConfig.poolOptions,
poolOptions: {
threads: {
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,
},
},
},
}

Expand Down
16 changes: 16 additions & 0 deletions test/config/test/resolution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ async function config(cliOptions: UserConfig, configValue: UserConfig = {}, vite
}

describe('correctly defines isolated flags', async () => {
it('does not merge user-defined poolOptions with itself', async () => {
const c = await config({}, {
poolOptions: {
array: [1, 2, 3],
},
})
// Ensure poolOptions.array has not been merged with itself
// Previously, this would have been [1,2,3,1,2,3]
expect(c.poolOptions?.array).toMatchInlineSnapshot(`
[
1,
2,
3,
]
`)
})
it('prefers CLI poolOptions flags over config', async () => {
const c = await config({
isolate: true,
Expand Down
1 change: 1 addition & 0 deletions test/run/pool-custom-fixtures/pool/custom-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default (ctx: Vitest): ProcessPool => {
name: 'custom',
async runTests(specs) {
console.warn('[pool] printing:', options.print)
console.warn('[pool] array option', options.array)
for await (const [project, file] of specs) {
ctx.state.clearFiles(project)
const methods = createMethodsRPC(project)
Expand Down
1 change: 1 addition & 0 deletions test/run/pool-custom-fixtures/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default defineConfig({
poolOptions: {
custom: {
print: 'options are respected',
array: [1, 2, 3],
},
},
poolMatchGlobs: [
Expand Down
1 change: 1 addition & 0 deletions test/run/test/custom-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ test('can run custom pools with Vitest', async () => {

expect(vitest.stderr).toMatchInlineSnapshot(`
"[pool] printing: options are respected
[pool] array option [ 1, 2, 3 ]
[pool] running tests for custom-pool-test in /pool-custom-fixtures/tests/custom-not-run.spec.ts
[pool] custom pool is closed!
"
Expand Down