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

feat: check related against forceRerunTriggers #1595

Merged
merged 13 commits into from Jul 5, 2022
2 changes: 1 addition & 1 deletion docs/config/index.md
Expand Up @@ -374,7 +374,7 @@ Glob pattern of file paths to be ignored from triggering watch rerun.
- **Type**: `string[]`
- **Default:** `[]`

Glob patter of file paths that will trigger the whole suite rerun.
Glob patter of file paths that will trigger the whole suite rerun. This paired with the `--changed` argument will run the whole test suite if the trigger is found in the git diff.

Useful if you are testing calling CLI commands, because Vite cannot construct a module graph:

Expand Down
2 changes: 2 additions & 0 deletions docs/guide/cli.md
Expand Up @@ -83,6 +83,8 @@ Clears cache folder.

To run tests against changes made in the last commit, you can use `--changed HEAD~1`. You can also pass commit hash or branch name.

If paired with the `forceRerunTriggers` config option it will run the whole test suite if a match is found.

### shard

- **Type**: `string`
Expand Down
4 changes: 4 additions & 0 deletions packages/vitest/src/node/core.ts
Expand Up @@ -189,6 +189,10 @@ export class Vitest {
if (!related)
return tests

const forceRerunTriggers = this.config.forceRerunTriggers
if (forceRerunTriggers.length && mm(related, forceRerunTriggers).length)
return tests

// don't run anything if no related sources are found
if (!related.length)
return []
Expand Down
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

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

8 changes: 8 additions & 0 deletions test/related/force-rerun.vitest.config.ts
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['tests/related.test.ts'],
forceRerunTriggers: ['**/rerun.temp/**'],
},
})
5 changes: 4 additions & 1 deletion test/related/package.json
Expand Up @@ -2,9 +2,12 @@
"name": "@vitest/test-related",
"private": true,
"scripts": {
"test": "vitest related src/sourceA.ts --globals"
"test": "nr test:related && nr test:rerun",
"test:related": "vitest run related src/sourceA.ts --globals",
"test:rerun": "vitest run rerun"
},
"devDependencies": {
"execa": "^6.1.0",
"vitest": "workspace:*"
}
}
24 changes: 24 additions & 0 deletions test/related/tests/force-rerun.test.ts
@@ -0,0 +1,24 @@
import { unlink, writeFile } from 'fs'
import { beforeEach, describe, expect, it } from 'vitest'
import { execa } from 'execa'

const run = async () => await execa('vitest', ['run', '--changed', '--config', 'force-rerun.vitest.config.ts'])

const fileName = 'rerun.temp'

describe('forceRerunTrigger', () => {
beforeEach(async () => {
unlink(fileName, () => {})
})

it('should run the whole test suite if file exists', async () => {
writeFile(fileName, '', error => console.error(error))
const { stdout } = await run()
expect(stdout).toContain('1 passed')
})
elliotwestlake marked this conversation as resolved.
Show resolved Hide resolved

it('should run no tests if file does not exist', async () => {
const { stdout } = await run()
expect(stdout).toContain('No test files found, exiting with code 0')
})
})