常见错误
无法找到模块 './relative-path'
如果您遇到模块无法找到的错误,这可能有以下几种原因:
- 您拼写了错误的路径。请确保路径正确。
- 您可能在
tsconfig.json
中配置了baseUrl
。Vite 默认不会考虑tsconfig.json
的设置,因此如果您依赖此行为,可能需要自行安装vite-tsconfig-paths
。
- 您可能在
ts
import { defineConfig } from 'vitest/config';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [tsconfigPaths()],
});
或者将路径重写为非根目录相对路径:
diff
- import helpers from 'src/helpers'
+ import helpers from '../src/helpers'
- 确保您没有使用相对路径的别名。Vite 会将它们视为相对于导入文件而非项目根目录的路径。
ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
alias: {
'@/': './src/',
'@/': new URL('./src/', import.meta.url).pathname,
},
},
});
无法终止 worker
当 NodeJS 的 fetch
与默认的 pool: 'threads'
一起使用时,可能会发生此错误。此问题已在 Timeout abort can leave process(es) running in the background #3077 中进行跟踪。
作为变通方案,您可以切换到 pool: 'forks'
或 pool: 'vmForks'
。
ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
pool: 'forks',
},
});
bash
vitest --pool=forks
段错误和原生代码错误
在 pool: 'threads'
模式下运行 原生 NodeJS 模块 可能会遇到来自原生代码的难以理解的错误。
Segmentation fault (core dumped)
thread '<unnamed>' panicked at 'assertion failed
Abort trap: 6
internal error: entered unreachable code
在这些情况下,原生模块很可能不具备多线程安全性。作为变通方案,您可以切换到 pool: 'forks'
,它会在多个 node:child_process
中运行测试用例,而非 node:worker_threads
。
ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
pool: 'forks',
},
});
bash
vitest --pool=forks