From ad41d3e649a2402b0ce092695baae07c8296194e Mon Sep 17 00:00:00 2001 From: Adrien Cacciaguerra Date: Fri, 27 Oct 2023 18:53:23 +0200 Subject: [PATCH] test(vitest): add tests on exec args --- pnpm-lock.yaml | 3 ++ test/run/exec-args-fixtures/forks.test.ts | 14 +++++++++ test/run/exec-args-fixtures/threads.test.ts | 12 ++++++++ test/run/exec-args-fixtures/vmThreads.test.ts | 16 ++++++++++ .../no-exec-argv.test.ts | 12 ++++++++ .../no-exec-args-fixtures/vitest.config.ts | 7 +++++ test/run/package.json | 1 + test/run/test/exec-args.test.ts | 30 +++++++++++++++++++ 8 files changed, 95 insertions(+) create mode 100644 test/run/exec-args-fixtures/forks.test.ts create mode 100644 test/run/exec-args-fixtures/threads.test.ts create mode 100644 test/run/exec-args-fixtures/vmThreads.test.ts create mode 100644 test/run/no-exec-args-fixtures/no-exec-argv.test.ts create mode 100644 test/run/no-exec-args-fixtures/vitest.config.ts create mode 100644 test/run/test/exec-args.test.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a3b57f94d4f5..f638c4220009 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1889,6 +1889,9 @@ importers: test/run: devDependencies: + execa: + specifier: ^7.1.1 + version: 7.1.1 vite: specifier: ^4.5.0 version: 4.5.0(@types/node@18.16.19)(less@4.1.3) diff --git a/test/run/exec-args-fixtures/forks.test.ts b/test/run/exec-args-fixtures/forks.test.ts new file mode 100644 index 000000000000..bf265bbe49a5 --- /dev/null +++ b/test/run/exec-args-fixtures/forks.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from 'vitest' + +describe('exec-args', async () => { + it('should have the correct flags', () => { + // passed via poolOptions.forks.execArgv in vitest.config.ts + expect(process.execArgv).toContain('--hash-seed=1') + expect(process.execArgv).toContain('--random-seed=1') + expect(process.execArgv).toContain('--no-opt') + + // added via vitest + expect(process.execArgv).toContain('--conditions') + expect(process.execArgv).toContain('node') + }) +}) diff --git a/test/run/exec-args-fixtures/threads.test.ts b/test/run/exec-args-fixtures/threads.test.ts new file mode 100644 index 000000000000..731636e34fbf --- /dev/null +++ b/test/run/exec-args-fixtures/threads.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from 'vitest' + +describe('exec-args', async () => { + it('should have the correct flags', () => { + // passed via poolOptions.forks.execArgv in vitest.config.ts + expect(process.execArgv).toContain('--inspect-brk') + + // added via vitest + expect(process.execArgv).toContain('--conditions') + expect(process.execArgv).toContain('node') + }) +}) diff --git a/test/run/exec-args-fixtures/vmThreads.test.ts b/test/run/exec-args-fixtures/vmThreads.test.ts new file mode 100644 index 000000000000..cff9e3440e07 --- /dev/null +++ b/test/run/exec-args-fixtures/vmThreads.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from 'vitest' + +describe('exec-args', async () => { + it('should have the correct flags', () => { + // passed via poolOptions.vmThreads.execArgv in vitest.config.ts + expect(process.execArgv).toContain('--inspect-brk') + + // added via vitest + expect(process.execArgv).toContain('--experimental-import-meta-resolve') + expect(process.execArgv).toContain('--experimental-vm-modules') + expect(process.execArgv).toContain('--require') + expect(process.execArgv).toContainEqual(expect.stringContaining('/packages/vitest/suppress-warnings.cjs')) + expect(process.execArgv).toContain('--conditions') + expect(process.execArgv).toContain('node') + }) +}) diff --git a/test/run/no-exec-args-fixtures/no-exec-argv.test.ts b/test/run/no-exec-args-fixtures/no-exec-argv.test.ts new file mode 100644 index 000000000000..010f45e87ef6 --- /dev/null +++ b/test/run/no-exec-args-fixtures/no-exec-argv.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from 'vitest' + +describe('exec-args', async () => { + it('should have the correct flags', () => { + // flags should not be passed + expect(process.execArgv).not.toContain('--inspect-brk') + + // added via vitest + expect(process.execArgv).toContain('--conditions') + expect(process.execArgv).toContain('node') + }) +}) diff --git a/test/run/no-exec-args-fixtures/vitest.config.ts b/test/run/no-exec-args-fixtures/vitest.config.ts new file mode 100644 index 000000000000..c0205689c1f4 --- /dev/null +++ b/test/run/no-exec-args-fixtures/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + pool: 'threads', + }, +}) diff --git a/test/run/package.json b/test/run/package.json index 5bc48ffdaa9d..1ffa3012ba94 100644 --- a/test/run/package.json +++ b/test/run/package.json @@ -5,6 +5,7 @@ "test": "vitest" }, "devDependencies": { + "execa": "^7.1.1", "vite": "latest", "vitest": "workspace:*" } diff --git a/test/run/test/exec-args.test.ts b/test/run/test/exec-args.test.ts new file mode 100644 index 000000000000..651ee492794f --- /dev/null +++ b/test/run/test/exec-args.test.ts @@ -0,0 +1,30 @@ +import { expect, test } from 'vitest' +import { execa } from 'execa' +import { runVitest } from '../../test-utils' + +test.each([ + { pool: 'forks', execArgv: ['--hash-seed=1', '--random-seed=1', '--no-opt'] }, + { pool: 'threads', execArgv: ['--inspect-brk'] }, + { pool: 'vmThreads', execArgv: ['--inspect-brk'] }, +] as const)('should pass execArgv to { pool: $pool } ', async ({ pool, execArgv }) => { + const fileToTest = `exec-args-fixtures/${pool}.test.ts` + + const vitest = await runVitest({ + include: [fileToTest], + pool, + poolOptions: { + [pool]: { + execArgv, + }, + }, + }) + + expect(vitest.stdout).toContain(`✓ ${fileToTest}`) +}) + +test('should not pass execArgv to workers when not specified in the config', async () => { + const { stdout } = await execa('npx', ['vitest', '--run', '--root', 'no-exec-args-fixtures', '--config', 'vitest.config.ts'], + { env: { NODE_OPTIONS: '--inspect-brk' } }) + + expect(stdout).toContain('✓ no-exec-argv.test.ts') +})