Skip to content

Commit

Permalink
feat(dev): add modulePatterns option to the vitest config
Browse files Browse the repository at this point in the history
  • Loading branch information
fooddilsn committed May 9, 2023
1 parent fde628a commit e532f1f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
17 changes: 17 additions & 0 deletions docs/config/index.md
Expand Up @@ -1410,3 +1410,20 @@ This config option affects truncating values in `test.each` titles and inside th
Stop test execution when given number of tests have failed.

By default Vitest will run all of your test cases even if some of them fail. This may not be desired for CI builds where you are only interested in 100% successful builds and would like to stop test execution as early as possible when test failures occur. The `bail` option can be used to speed up CI runs by preventing it from running more tests when failures have occured.

### modulePatterns

- **Type**: `string[]`
- **Default**: `['**/node_modules/**']`

Glob pattern for file paths that should be treated as modules. Setting this option will override the default, if you wish to still search `**/node_modules/**` for packages include it along with any other options:

```ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
modulePatterns: ['**/node_modules/**', path.resolve('../../packages/**')],
},
})
```
1 change: 1 addition & 0 deletions packages/vitest/src/defaults.ts
Expand Up @@ -94,6 +94,7 @@ const config = {
exclude: defaultExclude,
},
slowTestThreshold: 300,
modulePatterns: ['**/node_modules/**'],
}

export const configDefaults: Required<Pick<UserConfig, keyof typeof config>> = Object.freeze(config)
2 changes: 2 additions & 0 deletions packages/vitest/src/runtime/execute.ts
Expand Up @@ -13,6 +13,7 @@ import { rpc } from './rpc'

export interface ExecuteOptions extends ViteNodeRunnerOptions {
mockMap: MockMap
modulePatterns: string[]
}

export async function createVitestExecutor(options: ExecuteOptions) {
Expand Down Expand Up @@ -70,6 +71,7 @@ export async function startViteNode(ctx: ContextRPC) {
moduleCache,
mockMap,
interopDefault: config.deps.interopDefault,
modulePatterns: config.modulePatterns,
root: config.root,
base: config.base,
})
Expand Down
7 changes: 6 additions & 1 deletion packages/vitest/src/runtime/mocker.ts
Expand Up @@ -2,6 +2,7 @@ import { existsSync, readdirSync } from 'node:fs'
import { basename, dirname, extname, isAbsolute, join, resolve } from 'pathe'
import { getColors, getType } from '@vitest/utils'
import { isNodeBuiltin } from 'vite-node/utils'
import mm from 'micromatch'
import { getWorkerState } from '../utils/global'
import { getAllMockableProperties } from '../utils/base'
import { spyOn } from '../integrations/spy'
Expand Down Expand Up @@ -58,6 +59,10 @@ export class VitestMocker {
return this.executor.moduleCache
}

private get modulePatterns() {
return this.executor.options.modulePatterns
}

private deleteCachedItem(id: string) {
const mockId = this.getMockPath(id)
if (this.moduleCache.has(mockId))
Expand All @@ -83,7 +88,7 @@ export class VitestMocker {
const [id, fsPath] = await this.executor.resolveUrl(rawId, importer)
// external is node_module or unresolved module
// for example, some people mock "vscode" and don't have it installed
const external = (!isAbsolute(fsPath) || fsPath.includes('/node_modules/')) ? rawId : null
const external = (!isAbsolute(fsPath) || mm.isMatch(fsPath, this.modulePatterns, { dot: true })) ? rawId : null // { dot: true } is needed to match dotfolder like .pnpm

return {
id,
Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/src/types/config.ts
Expand Up @@ -580,6 +580,13 @@ export interface InlineConfig {
* Stop test execution when given number of tests have failed.
*/
bail?: number

/**
* Glob pattern for file paths that should be treated as modules.
*
* @default ['**\/node_modules\/**']
*/
modulePatterns?: string[]
}

export interface TypecheckConfig {
Expand Down

0 comments on commit e532f1f

Please sign in to comment.