chai icon indicating copy to clipboard operation
chai copied to clipboard

Order of `chai.use` breaks `.has.property` target change

Open achingbrain opened this issue 5 years ago • 1 comments

Running the following code:

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))

chai.expect({a: 1}).to.have.property('a').that.is.a('number') // copied from the docs

results in this error:

$ node test.js 

/path/to/project/node_modules/chai/lib/chai/assertion.js:141
      throw new AssertionError(msg, {
      ^
AssertionError: expected { a: 1 } to be a number
    at Object.<anonymous> (/path/to/project/test.js:5:51)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
...

However swapping the order of the chai.use(...) statements around or just using one of them makes it pass:

// fails
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))

// passes
chai.use(require('chai-as-promised'))
chai.use(require('dirty-chai'))

// passes
chai.use(require('chai-as-promised'))
// chai.use(require('dirty-chai'))

// passes
// chai.use(require('chai-as-promised'))
chai.use(require('dirty-chai'))

I'm not really sure if this is a problem with chai, chai-as-promised, dirty-chai or a combination of all three but presumably the order in which you chai.use things shouldn't matter?

Because chai is a singleton this can also happen across multiple files and even multiple modules in the same project if they all depend on the same version (as seen in #1213).

achingbrain avatar Oct 03 '19 11:10 achingbrain

I'm not really sure if this is a problem with chai, chai-as-promised, dirty-chai or a combination of all three but presumably the order in which you chai.use things shouldn't matter?

I'd think so as well, though looking at the code of chai-as-promised and dirty-chai, I'm not so certain. They each have their own way of modifying Chai, so interactions certainly are possible.

rgroothuijsen avatar Apr 16 '20 18:04 rgroothuijsen