Skip to content

Commit

Permalink
fix: rerun tests, when setup file is edited (#2625)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com>
fix #2614
  • Loading branch information
mysteryven authored Jan 16, 2023
1 parent 03af706 commit 019a6d5
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 0 deletions.
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)
})

0 comments on commit 019a6d5

Please sign in to comment.