Skip to content

Commit

Permalink
feat: improve watcher performance, add forceRerunTriggers option (#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 5, 2022
1 parent b4a9b0b commit 3f5ff5b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
28 changes: 24 additions & 4 deletions docs/config/index.md
Expand Up @@ -336,12 +336,32 @@ Beware that the global setup is run in a different global scope if you are using
:::


### watchIgnore
### watchExclude

- **Type:** `(string | RegExp)[]`
- **Default:** `[/\/node_modules\//, /\/dist\//]`
- **Type:** `string[]`
- **Default:** `['**/node_modules/**', '**/dist/**']`

Glob pattern of file paths to be ignored from triggering watch rerun.

### forceRerunTriggers

- **Type**: `string[]`
- **Default:** `[]`

Glob patter of file paths that will trigger the whole suite rerun.

Pattern of file paths to be ignored from triggering watch rerun. Glob pattern is not supported.
Useful if you are testing calling CLI commands, because Vite cannot construct a module graph:

```ts
test('execute a script', async () => {
// Vite cannot rerun this test, if content of `dist/index.js` changes
await execa('node', ['dist/index.js'])
})
```

::: tip
Make sure that your files are not excluded by `watchExclude`.
:::

### isolate

Expand Down
3 changes: 2 additions & 1 deletion packages/vitest/src/defaults.ts
Expand Up @@ -61,7 +61,8 @@ const config = {
testTimeout: 5000,
hookTimeout: 10000,
isolate: true,
watchIgnore: [/\/node_modules\//, /\/dist\//],
watchExclude: ['**/node_modules/**', '**/dist/**'],
forceRerunTriggers: [],
update: false,
reporters: [],
silent: false,
Expand Down
17 changes: 14 additions & 3 deletions packages/vitest/src/node/core.ts
Expand Up @@ -122,8 +122,8 @@ export class Vitest {
this.console.error(c.dim('filter: ') + c.yellow(filters.join(comma)))
if (this.config.include)
this.console.error(c.dim('include: ') + c.yellow(this.config.include.join(comma)))
if (this.config.watchIgnore)
this.console.error(c.dim('ignore: ') + c.yellow(this.config.watchIgnore.join(comma)))
if (this.config.watchExclude)
this.console.error(c.dim('watch exclude: ') + c.yellow(this.config.watchExclude.join(comma)))

if (this.config.passWithNoTests)
this.log('No test files found, exiting with code 0\n')
Expand Down Expand Up @@ -364,6 +364,12 @@ export class Vitest {
}
}
const watcher = this.server.watcher

if (this.config.forceRerunTriggers.length)
watcher.add(this.config.forceRerunTriggers)

watcher.unwatch(this.config.watchExclude)

watcher.on('change', onChange)
watcher.on('unlink', onUnlink)
watcher.on('add', onAdd)
Expand All @@ -380,9 +386,14 @@ export class Vitest {
* @returns A value indicating whether rerun is needed (changedTests was mutated)
*/
private handleFileChanged(id: string): boolean {
if (this.changedTests.has(id) || this.invalidates.has(id) || this.config.watchIgnore.some(i => id.match(i)))
if (this.changedTests.has(id) || this.invalidates.has(id))
return false

if (mm.isMatch(id, this.config.forceRerunTriggers)) {
this.state.getFilepaths().forEach(file => this.changedTests.add(file))
return true
}

const mod = this.server.moduleGraph.getModuleById(id)
if (!mod)
return false
Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/node/plugins/index.ts
Expand Up @@ -77,6 +77,9 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest())
},
server: {
...preOptions.api,
watch: {
ignored: preOptions.watchExclude,
},
open,
hmr: false,
preTransformRequests: false,
Expand Down
13 changes: 10 additions & 3 deletions packages/vitest/src/types/config.ts
Expand Up @@ -198,11 +198,18 @@ export interface InlineConfig {
globalSetup?: string | string[]

/**
* Pattern of file paths to be ignore from triggering watch rerun
* Glob pattern of file paths to be ignore from triggering watch rerun
*/
watchExclude?: string[]

/**
* Glob patter of file paths that will trigger the whole suite rerun
*
* @default [/\/node_modules\//, /\/dist\//]
* Useful if you are testing calling CLI commands
*
* @default []
*/
watchIgnore?: (string | RegExp)[]
forceRerunTriggers?: string[]

/**
* Isolate environment for each test file
Expand Down

0 comments on commit 3f5ff5b

Please sign in to comment.