Skip to content

Commit

Permalink
feat: check related against forceRerunTriggers (#1595)
Browse files Browse the repository at this point in the history
* feat: check related against forceRerunTriggers

* chore: fix lock

* check length of micromatch

* add execa

* add force-rerun tests and vitest config

* update docs

* watch false on related

* typo

* linting

* fix lock and add timeout to tests

* add defaults to forceRerunTriggers

* chore: remove dist from forceRerunTriggers

* Wording

Co-authored-by: Vladimir Sheremet <sheremet.va@icloud.com>
  • Loading branch information
elliotwestlake and sheremet-va committed Jul 5, 2022
1 parent 0a642c6 commit 40fc526
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/config/index.md
Expand Up @@ -372,9 +372,9 @@ Glob pattern of file paths to be ignored from triggering watch rerun.
### forceRerunTriggers

- **Type**: `string[]`
- **Default:** `[]`
- **Default:** `['**/package.json/**', '**/vitest.config.*/**', '**/vite.config.*/**']`

Glob patter of file paths that will trigger the whole suite rerun.
Glob pattern of file paths that will trigger the whole suite rerun. When 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
6 changes: 5 additions & 1 deletion packages/vitest/src/defaults.ts
Expand Up @@ -62,7 +62,11 @@ const config = {
hookTimeout: 10000,
isolate: true,
watchExclude: ['**/node_modules/**', '**/dist/**'],
forceRerunTriggers: [],
forceRerunTriggers: [
'**/package.json/**',
'**/vitest.config.*/**',
'**/vite.config.*/**',
],
update: false,
reporters: [],
silent: false,
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
2 changes: 2 additions & 0 deletions 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 related src/sourceA.ts --globals --watch=false",
"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, '', () => {})
const { stdout } = await run()
expect(stdout).toContain('1 passed')
}, 60_000)

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')
}, 60_000)
})

0 comments on commit 40fc526

Please sign in to comment.