2.8 KiB
| title |
|---|
| includeSource | Config |
includeSource
- Type:
string[] - Default:
[]
A list of glob patterns that match your in-source test files. These patterns are resolved relative to the root (process.cwd() by default).
When defined, Vitest will run all matched files that have import.meta.vitest inside.
::: warning
Vitest performs a simple text-based inclusion check on source files. If a file contains import.meta.vitest, even in a comment, it will be matched as an in-source test file.
:::
Vitest uses the tinyglobby package to resolve the globs.
Example
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
includeSource: ['src/**/*.{js,ts}'],
},
})
Then you can write tests inside your source files:
export function add(...args: number[]) {
return args.reduce((a, b) => a + b, 0)
}
// #region in-source test suites
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest
it('add', () => {
expect(add()).toBe(0)
expect(add(1)).toBe(1)
expect(add(1, 2, 3)).toBe(6)
})
}
// #endregion
For your production build, you need to replace the import.meta.vitest with undefined, letting the bundler do the dead code elimination.
::: code-group
import { defineConfig } from 'vite'
export default defineConfig({
define: { // [!code ++]
'import.meta.vitest': 'undefined', // [!code ++]
}, // [!code ++]
})
import { defineConfig } from 'rolldown/config'
export default defineConfig({
transform: {
define: { // [!code ++]
'import.meta.vitest': 'undefined', // [!code ++]
}, // [!code ++]
},
})
import replace from '@rollup/plugin-replace' // [!code ++]
export default {
plugins: [
replace({ // [!code ++]
'import.meta.vitest': 'undefined', // [!code ++]
}) // [!code ++]
],
// other options
}
import { defineBuildConfig } from 'unbuild'
export default defineBuildConfig({
replace: { // [!code ++]
'import.meta.vitest': 'undefined', // [!code ++]
}, // [!code ++]
// other options
})
const webpack = require('webpack')
module.exports = {
plugins: [
new webpack.DefinePlugin({ // [!code ++]
'import.meta.vitest': 'undefined', // [!code ++]
})// [!code ++]
],
}
:::
::: tip
To get TypeScript support for import.meta.vitest, add vitest/importMeta to your tsconfig.json:
{
"compilerOptions": {
"types": ["vitest/importMeta"]
}
}
:::