简体   繁体   中英

Jest : ReferenceError: describe is not defined

I try to setup jest for my project following this tutorial: https://dev.to/nedsoft/testing-nodejs-express-api-with-jest-and-supertest-1km6

But when I try to run a basic test I've got the error below:

 FAIL  tests/sample.test.js
  ● Test suite failed to run

    ReferenceError: describe is not defined

    > 1 | describe('Sample Test', () => {
        | ^
      2 |     it('should test that true === true', () => {
      3 |         expect(true).toBe(true)
      4 |     })

      at Object.<anonymous> (tests/sample.test.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.521 s
Ran all test suites.
(node:15452) ExperimentalWarning: The fs.promises API is experimental
npm ERR! Test failed.  See above for more details.

I try to remove in my package.json:

testEnvironment: 'node',

But nothing change.

A idea?

Ok the problem is last version of jest has conflict with node 10.13.0

Source: https://github.com/facebook/jest/issues/9538

So make this and it is ok:

npm install --save-dev jest@25.2.2 supertest

"jest": "^27.2.0" . In my case, I got ReferenceError: describe is not defined error because I set theinjectGlobals configuration to false in jest.config.js .

The doc about injectGlobals configuration:

Insert Jest's globals ( expect , test , describe , beforeEach etc.) into the global environment. If you set this to false , you should import from @jest/globals

jest.config.js :

module.exports = {
  injectGlobals: true,
  testEnvironment: 'node',
};
describe('index', () => {
  test('should pass', () => {
    expect(1 + 1).toBe(2);
  });
});

After setting injectGlobals to true , it works fine.

In my case it was because I was trying to add a test for one of the pages of my nextjs app.

Instead of adding it inside of the pages folder, I moved the offending test that was causing the Jest: ReferenceError: describe is not defined to a test folder, and it started to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM