Skip to content

chai v5, register-expect.js, and plugins #1655

@shawnsparks

Description

@shawnsparks

Problem

My mocha setup has traditionally looked something like this:

mocha config excerpt

"require": [
    "chai/register-expect.js"
    "test/setup/test-libs.ts"
]

test/setup/test-libs.ts

import * as chai from 'chai';
import sinonChai from 'sinon-chai';
import chaiAsPromised from 'chai-as-promised';

chai.use(sinonChai);
chai.use(chaiAsPromised);

test/unit/example.spec.ts

import sinon from 'sinon';

describe('an example', () => {
    it('should work', () => {
        expect(sinon.stub).to.not.have.been.called;
    );
});

And then I don't bother importing expect in various test files and everything worked great prior to chai v5. Since going to chai v5, I get Error: Invalid Chai property: called.

Workaround

I found a workaround by not using chai/register-expect.js, but it seems odd I have to do this. I haven't been able to find any information indicating what I'm doing wrong or if this is a known issue.

  1. Remove chai/register-expect.js from mocha config
  2. Add globalThis.expect = expect to test/setup/test-libs.ts

mocha config excerpt

"require": [
    "test/setup/test-libs.ts"
]

test/setup/test-libs.ts

import * as chai from 'chai';
import sinonChai from 'sinon-chai';
import chaiAsPromised from 'chai-as-promised';

chai.use(sinonChai);
chai.use(chaiAsPromised);

//@ts-ignore
globalThis.expect = chai.expect;

test/unit/example.spec.ts

import sinon from 'sinon';

describe('an example', () => {
    it('should work', () => {
        expect(sinon.stub).to.not.have.been.called;
    );
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions