{"id":15845106,"url":"https://github.com/rluvaton/expect-matchers","last_synced_at":"2025-03-15T20:30:51.977Z","repository":{"id":203519668,"uuid":"709781719","full_name":"rluvaton/expect-matchers","owner":"rluvaton","description":"Additional Jest/vitest/expect matchers","archived":false,"fork":false,"pushed_at":"2024-01-29T19:10:12.000Z","size":30,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-27T04:09:23.854Z","etag":null,"topics":["assertions","bdd","expect","extended","jest","matchers","testing","vitest"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-25T11:44:35.000Z","updated_at":"2023-12-14T06:17:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"3dd1107b-9547-4d34-81c2-d3cd285d6842","html_url":"https://github.com/rluvaton/expect-matchers","commit_stats":null,"previous_names":["rluvaton/expect-matchers"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fexpect-matchers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fexpect-matchers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fexpect-matchers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rluvaton%2Fexpect-matchers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rluvaton","download_url":"https://codeload.github.com/rluvaton/expect-matchers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243790952,"owners_count":20348378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["assertions","bdd","expect","extended","jest","matchers","testing","vitest"],"created_at":"2024-10-05T17:41:53.299Z","updated_at":"2025-03-15T20:30:51.571Z","avatar_url":"https://github.com/rluvaton.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# expect-matchers\n[![npm](https://img.shields.io/npm/v/expect-matchers)](https://www.npmjs.com/package/expect-matchers)\n\n## Why\n\n### TL;DR\nUntil and if [jest-community/jest-extended#640](https://github.com/jest-community/jest-extended/pull/640) is merged I need better toIncludeSameMembers\n\nCurrently, if you have a test like this and is using jest-extended:\n```js\nit('example', () =\u003e {\n\tconst expected = Array.from({ length: 1000 }, (_, i) =\u003e ({\n\t\ti,\n\t\tvalue: 'expected'\n\t}));\n\tconst actual = [...expected];\n\tconst tmp = actual[200];\n\tactual[200] = actual[201];\n\tactual[201] = { ...tmp, value: 'hope you find me' };\n\n\texpect(actual).toIncludeSameMembers(expected);\n});\n```\n\nYou will get an error like this:\n```js\n   expect(received).toIncludeSameMembers(expected)\n\n    Expected list to have the following members and no more:\n      [{\"i\": 0, \"value\": \"expected\"}, {\"i\": 1, \"value\": \"expected\"}, {\"i\": 2, \"value\": \"expected\"}, {\"i\": 3, \"value\": \"expected\"}, {\"i\": 4, \"value\": \"expected\"}, {\"i\": 5, \"value\": \"expected\"}, {\"i\": 6, \"value\": \"expected\"}, {\"i\": 7, \"value\": \"expected\"}, {\"i\": 8, \"value\": \"expected\"}, {\"i\": 9, \"value\": \"expected\"}, …]\n    Received:\n      [{\"i\": 0, \"value\": \"expected\"}, {\"i\": 1, \"value\": \"expected\"}, {\"i\": 2, \"value\": \"expected\"}, {\"i\": 3, \"value\": \"expected\"}, {\"i\": 4, \"value\": \"expected\"}, {\"i\": 5, \"value\": \"expected\"}, {\"i\": 6, \"value\": \"expected\"}, {\"i\": 7, \"value\": \"expected\"}, {\"i\": 8, \"value\": \"expected\"}, {\"i\": 9, \"value\": \"expected\"}, …]\n\n      410 |             actual[201] = { ...tmp, value: 'hope you find me' };\n      411 |\n    \u003e 412 |             expect(actual).toIncludeSameMembers(expected);\n          |                            ^\n      413 |     });\n\n```\n\n**which is very hard to understand** so I instead this will be the error message:\n\n```js\n    expect(received).toIncludeSameMembers(expected)\n\n    - Expected  - 1\n    + Received  + 1\n\n    @@ -799,11 +799,11 @@\n          \"i\": 199,\n          \"value\": \"expected\",\n        },\n        Object {\n          \"i\": 200,\n    -     \"value\": \"expected\",\n    +     \"value\": \"hope you find me\",\n        },\n        Object {\n          \"i\": 201,\n          \"value\": \"expected\",\n        },\n\n      410 |             actual[201] = { ...tmp, value: 'hope you find me' };\n      411 |\n    \u003e 412 |             expect(actual).toIncludeSameMembers(expected);\n          |                            ^\n      413 |     });\n```\n\n## Installation\n\nWith npm:\n\n```sh\nnpm install --save-dev expect-matchers\n```\n\nWith yarn:\n\n```sh\nyarn add -D expect-matchers\n```\n\n## Setup\n\n```javascript\n// ./testSetup.js\n\n// add all expect-matchers matchers\nimport * as matchers from 'expect-matchers';\nexpect.extend(matchers);\n\n// or just add specific matchers\nimport { toIncludeSameMembers } from 'expect-matchers';\nexpect.extend({ sinonToBeCalled, sinonToBeCalledTimes });\n```\n\nAdd your setup script at the beginning of each test in jest it called (`setupFilesAfterEnv`)\n\n### If you use jest\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\": [\"expect-matchers/all\"]\n}\n```\n\n### Typescript\n\nIf your editor does not recognise the custom `expect-matchers` matchers, add a `global.d.ts` file to your project with:\n\n```ts\nimport 'expect-matchers';\n```\n\n### If you also use `jest-extended`\nIf you also use `jest-extended` you should not extend `expect` with the matcher `toIncludeSameMembers` as it can cause undefined behavior\n\n## Matchers\n\n#### .toIncludeSameMembers([members, fnOrKey?])\n\n```js\ntest('passes when arrays match in a different order', () =\u003e {\n  expect([1, 2, 3]).toIncludeSameMembers([3, 1, 2]);\n  expect([{ foo: 'bar' }, { baz: 'qux' }]).toIncludeSameMembers([{ baz: 'qux' }, { foo: 'bar' }]);\n});\n```\n\n##### If you are working with objects\nFor best error output you should pass a key or a function that will return a unique key to the matcher so we can display the error better\n\n```js\ntest('passes when arrays match in a different order', () =\u003e {\n  expect([{ id: 2, foo: 'bar' }, { id: 1, baz: 'qux' }]).toIncludeSameMembers([{ id: 1, baz: 'QUX' }, { id: 2, foo: 'bar' }], 'id');\n  expect([{ id: 2, foo: 'bar' }, { id: 1, baz: 'qux' }]).toIncludeSameMembers([{ id: 1, baz: 'QUX' }, { id: 2, foo: 'bar' }], (itemA, itemB) =\u003e itemA.id === itemB.id);\n});\n```\n\n## Inspirations and credits\n`jest-extended` for the loading, setup some of the code and the file directory structure\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frluvaton%2Fexpect-matchers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frluvaton%2Fexpect-matchers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frluvaton%2Fexpect-matchers/lists"}