Skip to content

ReferenceError: describe is not defined

Error message

                describe is not defined
              

Quick summary

The test uses Jest globals (describe, test, expect) but they're not defined. Jest is either not the test runner or globals aren't configured.

Why this happens

  • The file is being run by Node.js directly instead of through Jest.

  • Jest's globals configuration is set to false but imports are missing.

  • The test file is not in a location Jest discovers as a test file.

  • A different test runner is being used that doesn't provide these globals.

Minimal example

✗ Broken code
// Running with: node test.js
// describe is not a Node.js global
describe('test', () => { /* ... */ });
✓ Fixed code
// Running with: npx jest
// Jest provides describe, test, expect as globals
describe('test', () => { /* ... */ });

Run the test through Jest, not Node.js, so globals are available.

How to diagnose

  • Check how the test is being run — it should be through 'jest', not 'node'.

  • Check jest.config.js for globals: false setting.

  • Verify the file matches Jest's testMatch pattern (e.g., *.test.js).

  • Check package.json test script: it should be "test": "jest".

How to fix

  • Run tests through Jest: npx jest or npm test.

  • If globals is false in config, add imports: import { describe, test, expect } from '@jest/globals'.

  • Ensure the test file is named correctly (e.g., utils.test.js, not utils.js).

How to prevent

  • Always run tests through the test runner, not directly with Node.js.

  • Keep Jest configuration consistent across the project.

Related Resources

Related Glossary

Related Lessons

← Back to language