38

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"
},
skyboyer
  • 22,209
  • 7
  • 57
  • 64
mdmb
  • 4,833
  • 7
  • 42
  • 90

13 Answers13

50

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

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

Oboo Cheng
  • 4,250
  • 3
  • 24
  • 29
  • How did you fix that? – xtra Sep 22 '20 at 06:23
  • 1
    @xtra you mean the auto import? I just delete the automatically added import sentence manually and disabled the VSCode auto-import function. [here's an ref](https://stackoverflow.com/questions/47350589/how-to-disable-automatic-import-statements-in-vscode-october-2017) – Oboo Cheng Sep 23 '20 at 02:11
  • 2
    If this happens, next step is https://stackoverflow.com/questions/57874114/intellisense-for-jest-not-working-in-vs-code to have VSCode understand the jest globals. – J.G.Sebring Feb 13 '21 at 22:21
  • 1
    @J.G.Sebring nice add to the solution – Guy Park Jan 18 '22 at 08:02
21

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

Fragalli
  • 229
  • 2
  • 7
18

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.

nkhil
  • 1,452
  • 1
  • 18
  • 37
6

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(...)...;
    })
});
Jafar Amini
  • 315
  • 2
  • 8
3

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"
  ]
}
Linden X. Quan
  • 584
  • 5
  • 18
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.

Nick Ribal
  • 1,959
  • 19
  • 26
1

Make sure that your describe function is not async

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

// Correct
describe('setPackages', () => {
 // ...
});
mufasa
  • 1,271
  • 13
  • 18
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.

Sina
  • 379
  • 2
  • 9
1

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.

srk
  • 1,625
  • 1
  • 10
  • 26
  • 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)? – Marson Mao Apr 26 '23 at 08:15
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.

mdmb
  • 4,833
  • 7
  • 42
  • 90
0

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

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.

Shrihari
  • 95
  • 2
  • 11
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