Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest Your test suite must contain at least one test

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?

like image 468
dev_el Avatar asked Sep 04 '25 03:09

dev_el


1 Answers

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 or Component.spec.js). It will also find files called test.js or spec.js.

A simple solution would be to rename the file.

like image 106
Arun Kumar Mohan Avatar answered Sep 05 '25 20:09

Arun Kumar Mohan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!