feat: check related against forceRerunTriggers (#1595)

* 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>
This commit is contained in:
Elliot Westlake 2022-07-05 09:04:09 +01:00 committed by GitHub
parent 0a642c6e57
commit 40fc526e55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 4 deletions

View File

@ -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:

View File

@ -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`

View File

@ -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,

View File

@ -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 []

2
pnpm-lock.yaml generated
View File

@ -848,8 +848,10 @@ importers:
test/related:
specifiers:
execa: ^6.1.0
vitest: workspace:*
devDependencies:
execa: 6.1.0
vitest: link:../../packages/vitest
test/reporters:

View File

@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ['tests/related.test.ts'],
forceRerunTriggers: ['**/rerun.temp/**'],
},
})

View File

@ -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:*"
}
}

View File

@ -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)
})