Mocha test fails because expect does not exist
ReferenceError: expect is not definedShort 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.
Fix it fast
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 chai2Match what you see, then jump to its fix
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/ | head2Fix
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 mochaConfirmed by mochajs.org
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 mochaConfirmed by github.com
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix