首页 > 解决方案 > 开玩笑 你的测试套件必须至少包含一个测试

问题描述

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

import React from 'react'

export default function HomePage () {
  return (
    <main>
      <h1>Testing Next.js With Jest and React Testing Library</h1>
    </main>
  )
}

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

import React from 'react'
// Using render and screen from test-utils.js instead of
// @testing-library/react
import { render, screen } from '../test-utils'
import HomePage from '../../pages/test'

describe('HomePage', () => {
  it('should render the heading', () => {
    render(<HomePage />)

    const heading = screen.getByText('Testing Next.js With Jest and React Testing Library')

    // we can only use toBeInTheDocument because it was imported
    // in the jest.setup.js and configured in jest.config.js
    expect(heading).toBeInTheDocument()
  })
})

运行测试后,我收到以下错误

 FAIL  pages/test.js
  ● Test suite failed to run

    Your test suite must contain at least one test.

      at onResult (node_modules/@jest/core/build/TestScheduler.js:175:18)
      at node_modules/@jest/core/build/TestScheduler.js:304:17
      at node_modules/emittery/index.js:260:13
          at Array.map (<anonymous>)
      at Emittery.Typed.emit (node_modules/emittery/index.js:258:23)

 PASS  test/pages/index.test.js

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total

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

标签: reactjsjestjsnext.js

解决方案


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

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

(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$

文档中,

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

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


推荐阅读