2

The Jest documentation on Globals says the following (emphasis mine).

In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, it} from '@jest/globals'.

So, why does typing jest or npm test with scripts.test (in package.json) pointing to jest --verbose cause the ReferenceError: describe is not defined error? Some of the questions around this on SO and elsewhere, mention that, jest should be installed globally, installed with --save-dev, referred to local node_modules, etc. None of them work.

What am I missing?

This is my index.spec.js.

function checkArgs(args) {
  if (args.length > 4) {
    // Error message on extraneous parameters
    return false
  } else if (args.length < 4) {
    // Error message on insufficient parameters
    return false
  } else {
    return true
  }
}

describe("Arguments check", () => {
  test("Number of arguments should be 4", () => {
    expect(checkArgs(process.argv)).toBe(true)
  })
})
4
  • That piece of code you posted is okay. Do you have a custom jest configuration? Jest should be installed locally. Also, try removing node_modules directory and try a new install. Commented Jun 23, 2020 at 9:46
  • I tried jest init too. Didn't help. Commented Jun 23, 2020 at 10:07
  • I was able to get this working with const {describe, expect, test} = require( '@jest/globals') at the top of the file. :roll-eyes: Commented Jun 23, 2020 at 15:28
  • Take a look at this thread: stackoverflow.com/q/28400459/22161745 Commented Jul 2, 2023 at 6:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.