Skip to content

Commit

Permalink
refactor: ensure moduleDirectories always starts and ends with "/"
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored and fooddilsn committed May 25, 2023
1 parent b638210 commit d845d5e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 31 deletions.
41 changes: 22 additions & 19 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,28 @@ TypeError: default is not a function

By default, Vitest assumes you are using a bundler to bypass this and will not fail, but you can disable this behaviour manually, if you code is not processed.


#### deps.moduleDirectories

- **Type:** `(string | RegExp)[]`
- **Default**: `['node_modules']`

A list of directories that should be treated as module directories. This config option affects the behavior of [`vi.mock`](/api/vi#vi-mock): when no factory is provided and the path of what you are mocking matches one of the `moduleDirectories` values, Vitest will try to resolve the mock by looking for a `__mocks__` folder in the [root](/config/#root) of the project.

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: {
deps: {
moduleDirectories: ['node_modules', path.resolve('../../packages')],
}
},
})
```

### runner

- **Type**: `VitestRunnerConstructor`
Expand Down Expand Up @@ -1410,22 +1432,3 @@ 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.

### moduleDirectories

- **Type:** `(string | RegExp)[]`
- **Default**: `['node_modules']`

A list of directories that should be treated as module directories. This config option affects the behavior of [`vi.mock`](/api/vi#vi-mock): when no factory is provided and the path of what you are mocking matches one of the `moduleDirectories` values, Vitest will try to resolve the mock by looking for a `__mocks__` folder in the [root](/config/#root) of the project.

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: {
moduleDirectories: ['node_modules', path.resolve('../../packages')],
},
})
```
1 change: 1 addition & 0 deletions packages/vite-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Awaitable<T> = T | PromiseLike<T>
export interface DepsHandlingOptions {
external?: (string | RegExp)[]
inline?: (string | RegExp)[] | true
moduleDirectories?: (string | RegExp)[]
cacheDir?: string
/**
* Try to guess the CJS version of a package when it's invalid ESM
Expand Down
12 changes: 11 additions & 1 deletion packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function resolveConfig(
...options,
root: viteConfig.root,
mode,
} as ResolvedConfig
} as any as ResolvedConfig

resolved.inspect = Boolean(resolved.inspect)
resolved.inspectBrk = Boolean(resolved.inspectBrk)
Expand Down Expand Up @@ -136,6 +136,16 @@ export function resolveConfig(
resolved.deps.inline.push(...extraInlineDeps)
}
}
resolved.deps.moduleDirectories ??= ['/node_modules/']
resolved.deps.moduleDirectories = resolved.deps.moduleDirectories.map((dir) => {
if (dir instanceof RegExp)
return dir
if (!dir.startsWith('/'))
dir = `/${dir}`
if (!dir.endsWith('/'))
dir += '/'
return dir
})

if (resolved.runner) {
resolved.runner = resolveModule(resolved.runner, { paths: [resolved.root] })
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { rpc } from './rpc'

export interface ExecuteOptions extends ViteNodeRunnerOptions {
mockMap: MockMap
moduleDirectories: (string | RegExp)[]
moduleDirectories?: (string | RegExp)[]
}

export async function createVitestExecutor(options: ExecuteOptions) {
Expand Down Expand Up @@ -71,7 +71,7 @@ export async function startViteNode(ctx: ContextRPC) {
moduleCache,
mockMap,
interopDefault: config.deps.interopDefault,
moduleDirectories: config.moduleDirectories,
moduleDirectories: config.deps.moduleDirectories,
root: config.root,
base: config.base,
})
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/mocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class VitestMocker {
}

private get moduleDirectories() {
return this.executor.options.moduleDirectories
return this.executor.options.moduleDirectories || []
}

private deleteCachedItem(id: string) {
Expand All @@ -70,7 +70,7 @@ export class VitestMocker {

private isAModuleDirectory(path: string) {
return this.moduleDirectories.some(dir =>
dir instanceof RegExp ? dir.test(path) : path.includes(`/${dir.replace(/(^\/?|\/?$)/g, '')}/`),
dir instanceof RegExp ? dir.test(path) : path.includes(dir),
)
}

Expand Down
14 changes: 7 additions & 7 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ interface DepsOptions {
* @default false
*/
registerNodeLoader?: boolean

/**
* A list of directories that should be treated as module directories.
*
* @default ['node_modules']
*/
moduleDirectories?: (string | RegExp)[]
}

export interface InlineConfig {
Expand Down Expand Up @@ -580,13 +587,6 @@ export interface InlineConfig {
* Stop test execution when given number of tests have failed.
*/
bail?: number

/**
* A list of directories that should be treated as module directories.
*
* @default ['node_modules']
*/
moduleDirectories?: (string | RegExp)[]
}

export interface TypecheckConfig {
Expand Down

0 comments on commit d845d5e

Please sign in to comment.