chai@5.0.0 migrated to ES modules (see PR#1498) and changed the implementation of chai.use so that, I assume, it doesn't change the module export. chai-spies has not been updated since before this change, and therefore, throws an exception if used as documented.
Reproduction
const chai = require('chai');
const spies = require('chai-spies');
// The following will return an object with a spy function but does not apply it to the chai global.
chai.use(spies);
const fn = () => {};
chai.spy(fn);
This example produces the following error.
TypeError: chai.spy is not a function
at Object.eval (index.js:7:6)
Additionally, subsequent calls to chai.use(spies) will not return an object with a spy function at all (see PR#1498).
Workaround
To work around the bug, create a module that exports the spy function, and import it where needed.
/**
* chai-spy.js
*
* Export the spy function from chai-spies for use with chai@>=5.0.0.
*
* chai@5.0.0 migrated to ES modules and changed the implementation of chai.use so that, I assume, it doesn't change
* the module export. chai-spies has not been updated since before this change, and therefore, needs an alternative
* usage than is documented.
*
* See https://github.com/chaijs/chai-spies/issues/121.
*/
const chai = require('chai');
const spies = require('chai-spies');
module.exports = chai.use(spies).spy;
Usage
const spy = require('./chai-spy.js');
const fn = () => {};
chai.spy(fn);
chai@5.0.0 migrated to ES modules (see PR#1498) and changed the implementation of
chai.useso that, I assume, it doesn't change the module export. chai-spies has not been updated since before this change, and therefore, throws an exception if used as documented.Reproduction
This example produces the following error.
Additionally, subsequent calls to
chai.use(spies)will not return an object with aspyfunction at all (see PR#1498).Workaround
To work around the bug, create a module that exports the
spyfunction, and import it where needed.Usage