如何为 Nuxt3 设置单元测试?

问题描述 投票:0回答:0

我通过

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 调用测试?
javascript vue.js nuxt.js vitest
© www.soinside.com 2019 - 2024. All rights reserved.