I have a simple test file in ./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>
)
}
In ./test/pages/index.test.js
I made the following simple test to check if my page is rendering properly and if it has a heading
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()
})
})
After running the test, I get the following error
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
Why does jest say I am missing a test?
Why does jest say I am missing a test?
Because Jest thinks pages/test.js
is a test file. Jest uses the following regex to detect test files.
(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$
From the docs,
By default it looks for
.js
,.jsx
,.ts
and.tsx
files inside of__tests__
folders, as well as any files with a suffix of.test
or.spec
(e.g.Component.test.js
orComponent.spec.js
). It will also find files calledtest.js
orspec.js
.
A simple solution would be to rename the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With