{"id":13769589,"url":"https://github.com/mattphillips/jest-chain","last_synced_at":"2025-04-13T11:48:21.469Z","repository":{"id":32211236,"uuid":"131911897","full_name":"mattphillips/jest-chain","owner":"mattphillips","description":"Chain Jest matchers together to create one powerful assertion 🃏⛓","archived":false,"fork":false,"pushed_at":"2023-01-09T08:38:47.000Z","size":442,"stargazers_count":277,"open_issues_count":3,"forks_count":16,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-04T05:08:39.689Z","etag":null,"topics":["assertions","chain","chainable","dry","expect","expressive","fail-fast","jest","matchers"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/jest-chain","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/mattphillips.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["mattphillips"]}},"created_at":"2018-05-02T22:21:29.000Z","updated_at":"2024-11-07T22:11:03.000Z","dependencies_parsed_at":"2023-01-14T20:45:56.439Z","dependency_job_id":null,"html_url":"https://github.com/mattphillips/jest-chain","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattphillips%2Fjest-chain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattphillips%2Fjest-chain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattphillips%2Fjest-chain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattphillips%2Fjest-chain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattphillips","download_url":"https://codeload.github.com/mattphillips/jest-chain/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710411,"owners_count":21149186,"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","chain","chainable","dry","expect","expressive","fail-fast","jest","matchers"],"created_at":"2024-08-03T17:00:29.050Z","updated_at":"2025-04-13T11:48:21.447Z","avatar_url":"https://github.com/mattphillips.png","language":"JavaScript","funding_links":["https://github.com/sponsors/mattphillips"],"categories":["Packages","Commit hooks"],"sub_categories":["Library extensions","Server-rendered React"],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003ejest-chain\u003c/h1\u003e\n\n🃏⛓\n\nChain Jest matchers together to create one powerful assertion\n\n\u003c/div\u003e\n\n\u003chr /\u003e\n\n[![Build Status](https://img.shields.io/github/workflow/status/mattphillips/jest-chain/GitHub%20CI/main?style=flat-square)](https://github.com/mattphillips/jest-chain/actions/workflows/ci.yaml)\n[![Code Coverage](https://img.shields.io/codecov/c/github/mattphillips/jest-chain.svg?style=flat-square)](https://codecov.io/github/mattphillips/jest-chain)\n[![version](https://img.shields.io/npm/v/jest-chain.svg?style=flat-square)](https://www.npmjs.com/package/jest-chain)\n[![downloads](https://img.shields.io/npm/dm/jest-chain.svg?style=flat-square)](http://npm-stat.com/charts.html?package=jest-chain\u0026from=2017-09-14)\n[![MIT License](https://img.shields.io/npm/l/jest-chain.svg?style=flat-square)](https://github.com/mattphillips/jest-chain/blob/master/LICENSE)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n[![Roadmap](https://img.shields.io/badge/%F0%9F%93%94-roadmap-CD9523.svg?style=flat-square)](https://github.com/mattphillips/jest-chain/blob/master/docs/ROADMAP.md)\n[![Examples](https://img.shields.io/badge/%F0%9F%92%A1-examples-ff615b.svg?style=flat-square)](https://github.com/mattphillips/jest-chain/blob/master/docs/EXAMPLES.md)\n\n- 🍸 Less code duplication\n- 🤗 Chain core and custom matchers together\n- 👾 Expressive assertions\n- 🚨 Fail fast assertions\n\n## Problem\n\nOften in [Jest](https://facebook.github.io/jest/) when you are writing tests you may want to perform multiple assertions on the\nsame variable. Currently to achieve this you have to write an individual `expect` for each\nassertion.\n\nFor example:\n\n```js\nit(\"add 1 and 1\", () =\u003e {\n  const actual = 1 + 1;\n  expect(actual).toBe(2);\n  expect(actual).toBeGreaterThan(1);\n  expect(actual).toBeLessThan(3);\n});\n```\n\nWith `jest-chain` this can instead be written by chaining the matchers together:\n\n```js\nit(\"add 1 and 1\", () =\u003e {\n  expect(1 + 1)\n    .toBe(2)\n    .toBeGreaterThan(1)\n    .toBeLessThan(3);\n});\n```\n\n## Installation\n\nWith npm:\n\n```sh\nnpm install --save-dev jest-chain\n```\n\nWith yarn:\n\n```sh\nyarn add -D jest-chain\n```\n\n## Setup\n\nAdd `jest-chain` to your Jest `setupFilesAfterEnv` configuration. [See for help](https://jestjs.io/docs/en/next/configuration#setupfilesafterenv-array)\n\n### Jest \u003ev24\n\n```json\n\"jest\": {\n  \"setupFilesAfterEnv\": [\"jest-chain\"]\n}\n```\n\n### Jest \u003cv23\n\n```json\n\"jest\": {\n  \"setupTestFrameworkScriptFile\": \"jest-chain\"\n}\n```\n\nIf you are already using another test framework, like [jest-extended](https://github.com/jest-community/jest-extended), then you should create a test setup file and `require` each of the frameworks you are using (including `jest-chain` 😉)\n\nFor example:\n\n```js\n// ./testSetup.js\nrequire(\"jest-chain\");\nrequire(\"any other test framework libraries you are using\");\n```\n\nThen in your Jest config:\n\n```json\n\"jest\": {\n  \"setupTestFrameworkScriptFile\": \"./testSetup.js\"\n}\n```\n\n### Configure Typescript\n\nAdd the following entry to your tsconfig to enable Typescript support.\n\n```json\n  \"files\": [\"node_modules/jest-chain/types/index.d.ts\"],\n```\n\n[Example typescript project here](/example/typescript)\n\n**Note:** if you are using any other custom matcher libraries then make sure that the `jest-chain` type import is at the bottom so that the types can chain core matchers with your customer matcher library.\n\n## Usage\n\nUse Jest's `expect` function the same way you would normally but with the ability to chain any\nmatcher to another, including nested matchers such as: `.not`, `.resolves` and `.rejects`.\n\n`jest-chain` supports custom Jest matchers, like [jest-extended](https://github.com/jest-community/jest-extended), in the usual way with `expect.extend(matcher)`.\nEach of these custom matchers are also chainable.\n\nSome examples:\n\n```js\nexpect([1, 2, 3]).toHaveLength(3).toEqual([1, 2, 3]);\n```\n\n```js\n// with jest-extended\nexpect([1, 2, 3]).toBeArray().toBeArrayOfSize(3).toEqual([1, 2, 3]).toIncludeAnyMembers([1, 2]);\n\nexpect(100).toBePositive().toBeGreaterThan(99).toBeLessThan(101).toBeNumber().not.toBeNaN().toBe(100);\n\nexpect(\"hello world\")\n  .toBeString()\n  .toEqualCaseInsensitive(\"HELLO WORLD\")\n  .toStartWith(\"hello\")\n  .toEndWith(\"world\")\n  .not.toInclude(\"!\")\n  .toBe(\"hello world\");\n```\n\n**Matcher failures will fail fast from left to right, they have no impact on each other. 🎉**\n\n_Note: `jest-chain` does not currently support asymmetric matcher chaining, if you want this please send a PR_ 😊\n\n## LICENSE\n\n[MIT](/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattphillips%2Fjest-chain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattphillips%2Fjest-chain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattphillips%2Fjest-chain/lists"}