reactjs - 开玩笑 你的测试套件必须至少包含一个测试
问题描述
我有一个简单的测试文件./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
为什么开玩笑说我错过了考试?
解决方案
为什么开玩笑说我错过了考试?
因为 Jest 认为pages/test.js
是一个测试文件。Jest 使用以下正则表达式来检测测试文件。
(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$
从文档中,
默认情况下,它会查找文件夹内的 、 和 文件
.js
,以及.jsx
任何带有 or 后缀的文件(例如or )。它还将查找名为or的文件。.ts
.tsx
__tests__
.test
.spec
Component.test.js
Component.spec.js
test.js
spec.js
一个简单的解决方案是重命名文件。
推荐阅读
- asp.net-core - .NET Core Blazor 从 Razor 页面或 Blazor 组件动态检索 html 作为字符串
- c# - Azure Devops GitHttpClient 的 CreateAnnotatedTagAsync API 失败
- reactjs - navigator.geolocation.watchPosition 在 React 中两次:更新频率不同
- javascript - 将代码从 Angular 7 迁移到 10 时出现缺少依赖错误
- r - 闪亮的数据表模式可编辑 - 限制特定的列和行
- javascript - 通过 getter 将数据代理到计算属性(v-model)
- javascript - await 没有得到返回值
- python - 尝试使用 openpyxl.load_workbook 访问文件时出现“TypeError:值必须是序列”
- excel - 在excel中的特定范围之间复制数据
- php - WHMCS 配置模块,默认添加自定义字段