Skip to content

Commit

Permalink
fix: listen setup files to force rerun.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Jan 10, 2023
1 parent 532cc11 commit d8a68e9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/config/index.md
Expand Up @@ -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`).

Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/node/config.ts
Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions test/setup/package.json
@@ -0,0 +1,11 @@
{
"name": "@vitest/test-setup",
"private": true,
"scripts": {
"test": "vitest"
},
"devDependencies": {
"execa": "^6.1.0",
"vitest": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions 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'],
},
})
Empty file.
5 changes: 5 additions & 0 deletions 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)
})
25 changes: 25 additions & 0 deletions 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)
})

0 comments on commit d8a68e9

Please sign in to comment.