Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shawnsparks opened this issue Dec 12, 2024 · 2 comments
Open

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

shawnsparks opened this issue Dec 12, 2024 · 2 comments

Comments

@shawnsparks
Copy link

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;
    );
});
@43081j
Copy link
Contributor

43081j commented Dec 13, 2024

This is very strange for sure

The register script pretty much sets globalThis.expect = chai.expect just the same

But it seems you do get expect, you just don't get the plugin modifications. Which is even stranger

Off the top of my head I'm not sure why this would happen. We will have to dig a bit more

@shawnsparks
Copy link
Author

I should add it works just fine if I explicitly import expect again in the spec files instead of using the global expect. As best I can tell, it is as if register-expect.js and all my other imports are getting two different copies of expect or the Assertion prototype. No idea why that would be the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants