You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用tRPC createCaller时Vitest测试失败:需配置assetsInclude

Vitest + tRPC单元测试报错:HTML文件解析失败的解决办法

问题场景

使用Vitest编写单元测试,通过调用tRPC的createCaller模拟trpc上下文时,运行测试抛出以下错误:

Error: Failed to parse source for import analysis because the content contains invalid JS syntax. You may need to install appropriate plugins to handle the .html file format, or if it's an asset, add "**/*.html" to `assetsInclude` in your configuration.

解决办法

有两种可行方案:

  • 在vitest.config.js中添加assetsInclude: ['**/*.html']配置,告知Vitest将HTML文件视为静态资源处理
  • 移除项目中被测试代码所引入的HTML资源文件

配置示例(添加assetsInclude)

修改后的vitest.config.js如下:

export default defineConfig({
  plugins: [react],
  resolve: {
    alias,
  },
  // 添加HTML资源识别配置
  assetsInclude: ['**/*.html'],
  
  test: {
    globals: true,
    exclude: [...configDefaults.exclude, '/.next/'],
    setupFiles: './tests/setup.ts',
  },
})

内容的提问来源于stack exchange,提问作者Yaman Ktish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 08:52:03