Skip to content

Commit 019a6d5

Browse files
authoredJan 16, 2023
fix: rerun tests, when setup file is edited (#2625)
Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com> fix #2614
1 parent 03af706 commit 019a6d5

File tree

8 files changed

+66
-0
lines changed

8 files changed

+66
-0
lines changed
 

‎docs/config/index.md

+4
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ Silent console output from tests
491491

492492
Path to setup files. They will be run before each test file.
493493

494+
:::info
495+
Changing setup files will trigger rerun of all tests.
496+
:::
497+
494498
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`).
495499

496500
:::tip

‎packages/vitest/src/node/config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ export function resolveConfig(
191191
),
192192
)
193193

194+
resolved.forceRerunTriggers = [
195+
...resolved.forceRerunTriggers,
196+
...resolved.setupFiles,
197+
]
198+
194199
// the server has been created, we don't need to override vite.server options
195200
resolved.api = resolveApiConfig(options)
196201

‎pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/setup/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@vitest/test-setup",
3+
"private": true,
4+
"scripts": {
5+
"test": "vitest"
6+
},
7+
"devDependencies": {
8+
"execa": "^6.1.0",
9+
"vitest": "workspace:*"
10+
}
11+
}

‎test/setup/setup.vitest.config.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['tests/empty-setup.test.ts'],
6+
setupFiles: ['setupFiles/empty-setup.ts'],
7+
},
8+
})

‎test/setup/setupFiles/empty-setup.ts

Whitespace-only changes.

‎test/setup/tests/empty-setup.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, it } from 'vitest'
2+
3+
it('should success', async () => {
4+
expect(1 + 1).toBe(2)
5+
})

‎test/setup/tests/setup-files.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { promises as fs } from 'fs'
2+
import { afterEach, describe, expect, it } from 'vitest'
3+
import { execa } from 'execa'
4+
5+
const run = async () => await execa('vitest', ['run', '--changed', '--config', 'setup.vitest.config.ts'])
6+
7+
describe('setup files with forceRerunTrigger', () => {
8+
const file = './setupFiles/empty-setup.ts'
9+
10+
afterEach(async () => {
11+
await fs.writeFile(file, '', 'utf-8')
12+
})
13+
14+
it('should run no tests if setup file is not changed', async () => {
15+
const { stdout } = await run()
16+
expect(stdout).toContain('No test files found, exiting with code 0')
17+
}, 60_000)
18+
19+
it('should run the whole test suite if setup file is changed', async () => {
20+
const codes = 'export const a = 1'
21+
await fs.writeFile(file, codes, 'utf-8')
22+
const { stdout } = await run()
23+
expect(stdout).toContain('1 passed')
24+
}, 60_000)
25+
})

0 commit comments

Comments
 (0)
Please sign in to comment.