Skip to content

Commit

Permalink
fix: Prevent merging of poolOptions (#5221)
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa authored Feb 16, 2024
1 parent df6182d commit bc5b2d0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
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

0 comments on commit bc5b2d0

Please sign in to comment.