Nuxt3单元测试配置问题:执行测试时报错及相关疑问
问题:Nuxt3添加单元测试时遇到的错误及疑问
背景与操作
我通过npx nuxi init <project-name>创建了Nuxt3新项目,想要添加单元测试,执行了以下操作:
- 安装测试依赖:
npm install -D @nuxt/test-utils vitest - 在
./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
相关产品推荐
相关产品推荐

