Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rerun tests, when setup file is edited #2625

Merged
merged 7 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ Silent console output from tests

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

:::info
Changing setup files will trigger rerun of all tests.
:::

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`).

:::tip
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
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
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/setup/package.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 file is 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 file is 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)
})