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.
- Remove chai/register-expect.js from mocha config
- 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;
);
});
Problem
My mocha setup has traditionally looked something like this:
mocha config excerpt
test/setup/test-libs.ts
test/unit/example.spec.ts
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.
globalThis.expect = expectto test/setup/test-libs.tsmocha config excerpt
test/setup/test-libs.ts
test/unit/example.spec.ts