diff --git a/docs/config/index.md b/docs/config/index.md index d77d3c911219..3551904905f1 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -481,7 +481,7 @@ Silent console output from tests - **Type:** `string | string[]` -Path to setup files. They will be run before each test file. +Path to setup files. They will be run before each test file. They will be merged into [`forceRerunTriggers`](https://vitest.dev/config/#forcereruntriggers), all your tests will be rerun when setup files are changed. You can use `process.env.VITEST_POOL_ID` (integer-like string) inside to distinguish between threads (will always be `'1'`, if run with `threads: false`). diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index 882299efd112..0d7b4f24f9ec 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -191,6 +191,11 @@ export function resolveConfig( ), ) + resolved.forceRerunTriggers = [ + ...resolved.forceRerunTriggers, + ...resolved.setupFiles, + ] + // the server has been created, we don't need to override vite.server options resolved.api = resolveApiConfig(options) diff --git a/test/setup/package.json b/test/setup/package.json new file mode 100644 index 000000000000..8f59d466bbb7 --- /dev/null +++ b/test/setup/package.json @@ -0,0 +1,11 @@ +{ + "name": "@vitest/test-setup", + "private": true, + "scripts": { + "test": "vitest" + }, + "devDependencies": { + "execa": "^6.1.0", + "vitest": "workspace:*" + } +} diff --git a/test/setup/setup.vitest.config.ts b/test/setup/setup.vitest.config.ts new file mode 100644 index 000000000000..84c7a1e6f7ac --- /dev/null +++ b/test/setup/setup.vitest.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + include: ['tests/empty-setup.test.ts'], + setupFiles: ['setupFiles/empty-setup.ts'], + }, +}) diff --git a/test/setup/setupFiles/empty-setup.ts b/test/setup/setupFiles/empty-setup.ts new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test/setup/tests/empty-setup.test.ts b/test/setup/tests/empty-setup.test.ts new file mode 100644 index 000000000000..2621a8919ec2 --- /dev/null +++ b/test/setup/tests/empty-setup.test.ts @@ -0,0 +1,5 @@ +import { expect, it } from 'vitest' + +it('should success', async () => { + expect(1 + 1).toBe(2) +}) diff --git a/test/setup/tests/setup-files.test.ts b/test/setup/tests/setup-files.test.ts new file mode 100644 index 000000000000..e580812e1d9e --- /dev/null +++ b/test/setup/tests/setup-files.test.ts @@ -0,0 +1,25 @@ +import { promises as fs } from 'fs' +import { afterEach, describe, expect, it } from 'vitest' +import { execa } from 'execa' + +const run = async () => await execa('vitest', ['run', '--changed', '--config', 'setup.vitest.config.ts']) + +describe('setup files with forceRerunTrigger', () => { + const file = './setupFiles/empty-setup.ts' + + afterEach(async () => { + await fs.writeFile(file, '', 'utf-8') + }) + + it('should run no tests if setup files are not changed', async () => { + const { stdout } = await run() + expect(stdout).toContain('No test files found, exiting with code 0') + }, 60_000) + + it('should run the whole test suite if setup files are changed', async () => { + const codes = 'export const a = 1' + await fs.writeFile(file, codes, 'utf-8') + const { stdout } = await run() + expect(stdout).toContain('1 passed') + }, 60_000) +})