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: improve watcher performance, add forceRerunTriggers option #1424

Merged
merged 5 commits into from Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions docs/config/index.md
Expand Up @@ -332,12 +332,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.

### dumbWatchInclude

- **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/**'],
dumbWatchInclude: [],
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('ignore: ') + c.yellow(this.config.watchExclude.join(comma)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.console.error(c.dim('ignore: ') + c.yellow(this.config.watchExclude.join(comma)))
this.console.error(c.dim('exclude: ') + c.yellow(this.config.watchExclude.join(comma)))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think exclude is a bit misleading. Renamed to watch exclude


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.dumbWatchInclude.length)
watcher.add(this.config.dumbWatchInclude)

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.dumbWatchInclude)) {
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)[]
dumbWatchInclude?: string[]
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

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