开玩笑 你的测试套件必须包含至少一个测试

dev*_*_el 3 reactjs jestjs next.js

我有一个简单的测试文件./pages/test.js

\n
import React from 'react'\n\nexport default function HomePage () {\n  return (\n    <main>\n      <h1>Testing Next.js With Jest and React Testing Library</h1>\n    </main>\n  )\n}\n
Run Code Online (Sandbox Code Playgroud)\n

./test/pages/index.test.js做了以下简单的测试来检查我的页面是否正确呈现以及是否有标题

\n
import React from 'react'\n// Using render and screen from test-utils.js instead of\n// @testing-library/react\nimport { render, screen } from '../test-utils'\nimport HomePage from '../../pages/test'\n\ndescribe('HomePage', () => {\n  it('should render the heading', () => {\n    render(<HomePage />)\n\n    const heading = screen.getByText('Testing Next.js With Jest and React Testing Library')\n\n    // we can only use toBeInTheDocument because it was imported\n    // in the jest.setup.js and configured in jest.config.js\n    expect(heading).toBeInTheDocument()\n  })\n})\n
Run Code Online (Sandbox Code Playgroud)\n

运行测试后,出现以下错误

\n
 FAIL  pages/test.js\n  \xe2\x97\x8f Test suite failed to run\n\n    Your test suite must contain at least one test.\n\n      at onResult (node_modules/@jest/core/build/TestScheduler.js:175:18)\n      at node_modules/@jest/core/build/TestScheduler.js:304:17\n      at node_modules/emittery/index.js:260:13\n          at Array.map (<anonymous>)\n      at Emittery.Typed.emit (node_modules/emittery/index.js:258:23)\n\n PASS  test/pages/index.test.js\n\nTest Suites: 1 failed, 1 passed, 2 total\nTests:       1 passed, 1 total\n
Run Code Online (Sandbox Code Playgroud)\n

为什么笑话说我错过了考试?

\n

Aru*_*han 5

为什么笑话说我错过了考试?

因为Jest认为pages/test.js是一个测试文件。Jest 使用以下正则表达式来检测测试文件。

(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$
Run Code Online (Sandbox Code Playgroud)

文档中,

默认情况下,它会查找文件夹内的.js.jsx和文件,以及带有或后缀(例如或)的任何文件。它还会查找名为或 的文件。.ts.tsx__tests__.test.specComponent.test.jsComponent.spec.jstest.jsspec.js

一个简单的解决方案是重命名该文件。