Your test suite must contain at least one test
Asked Answered
M

16

44

I have updated some of the dependencies today in my project, but it went through really smoothly. Now, when I'm about to push it, I started my tests. And boom. All of them throw:

Your test suite must contain at least one test.

My packages:

"jest": "23.1.0",
"jest-enzyme": "^6.0.1",
"jest-webpack-alias": "^3.3.3",
"jsdom": "^11.2.0",
"jsdom-global": "^3.0.2",
"enzyme": "3.3.0",
"enzyme-adapter-react-16": "^1.1.0",
"enzyme-to-json": "3.3.4",

And that is how my sample test file looks like:

/shared/components/App/MyRoute/__tests__/MyRoute.test.js

/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import { shallow } from 'enzyme';
import { ContactsRoute } from '../Route';

describe('<ContactsRoute />', () => {
  test('renders', () => {
    const wrapper = shallow(<ContactsRoute t={key => key} />);
    expect(wrapper).toMatchSnapshot();
  });
});

I have no idea why they stopped running so suddenly?

Edit - adding my jest config

  "jest": {
"collectCoverageFrom": [
  "shared/**/*.{js,jsx}"
],
"globals": {
  "JWT_SECRET": "local",
  "IS_TEST": "true"
},
"snapshotSerializers": [
  "<rootDir>/node_modules/enzyme-to-json/serializer"
],
"testPathIgnorePatterns": [
  "<rootDir>/(build|internal|node_modules|flow-typed|public|shared/services)/"
],
"moduleNameMapper": {
  "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/_test_config_/mocks/fileMock.js"
},
"testURL": "http://localhost:3005",
"transform": {
  ".": "<rootDir>/_test_config_/preprocessors/webpackAlias.js",
  "^.+\\.css$": "<rootDir>/_test_config_/preprocessors/cssTransform.js",
  "^(?!.*\\.(js|jsx|css|json)$)": "<rootDir>/_test_config_/preprocessors/fileTransform.js"
},
"setupFiles": [
  "<rootDir>/_test_config_/preprocessors/polyfills.js"
],
"setupTestFrameworkScriptFile": "./node_modules/jest-enzyme/lib/index.js"
},
Manchineel answered 13/6, 2018 at 18:0 Comment(6)
test() isn't a thing. it() is.Bastian
@DanO was this changed in some recent versions?Manchineel
Using the same version of Jest. test() works fine for me.Helvetian
@DanO Jest has test() which is equivalent to it().Shannonshanny
huh, I'd never heard of it and I didn't see it in a (quick, non-exhaustive) search of the docs. learn something new every day, I guess.Bastian
you may want to disable the 'Your test suite must contain at least one test' rule in Jest Disabling the 'Your test suite must contain at least one test' rule in JestTsan
S
63

make sure describe / it/ expect variables are not been imported

In my case, the IDE( VSCode ) automatically import the variable describe from another library.

Subject answered 28/8, 2020 at 3:43 Comment(6)
How did you fix that?Abidjan
@Abidjan you mean the auto import? I just delete the automatically added import sentence manually and disabled the VSCode auto-import function. here's an refSubject
If this happens, next step is #57874614 to have VSCode understand the jest globals.Hermes
@Hermes nice add to the solutionTreadwell
This was my issue. vscode imported describe/it/test from node:test instead of @jest/globalsSklar
Vscode imported "test" from node test and that wasted my 2 hoursHeptachord
L
23

Make sure that you have at least one describe / it / expect

In my case, i wrote just a describe / expect test and got this error message

Lelea answered 14/6, 2019 at 12:27 Comment(1)
jest/test/expect gives this error too. Replacing "test" with "it" fixed it. ThanksLieutenancy
U
22

If all the other responses fail, if you have any files named test.js, Jest will expect that file to contain a test.

I was using a file named test.js to manually test some stuff, and Jest was throwing this error. I re-named the file to something else (eg: 123.js) and I stopped getting this error message.

Unclog answered 29/8, 2021 at 1:11 Comment(5)
Damnn thanks this was the issue in my caseShaffert
I'm glad it helped. Not sure why Jest does this, and the logs aren't helpful at all either.Unclog
Another option besides renaming the file is to configure testMatch to be more specific. I set it to **/?*.test.tsBrolly
my js method was called test and that ruined itEllett
Exactly, may be you have a .test file with no test in it.Ceiba
C
9

May be you forgot it() block:

maybe you have a mistake and in somewhere code is like this:

describe('some text', () => {
    expect(...)...;
});

replace it with:

describe('some text', () => {
    it('my test', () => {
        expect(...)...;
    })
});
Concepcionconcept answered 15/5, 2021 at 5:53 Comment(0)
E
4

I had the same issue. The below steps solved the issue for me.
Step 1:

rm -rf ./dist

Step 2: Edit tsconfig.json

{ 
  "exclude": [
    "src/**/*.test.ts"
  ]
}
Endblown answered 3/8, 2022 at 20:5 Comment(0)
U
3

A common source of this error is that Jest picks up some file, expecting it to contain tests, when in fact it's just some random file that the developer never intended for Jest to act on.

Here is how Jest identifies test files by default (source):

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.

Here are some hypothetical examples of files that would inadvertently be picked up by Jest:

  • __tests__/mocks.js - a file containing some mock data for other tests.
  • util.test.js - a file containing utility functions for other tests.
  • test.js - a file that exports some dummy component for ad hoc manual testing.

You can solve this problem in one of two ways:

  1. Rename the file so that Jest does not find it.
  2. Configure Jest to ignore the file using testMatch or testRegex.

Note: Several existing answers reference specific instances of this problem (e.g. test.js or __tests__/*.js). This answer is intended to be more general and comprehensive.

Urgent answered 15/2, 2023 at 16:5 Comment(1)
Thanks for the answer! For this one "a file containing utility functions for other tests", if I name it "util.js" then jest won't find it, but how do I prevent Webpack to bundle it? Do I need to use IgnorePlugin or I can just rely on Tree shaking (as App codes won't import it)?Roughshod
E
2

Make sure that your describe function is not async

// Wrong
describe('setPackages', async () => {
 // ...
});

// Correct
describe('setPackages', () => {
 // ...
});
Erinerina answered 12/1, 2022 at 11:49 Comment(0)
T
1

Another option is to call Jest with the --passWithNoTests argument, as documented here. The flag is useful when you don't know how many tests a script will run, such as with git commit hooks.

Tauten answered 15/5, 2021 at 13:42 Comment(0)
E
1

another issue which raises this Error is when you have put some none-test files into your __test__ directory.

All files in your __test__ directory must include some tests to run.

Expel answered 13/2, 2022 at 13:14 Comment(0)
P
1

In my case, I removed the following entry from my test file for example : my_module.test.ts.

import test from "node:test";

After that (describe|test|expect) and (describe| it|expect) both worked perfectly!

Psychomotor answered 5/9, 2023 at 12:6 Comment(0)
M
0

So, I found the answer.

The Note in here says that if we are using babel-jest alongside with our own preprocessors, the babel-jest has to be explicitly defined in the transform option.

It is important to add "^.+\\.jsx?$": "babel-jest" as the first option in the transform config.

Manchineel answered 14/6, 2018 at 10:35 Comment(0)
E
0

Might happen when you have a test file but the contents are not saved? Try saving the file before running the tests.

Ema answered 25/2, 2022 at 4:17 Comment(0)
H
0

For my typescript users out there with this issue. Use yarn tsc -watch or npm tsc -watch. I just forgot to build my ts files to js and hence the case.

Haircut answered 28/8, 2022 at 5:25 Comment(0)
G
0

In my case there was a wrongly closed curly braces of beforeEach function,

Because of wrong location of this curly braces my it() function seemed like it was inside the beforeEach function. Therefore jest wasn't able to ident

Golgotha answered 8/2, 2023 at 15:34 Comment(0)
F
0

I had this problem and it was because I had mock data in my __tests__ folder. The way I solved it is to tell jest to ignore those files by using the property modulePathIgnorePatterns in jest.config.ts. you can see this question to set it correctly.

Florella answered 9/2 at 11:37 Comment(0)
L
0

I had a platform file called test.ts it was picking up. Within jest.config.ts I added:

testMatch: ['**/tests/**/*.[jt]s?(x)', '**/?(*.)+(spec).[jt]s?(x)'],

This overrode the default:

(default: [ "**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ])

Docs here: https://jestjs.io/docs/configuration#testmatch-arraystring

Lendlease answered 11/3 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.