Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Mocha test fails because expect does not exist

ErrorReferenceError: expect is not defined

Short answer

Mocha only runs tests. It ships no expect, assert or should of its own and accepts any library that throws an Error. If a spec file uses expect without importing it, you get ReferenceError. Install an assertion library such as Chai and import expect in that file, using import syntax for Chai 5.

  • Applies to Node.js 22, Mocha, Chai 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the file named in the stack trace and look at its first lines. If there is no import or require of an assertion library, start with the first cause.

    head -n 10 test/example.test.js
    npm ls chai
  2. 2Match what you see, then jump to its fix

    1. 1ReferenceError: expect is not defined, thrown by the first expect call in the fileThe spec file never imports an assertion libraryFix
    2. 2The file does require('chai') but the import fails, or it worked before chai was upgraded to 5Chai 5 is installed and the spec loads it with requireFix

What does the error look like?

  • ReferenceError: expect is not defined

Cause 1 of 2

The spec file never imports an assertion library

1Check

The Mocha docs say Mocha lets you use any assertion library, and that anything which throws an Error works. Mocha does not define expect as a global, so every file that calls it must get it from a library.

grep -rn "expect" test/ | head

2Fix

Install Chai as a dev dependency and import expect at the top of each spec file that uses it.

npm install --save-dev chai

// test/example.test.js (ES module syntax)
import { expect } from 'chai';

describe('sum', () => {
  it('adds two numbers', () => {
    expect(1 + 1).to.equal(2);
  });
});

3Verify

Run Mocha again. The ReferenceError should be gone and the test should pass or fail on its own result.

npx mocha

Confirmed by mochajs.org

Did this fix it?

Cause 2 of 2

Chai 5 is installed and the spec loads it with require

1Check

The Chai 5.0.0 release notes say Chai now only supports ES modules and tell you to use import or dynamic import(), because require('chai') will fail. Check which version you have and how the file loads it.

npm ls chai
grep -rn "require('chai')" test/

2Fix

Switch the spec to ES module syntax, or load Chai with dynamic import inside an async hook.

// CommonJS spec file that cannot become an ES module
let expect;
before(async () => {
  ({ expect } = await import('chai'));
});

3Verify

Run Mocha and confirm the specs execute.

npx mocha

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Import expect explicitly in every spec file instead of relying on a global.
  • Pin the Chai major version in package.json and read the release notes before upgrading.
  • If you want no extra dependency, the Node.js assert module also works with Mocha, because it throws an Error on failure.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.