Skip to content

chai-spies does not work as documented with chai@>=5.0.0 #121

@TrevorKarjanis

Description

@TrevorKarjanis

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions