{"id":13627900,"url":"https://github.com/rluvaton/sinon-jest-matchers","last_synced_at":"2026-04-30T16:32:04.986Z","repository":{"id":59108543,"uuid":"535444044","full_name":"rluvaton/sinon-jest-matchers","owner":"rluvaton","description":"Jest Matchers for sinon","archived":false,"fork":false,"pushed_at":"2023-02-02T11:28:13.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-31T06:09:07.385Z","etag":null,"topics":["hacktoberfest","javascript","jest","sinonjs","testing","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rluvaton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-11T22:41:32.000Z","updated_at":"2023-12-05T16:57:24.000Z","dependencies_parsed_at":"2023-02-17T19:00:22.710Z","dependency_job_id":null,"html_url":"https://github.com/rluvaton/sinon-jest-matchers","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/rluvaton/sinon-jest-matchers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fsinon-jest-matchers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fsinon-jest-matchers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fsinon-jest-matchers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fsinon-jest-matchers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rluvaton","download_url":"https://codeload.github.com/rluvaton/sinon-jest-matchers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fsinon-jest-matchers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32470879,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"ssl_error","status_checked_at":"2026-04-30T13:12:06.837Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","javascript","jest","sinonjs","testing","typescript"],"created_at":"2024-08-01T22:00:40.318Z","updated_at":"2026-04-30T16:32:04.959Z","avatar_url":"https://github.com/rluvaton.png","language":"TypeScript","funding_links":[],"categories":["Projects by main language"],"sub_categories":["typescript"],"readme":"# Sinon Jest Matchers\n[![npm](https://img.shields.io/npm/v/sinon-jest-matchers)](https://www.npmjs.com/package/sinon-jest-matchers)\n\n## Why\n\n### TL;DR\nWithout this library, in case of an error you will get very vague error messages\n\n**Before:**\n```console\nexpect(received).toEqual(expected) // deep equality\n\nExpected: 2\nReceived: 1\n```\n\n**After:**\n```console\nexpect(sinon.spy()).sinonToBeCalledTimes(expected)\n\nExpected number of calls: 2\nReceived number of calls: 1\n```\n\n### Deeper explanation:\n**Example:**\nIf you want to use sinon spies you will need to do something like this:\n```js\ntest('Check that spy called', async () =\u003e {\n    const mySpy = sinon.spy();\n\n    mySpy();\n    \n    // Triggering an error\n    expect(mySpy.callCount).toEqual(2);\n});\n```\n\nThe error message will be:\n```console\nexpect(received).toEqual(expected) // deep equality\n\nExpected: 2\nReceived: 1\n```\n\nHowever, if you use this library, the test will look like this:\n```js\ntest('Check that spy called', async () =\u003e {\n    const mySpy = sinon.spy();\n\n    mySpy();\n\n    // Triggering an error\n    expect(mySpy).sinonToBeCalledTimes(2);\n});\n```\n\nAnd the error message will be:\n```console\nexpect.sinonToBeCalledTimes(expected)\n\nExpected number of calls: 2\nReceived number of calls: 1\n```\n\n## Installation\n\nWith npm:\n\n```sh\nnpm install --save-dev sinon-jest-matchers\n```\n\nWith yarn:\n\n```sh\nyarn add -D sinon-jest-matchers\n```\n\n## Setup\n\n```javascript\n// ./testSetup.js\n\n// add all sinon-jest-matchers matchers\nimport * as matchers from 'sinon-jest-matchers';\nexpect.extend(matchers);\n\n// or just add specific matchers\nimport { sinonToBeCalled, sinonToBeCalledTimes } from 'sinon-jest-matchers';\nexpect.extend({ sinonToBeCalled, sinonToBeCalledTimes });\n```\n\nAdd your setup script to your Jest `setupFilesAfterEnv` configuration. [See for help](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)\n\n```json\n\"jest\": {\n  \"setupFilesAfterEnv\": [\"./testSetup.js\"]\n}\n```\n\nTo automatically extend `expect` with all matchers, you can use\n\n```json\n\"jest\": {\n  \"setupFilesAfterEnv\": [\"sinon-jest-matchers/all\"]\n}\n```\n\n### Typescript\n\nIf your editor does not recognise the custom `sinon-jest-matchers` matchers, add a `global.d.ts` file to your project with:\n\n```ts\nimport 'sinon-jest-matchers';\n```\n\n_Note: When using `ts-jest \u003e= 25.5.0`_\n\nSince the breaking changes in `25.5.0` you may also need to update your `tsconfig.json` to include the new `global.d.ts` file in the `files` property like so:\n\n```json\n{\n  \"compilerOptions\": {\n    ...\n  },\n  ...\n  \"files\": [\"global.d.ts\"]\n}\n```\n\nAlso note that when adding this for the first time this affects which files are compiled by the TypeScript compiler and you might need to add the `include` property as well. See the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) for more details.\n\nIf the above import syntax does not work, replace it with the following:\n\n```ts\n/// \u003creference types=\"sinon-jest-matchers\" /\u003e\n```\n\n## Matchers\n\n### `.sinonToBeCalled()`\n\nEquivalent to `.toBeCalled()` and `.toHaveBeenCalled()` in jest\n\n```js\nfunction drinkAll(callback, flavour) {\n  if (flavour !== 'octopus') {\n    callback(flavour);\n  }\n}\n\ndescribe('drinkAll', () =\u003e {\n  test('drinks something lemon-flavoured', () =\u003e {\n    const drink = sinon.spy();\n    drinkAll(drink, 'lemon');\n    expect(drink).sinonToBeCalled();\n  });\n\n  test('does not drink something octopus-flavoured', () =\u003e {\n    const drink = sinon.spy();\n    drinkAll(drink, 'octopus');\n    expect(drink).not.sinonToBeCalled();\n  });\n});\n```\n\n### `.sinonToBeCalledTimes(number)`\n\nEquivalent to `.toHaveBeenCalledTimes(number)` and `.toBeCalledTimes(number)` in jest\n\n```js\ntest('drinkEach drinks each drink', () =\u003e {\n  const drink = sinon.spy();\n  drinkEach(drink, ['lemon', 'octopus']);\n  expect(drink).sinonToBeCalledTimes(2);\n});\n```\n\n### `.sinonToBeCalledWith(arg1, arg2, ...)`\n\nEquivalent to `.toHaveBeenCalledWith(arg1, arg2, ...)` and `.toBeCalledWith()` in jest\n\n```js\ntest('registration applies correctly to orange La Croix', () =\u003e {\n    const beverage = new LaCroix('orange');\n    register(beverage);\n    const f = sinon.spy();\n    applyToAll(f);\n    expect(f).sinonToBeCalledWith(beverage);\n});\n```\n\n### `.sinonToBeCalledTimesWith(number, arg1, arg2, ...)`\n\nEquivalent combination of `.sinonToBeCalledTimes(number)` with `.sinonToBeCalledWith(arg1, arg2, ...)`\n\n```js\ntest('registration applies correctly to orange La Croix', () =\u003e {\n  const beverage = new LaCroix('orange');\n  register(beverage);\n  const f = sinon.spy();\n  applyToAll(f);\n  applyToAll(() =\u003e {});\n  applyToAll(f);\n  expect(f).sinonToBeCalledTimesWith(2, beverage);\n});\n```\n\n### `.sinonLastCalledWith(arg1, arg2, ...)`\n\nEquivalent to `.toHaveBeenLastCalledWith(arg1, arg2, ...)` and `.lastCalledWith(arg1, arg2, ...)` in jest\n\n```js\ntest('applying to all flavors does mango last', () =\u003e {\n  const drink = sinon.spy();\n  applyToAllFlavors(drink);\n  expect(drink).sinonLastCalledWith('mango');\n});\n```\n\n### `.sinonNthCalledWith(nthCall, arg1, arg2, ....)`\n\nEquivalent to `.toHaveBeenNthCalledWith(nthCall, arg1, arg2, ...)` and `.nthCalledWith(nthCall, arg1, arg2, ...)` in jest\n\n```js\ntest('drinkEach drinks each drink', () =\u003e {\n  const drink = sinon.spy();\n  drinkEach(drink, ['lemon', 'octopus']);\n  expect(drink).sinonNthCalledWith(1, 'lemon');\n  expect(drink).sinonNthCalledWith(2, 'octopus');\n});\n```\n\n\u003e Note:\n\u003e The nth argument must be positive integer starting from 1.\n\n### `.sinonToReturn()`\n\nEquivalent to `.toHaveReturned()` and `.toReturn()` in jest\n\n```js\ntest('drinks returns', () =\u003e {\n  const drink = sinon.spy(() =\u003e true);\n\n  drink();\n\n  expect(drink).sinonToReturn();\n});\n```\n\n### `.sinonToReturnTimes(number)`\n\nEquivalent to `.toHaveReturnedTimes(number)` and `.toReturnTimes(number)` in jest\n\n```js\ntest('drink returns twice', () =\u003e {\n  const drink = sinon.spy(() =\u003e true);\n\n  drink();\n  drink();\n\n  expect(drink).sinonToReturnTimes(2);\n});\n```\n\n### `.sinonToReturnWith(value)`\n\nEquivalent to `.toHaveReturnedWith(value)` and `.toReturnWith(value)` in jest\n\n```js\ntest('drink returns La Croix', () =\u003e {\n  const beverage = {name: 'La Croix'};\n  const drink = sinon.spy(beverage =\u003e beverage.name);\n\n  drink(beverage);\n\n  expect(drink).sinonToReturnWith('La Croix');\n});\n```\n\n### `.sinonLastReturnedWith(value)`\n\nEquivalent to `.toHaveLastReturnedWith(value)` and `.lastReturnedWith(value)` in jest\n\n```js\ntest('drink returns La Croix (Orange) last', () =\u003e {\n  const beverage1 = {name: 'La Croix (Lemon)'};\n  const beverage2 = {name: 'La Croix (Orange)'};\n  const drink = sinon.spy(beverage =\u003e beverage.name);\n\n  drink(beverage1);\n  drink(beverage2);\n\n  expect(drink).sinonLastReturnedWith('La Croix (Orange)');\n});\n```\n\n\n\n## Inspirations and credits\n1. The matchers and their tests taken from jest code and updated to use sinon\n2. `jest-extended` for the loading, setup and the file directory structure\n3. The expect API docs are taken from jest website and updated to sinon\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frluvaton%2Fsinon-jest-matchers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frluvaton%2Fsinon-jest-matchers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frluvaton%2Fsinon-jest-matchers/lists"}