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

Nuxt3单元测试配置问题:执行测试时报错及相关疑问

问题:Nuxt3添加单元测试时遇到的错误及疑问

背景与操作

我通过npx nuxi init <project-name>创建了Nuxt3新项目,想要添加单元测试,执行了以下操作:

  1. 安装测试依赖:npm install -D @nuxt/test-utils vitest
  2. 在./pages/foo.spec.ts中创建测试文件,代码如下:
import { describe, it, expect } from "vitest";
import { setup } from "@nuxt/test-utils";

describe("Suite", async () => {
  await setup();

  it("passes", () => {
    expect(true).toBeTruthy();
  });
});

执行测试时的错误

执行vitest --run时的错误

TypeError: Cannot read properties of undefined (reading 'on')
 ❯ setupVitest ../../node_modules/@nuxt/test-utils/dist/index.mjs:138:10
    137|     setTestContext(void 0);
    138|   };
    139|   const afterAll = async () => {
       |     ^
    140|     if (ctx.serverProcess) {
    141|       setTestContext(ctx);
 ❯ Module.setup ../../node_modules/@nuxt/test-utils/dist/index.mjs:197:3
 ❯ pages/foo.spec.ts:5:3

执行nuxt test时的错误

Listening http://[::]:38515
[error] ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
[error]
[error]  FAIL  pages/foo.spec.ts [ pages/foo.spec.ts ]
[error] Error: No test suite found in file /home/.../pages/foo.spec.ts

疑问

  • 测试文件应该放在哪里?
  • 如何通过npm scripts执行测试?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 13:05:02