{"library":"sinon-chai","title":"Sinon-Chai: Sinon.JS Assertions for Chai","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install sinon-chai"],"cli":null},"imports":["import sinonChai from 'sinon-chai';","import { use, expect } from 'chai';\nuse(sinonChai);","import 'chai/register-should'; // or `chai.should()`\n// ... then later:\nmySpy.should.have.been.calledOnce;"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { expect, use } from 'chai';\nimport * as sinon from 'sinon';\nimport sinonChai from 'sinon-chai';\n\n// Initialize Chai with sinon-chai plugin\nuse(sinonChai);\n\ndescribe('MyService with Sinon-Chai', () => {\n  let mySpy: sinon.SinonSpy;\n\n  beforeEach(() => {\n    // Create a new Sinon spy before each test\n    mySpy = sinon.spy();\n  });\n\n  afterEach(() => {\n    // Restore the spy after each test to prevent side effects\n    mySpy.restore();\n  });\n\n  it('should call the spy once with the expected argument', () => {\n    const serviceMethod = (param: string) => {\n      mySpy(param);\n    };\n\n    serviceMethod('first-call');\n\n    // Use sinon-chai assertions\n    expect(mySpy).to.have.been.calledOnce;\n    expect(mySpy).to.have.been.calledWith('first-call');\n    expect(mySpy).to.not.have.been.calledWith('wrong-argument');\n  });\n\n  it('should not call the spy if a condition is not met', () => {\n    const serviceMethodConditional = (shouldExecute: boolean) => {\n      if (shouldExecute) {\n        mySpy('executed');\n      }\n    };\n\n    serviceMethodConditional(false);\n\n    expect(mySpy).to.not.have.been.called;\n    expect(mySpy).to.have.callCount(0);\n  });\n});","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}