Sinon-Chai is a testing utility that integrates the Sinon.JS mocking framework with the Chai assertion library, allowing developers to write more expressive and readable assertions for spies, stubs, and mocks. Instead of using Sinon's direct assertion methods (`sinon.assert.calledWith`) or awkward Chai property checks, Sinon-Chai extends Chai's `should` and `expect` interfaces to provide natural language assertions like `expect(mySpy).to.have.been.calledWith('foo')`. The current stable version is 4.0.1, which supports Chai v5 and v6, and Sinon v4+. The library maintains an active release cadence, typically updating to support new major versions of its peer dependencies, Chai and Sinon. Key differentiators include its seamless integration into the Chai assertion chain, improving test readability and developer experience by providing a unified assertion style across a test suite. It's widely used in JavaScript testing environments for both Node.js and browser applications.
npm install sinon-chaiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up Sinon-Chai with Chai and Sinon, creating a spy and using `expect` assertions to verify call counts and arguments.
Ensure your project meets the minimum peer dependency requirements for Node.js, Sinon, and Chai before upgrading to `sinon-chai@3.0.0` or higher.
Check your `package.json` for `chai` and `sinon` versions. `sinon-chai@4.0.x` requires `chai: ^5.0.0 || ^6.0.0` and `sinon: >=4.0.0`.
Adjust your assertion syntax to `expect(spy).to.always.have.been.calledWith(...)` or `spy.should.always.have.been.calledWith(...)`.
For assertions involving `sinon.match`, integrate `chai-samsam` or perform a direct check on the spy's calls like `expect(mySpy.firstCall.args[0]).to.match(someRegex);`.
Ensure you have `import { expect, use } from 'chai';` and `use(sinonChai);` at the top of your test file. For the `should` style, also include `import 'chai/register-should';` or call `chai.should();`.Review your test setup and the code under test. Ensure the spy is correctly injected and that the execution path actually triggers the spy. Use `mySpy.getCalls()` or `console.log(mySpy.args)` to inspect the spy's behavior.
Use ESM import syntax: `import sinonChai from 'sinon-chai';`. If your project is CommonJS, you might need to ensure compatibility or check for older versions that explicitly supported CJS without such issues.