{"id":13494003,"url":"https://github.com/sapegin/jest-cheat-sheet","last_synced_at":"2026-02-11T11:03:12.500Z","repository":{"id":38619762,"uuid":"84238782","full_name":"sapegin/jest-cheat-sheet","owner":"sapegin","description":"Jest cheat sheet","archived":false,"fork":false,"pushed_at":"2024-07-13T09:39:18.000Z","size":98,"stargazers_count":5340,"open_issues_count":0,"forks_count":705,"subscribers_count":68,"default_branch":"master","last_synced_at":"2024-10-29T15:31:13.276Z","etag":null,"topics":["cheatsheet","jest","testing"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sapegin.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"License.md","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},"funding":{"github":"sapegin","ko_fi":"sapegin","custom":["https://www.buymeacoffee.com/sapegin"]}},"created_at":"2017-03-07T19:40:00.000Z","updated_at":"2024-10-22T04:53:57.000Z","dependencies_parsed_at":"2023-02-10T02:15:38.348Z","dependency_job_id":"ddece7eb-3c58-458a-935b-96280aa54df6","html_url":"https://github.com/sapegin/jest-cheat-sheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sapegin/jest-cheat-sheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapegin%2Fjest-cheat-sheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapegin%2Fjest-cheat-sheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapegin%2Fjest-cheat-sheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapegin%2Fjest-cheat-sheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sapegin","download_url":"https://codeload.github.com/sapegin/jest-cheat-sheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapegin%2Fjest-cheat-sheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270807934,"owners_count":24649346,"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","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["cheatsheet","jest","testing"],"created_at":"2024-07-31T19:01:20.822Z","updated_at":"2026-02-11T11:03:12.482Z","avatar_url":"https://github.com/sapegin.png","language":null,"funding_links":["https://github.com/sponsors/sapegin","https://ko-fi.com/sapegin","https://www.buymeacoffee.com/sapegin"],"categories":["Others","JavaScript","Resources","Test","Front-End Development","You may also like"],"sub_categories":["Cheat Sheet","Debug","Jest","Presets","Miscellaneous"],"readme":"\u003c!--\nTo update TOC run:\n  npx markdown-toc --maxdepth 3 -i Readme.md\n\nTo reformat run:\n  npx prettier --print-width 100 --single-quote --no-semi --prose-wrap never --write Readme.md\n--\u003e\n\n\u003cdiv align=\"center\" markdown=\"1\"\u003e\n\n\u003cimg src=\"https://raw.githubusercontent.com/facebook/jest/main/website/static/img/jest-readme-headline.png\" width=\"200\"\u003e\n\n\u003ch1\u003eJest cheat sheet\u003c/h1\u003e\n\n\u003c/div\u003e\n\n_Migrating to Vitest? [Check out Vitest cheat sheet](https://github.com/sapegin/vitest-cheat-sheet)._\n\n_I recommend [Mrm](https://github.com/sapegin/mrm/tree/master/packages/mrm-task-jest) and [jest-codemods](https://github.com/skovhus/jest-codemods) for single-command Jest installation and easy migration from other frameworks._\n\n[![Washing your code. A book on clean code for frontend developers](https://sapegin.me/images/washing-code-github.jpg)](https://sapegin.me/book/)\n\n## Table of contents\n\n\u003c!-- toc --\u003e\n\n- [Test structure](#test-structure)\n- [Matchers](#matchers)\n  - [Basic matchers](#basic-matchers)\n  - [Truthiness](#truthiness)\n  - [Numbers](#numbers)\n  - [Strings](#strings)\n  - [Arrays](#arrays)\n  - [Objects](#objects)\n  - [Exceptions](#exceptions)\n  - [Snapshots](#snapshots)\n  - [Mock functions](#mock-functions)\n  - [Misc](#misc)\n  - [Promise matchers (Jest 20+)](#promise-matchers-jest-20)\n- [Async tests](#async-tests)\n  - [async/await](#asyncawait)\n  - [Promises](#promises)\n  - [done() callback](#done-callback)\n- [Mocks](#mocks)\n  - [Mock functions](#mock-functions-1)\n  - [Returning, resolving and rejecting values](#returning-resolving-and-rejecting-values)\n  - [Mock modules using `jest.mock` method](#mock-modules-using-jestmock-method)\n  - [Mock modules using a mock file](#mock-modules-using-a-mock-file)\n  - [Mock object methods](#mock-object-methods)\n  - [Mock getters and setters (Jest 22.1.0+)](#mock-getters-and-setters-jest-2210)\n  - [Mock getters and setters](#mock-getters-and-setters)\n  - [Clearing and restoring mocks](#clearing-and-restoring-mocks)\n  - [Accessing the original module when using mocks](#accessing-the-original-module-when-using-mocks)\n  - [Timer mocks](#timer-mocks)\n- [Data-driven tests (Jest 23+)](#data-driven-tests-jest-23)\n- [Skipping tests](#skipping-tests)\n- [Testing modules with side effects](#testing-modules-with-side-effects)\n- [Usage with Babel and TypeScript](#usage-with-babel-and-typescript)\n- [Resources](#resources)\n- [You may also like](#you-may-also-like)\n- [Contributing](#contributing)\n- [Sponsoring](#sponsoring)\n- [Author and license](#author-and-license)\n\n\u003c!-- tocstop --\u003e\n\n## Test structure\n\n```js\ndescribe('makePoniesPink', () =\u003e {\n  beforeAll(() =\u003e {\n    /* Runs before all tests */\n  })\n  afterAll(() =\u003e {\n    /* Runs after all tests */\n  })\n  beforeEach(() =\u003e {\n    /* Runs before each test */\n  })\n  afterEach(() =\u003e {\n    /* Runs after each test */\n  })\n\n  test('make each pony pink', () =\u003e {\n    const actual = fn(['Alice', 'Bob', 'Eve'])\n    expect(actual).toEqual(['Pink Alice', 'Pink Bob', 'Pink Eve'])\n  })\n})\n```\n\n## Matchers\n\n[Using matchers](http://jestjs.io/docs/en/using-matchers), [matchers docs](https://jestjs.io/docs/en/expect)\n\n### Basic matchers\n\n```js\nexpect(42).toBe(42) // Strict equality (===)\nexpect(42).not.toBe(3) // Strict equality (!==)\nexpect([1, 2]).toEqual([1, 2]) // Deep equality\nexpect({ a: undefined, b: 2 }).toEqual({ b: 2 }) // Deep equality\nexpect({ a: undefined, b: 2 }).not.toStrictEqual({ b: 2 }) // Strict equality (Jest 23+)\n```\n\n### Truthiness\n\n```js\n// Matches anything that an if statement treats as true (true, 1, 'hello', {}, [], 5.3)\nexpect('foo').toBeTruthy()\n// Matches anything that an if statement treats as false (false, 0, '', null, undefined, NaN)\nexpect('').toBeFalsy()\n// Matches only null\nexpect(null).toBeNull()\n// Matches only undefined\nexpect(undefined).toBeUndefined()\n// The opposite of toBeUndefined\nexpect(7).toBeDefined()\n// Matches true or false\nexpect(true).toEqual(expect.any(Boolean))\n```\n\n### Numbers\n\n```js\nexpect(2).toBeGreaterThan(1)\nexpect(1).toBeGreaterThanOrEqual(1)\nexpect(1).toBeLessThan(2)\nexpect(1).toBeLessThanOrEqual(1)\nexpect(0.2 + 0.1).toBeCloseTo(0.3, 5)\nexpect(NaN).toEqual(expect.any(Number))\n```\n\n### Strings\n\n```js\nexpect('long string').toMatch('str')\nexpect('string').toEqual(expect.any(String))\nexpect('coffee').toMatch(/ff/)\nexpect('pizza').not.toMatch('coffee')\nexpect(['pizza', 'coffee']).toEqual([expect.stringContaining('zz'), expect.stringMatching(/ff/)])\n```\n\n### Arrays\n\n```js\nexpect([]).toEqual(expect.any(Array))\nexpect(['Alice', 'Bob', 'Eve']).toHaveLength(3)\nexpect(['Alice', 'Bob', 'Eve']).toContain('Alice')\nexpect([{ a: 1 }, { a: 2 }]).toContainEqual({ a: 1 })\nexpect(['Alice', 'Bob', 'Eve']).toEqual(expect.arrayContaining(['Alice', 'Bob']))\n```\n\n### Objects\n\n```js\nexpect({ a: 1 }).toHaveProperty('a')\nexpect({ a: 1 }).toHaveProperty('a', 1)\nexpect({ a: { b: 1 } }).toHaveProperty('a.b')\nexpect({ a: 1, b: 2 }).toMatchObject({ a: 1 })\nexpect({ a: 1, b: 2 }).toMatchObject({\n  a: expect.any(Number),\n  b: expect.any(Number),\n})\nexpect([{ a: 1 }, { b: 2 }]).toEqual([\n  expect.objectContaining({ a: expect.any(Number) }),\n  expect.anything(),\n])\n```\n\n### Exceptions\n\n```js\n// const fn = () =\u003e { throw new Error('Out of cheese!') }\nexpect(fn).toThrow()\nexpect(fn).toThrow('Out of cheese')\nexpect(fn).toThrowErrorMatchingSnapshot()\n```\n\n\u003cdetails\u003e\n  \u003csummary\u003eAliases\u003c/summary\u003e\n\n- `toThrowError` → `toThrow`\n  \u003c/details\u003e\n\n### Snapshots\n\n```js\nexpect(node).toMatchSnapshot()\n// Jest 23+\nexpect(user).toMatchSnapshot({\n  date: expect.any(Date),\n})\nexpect(user).toMatchInlineSnapshot()\n```\n\n### Mock functions\n\n```js\n// const fn = jest.fn()\n// const fn = jest.fn().mockName('Unicorn') -- named mock, Jest 22+\nexpect(fn).toBeCalled() // Function was called\nexpect(fn).not.toBeCalled() // Function was *not* called\nexpect(fn).toHaveBeenCalledTimes(1) // Function was called only once\nexpect(fn).toBeCalledWith(arg1, arg2) // Any of calls was with these arguments\nexpect(fn).toHaveBeenLastCalledWith(arg1, arg2) // Last call was with these arguments\nexpect(fn).toHaveBeenNthCalledWith(callNumber, args) // Nth call was with these arguments (Jest 23+)\nexpect(fn).toHaveReturnedTimes(2) // Function was returned without throwing an error (Jest 23+)\nexpect(fn).toHaveReturnedWith(value) // Function returned a value (Jest 23+)\nexpect(fn).toHaveLastReturnedWith(value) // Last function call returned a value (Jest 23+)\nexpect(fn).toHaveNthReturnedWith(value) // Nth function call returned a value (Jest 23+)\nexpect(fn.mock.calls).toEqual([\n  ['first', 'call', 'args'],\n  ['second', 'call', 'args'],\n]) // Multiple calls\nexpect(fn.mock.calls[0][0]).toBe(2) // fn.mock.calls[0][0] — the first argument of the first call\n```\n\n\u003cdetails\u003e\n  \u003csummary\u003eAliases\u003c/summary\u003e\n\n- `toBeCalled` → `toHaveBeenCalled`\n- `toBeCalledWith` → `toHaveBeenCalledWith`\n- `lastCalledWith` → `toHaveBeenLastCalledWith`\n- `nthCalledWith` → `toHaveBeenNthCalledWith`\n- `toReturnTimes` → `toHaveReturnedTimes`\n- `toReturnWith` → `toHaveReturnedWith`\n- `lastReturnedWith` → `toHaveLastReturnedWith`\n- `nthReturnedWith` → `toHaveNthReturnedWith`\n  \u003c/details\u003e\n\n### Misc\n\n```js\nexpect(new A()).toBeInstanceOf(A)\nexpect(() =\u003e {}).toEqual(expect.any(Function))\nexpect('pizza').toEqual(expect.anything())\n```\n\n### Promise matchers (Jest 20+)\n\n```js\ntest('resolve to lemon', () =\u003e {\n  expect.assertions(1)\n  // Make sure to add a return statement\n  return expect(Promise.resolve('lemon')).resolves.toBe('lemon')\n  return expect(Promise.reject('octopus')).rejects.toBeDefined()\n  return expect(Promise.reject(Error('pizza'))).rejects.toThrow()\n})\n```\n\nOr with async/await:\n\n```js\ntest('resolve to lemon', async () =\u003e {\n  expect.assertions(2)\n  await expect(Promise.resolve('lemon')).resolves.toBe('lemon')\n  await expect(Promise.resolve('lemon')).resolves.not.toBe('octopus')\n})\n```\n\n[resolves docs](https://jestjs.io/docs/en/expect#resolves)\n\n## Async tests\n\nSee [more examples](https://jestjs.io/docs/en/tutorial-async) in Jest docs.\n\nIt’s a good practice to specify a number of expected assertions in async tests, so the test will fail if your assertions weren’t called at all.\n\n```js\ntest('async test', () =\u003e {\n  expect.assertions(3) // Exactly three assertions are called during a test\n  // OR\n  expect.hasAssertions() // At least one assertion is called during a test\n\n  // Your async tests\n})\n```\n\nYou can also do this per file, outside any `describe` and `test`:\n\n```js\nbeforeEach(expect.hasAssertions)\n```\n\nThis will verify the presence of at least one assertion per test case. It also plays nice with more specific `expect.assertions(3)` declarations.\n\nIn addition, you can enforce it globally, across all test files (instead of having to repeat per file) by adding the exact same line into one of the scripts referenced by the `setupFilesAfterEnv` configuration option. (For example, `setupTests.ts` and that is referenced via a `setupFilesAfterEnv: ['\u003crootDir\u003e/setupTests.ts']` entry in `jest.config.ts`.)\n\n### async/await\n\n```js\ntest('async test', async () =\u003e {\n  expect.assertions(1)\n  const result = await runAsyncOperation()\n  expect(result).toBe(true)\n})\n```\n\n### Promises\n\n_Return_ a Promise from your test:\n\n```js\ntest('async test', () =\u003e {\n  expect.assertions(1)\n  return runAsyncOperation().then((result) =\u003e {\n    expect(result).toBe(true)\n  })\n})\n```\n\n### done() callback\n\nWrap your assertions in try/catch block, otherwise Jest will ignore failures:\n\n```js\ntest('async test', (done) =\u003e {\n  expect.assertions(1)\n  runAsyncOperation()\n  setTimeout(() =\u003e {\n    try {\n      const result = getAsyncOperationResult()\n      expect(result).toBe(true)\n      done()\n    } catch (err) {\n      done.fail(err)\n    }\n  })\n})\n```\n\n## Mocks\n\n### Mock functions\n\n```js\ntest('call the callback', () =\u003e {\n  const callback = jest.fn()\n  fn(callback)\n  expect(callback).toBeCalled()\n  expect(callback.mock.calls[0][1].baz).toBe('pizza') // Second argument of the first call\n  // Match the first and the last arguments but ignore the second argument\n  expect(callback).toHaveBeenLastCalledWith('meal', expect.anything(), 'margarita')\n})\n```\n\nYou can also use snapshots:\n\n```js\ntest('call the callback', () =\u003e {\n  const callback = jest.fn().mockName('Unicorn') // mockName is available in Jest 22+\n  fn(callback)\n  expect(callback).toMatchSnapshot()\n  // -\u003e\n  // [MockFunction Unicorn] {\n  //   \"calls\": Array [\n  // ...\n})\n```\n\nAnd pass an implementation to `jest.fn` function:\n\n```js\nconst callback = jest.fn(() =\u003e true)\n```\n\n[Mock functions docs](https://jestjs.io/docs/en/mock-function-api)\n\n### Returning, resolving and rejecting values\n\nYour mocks can return values:\n\n```js\nconst callback = jest.fn().mockReturnValue(true)\nconst callbackOnce = jest.fn().mockReturnValueOnce(true)\n```\n\nOr resolve values:\n\n```js\nconst promise = jest.fn().mockResolvedValue(true)\nconst promiseOnce = jest.fn().mockResolvedValueOnce(true)\n```\n\nThey can even reject values:\n\n```js\nconst failedPromise = jest.fn().mockRejectedValue('Error')\nconst failedPromiseOnce = jest.fn().mockRejectedValueOnce('Error')\n```\n\nYou can even combine these:\n\n```js\nconst callback = jest.fn().mockReturnValueOnce(false).mockReturnValue(true)\n\n// -\u003e\n//  call 1: false\n//  call 2+: true\n```\n\n### Mock modules using `jest.mock` method\n\n```js\njest.mock('lodash/memoize', () =\u003e (a) =\u003e a) // The original lodash/memoize should exist\njest.mock('lodash/memoize', () =\u003e (a) =\u003e a, { virtual: true }) // The original lodash/memoize isn’t required\n```\n\n[jest.mock docs](https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options)\n\n\u003e [!NOTE]  \n\u003e When using `babel-jest`, calls to `jest.mock` will automatically be hoisted to the top of the code block. Use `jest.doMock` if you want to explicitly avoid this behavior.\n\n### Mock modules using a mock file\n\n1.  Create a file like `__mocks__/lodash/memoize.js`:\n\n    ```js\n    module.exports = (a) =\u003e a\n    ```\n\n2.  Add to your test:\n\n    ```js\n    jest.mock('lodash/memoize')\n    ```\n\n\u003e [!NOTE]  \n\u003e When using `babel-jest`, calls to `jest.mock` will automatically be hoisted to the top of the code block. Use `jest.doMock` if you want to explicitly avoid this behavior.\n\n[Manual mocks docs](https://jestjs.io/docs/en/manual-mocks)\n\n### Mock object methods\n\n```js\nconst spy = jest.spyOn(console, 'log').mockImplementation(() =\u003e {})\nexpect(console.log.mock.calls).toEqual([['dope'], ['nope']])\nspy.mockRestore()\n```\n\n```js\nconst spy = jest.spyOn(ajax, 'request').mockImplementation(() =\u003e Promise.resolve({ success: true }))\nexpect(spy).toHaveBeenCalled()\nspy.mockRestore()\n```\n\n### Mock getters and setters (Jest 22.1.0+)\n\n```js\nconst location = {}\nconst getTitle = jest.spyOn(location, 'title', 'get').mockImplementation(() =\u003e 'pizza')\nconst setTitle = jest.spyOn(location, 'title', 'set').mockImplementation(() =\u003e {})\n```\n\n### Mock getters and setters\n\n```js\nconst getTitle = jest.fn(() =\u003e 'pizza')\nconst setTitle = jest.fn()\nconst location = {}\nObject.defineProperty(location, 'title', {\n  get: getTitle,\n  set: setTitle,\n})\n```\n\n### Clearing and restoring mocks\n\nFor one mock:\n\n```js\nfn.mockClear() // Clears mock usage date (fn.mock.calls, fn.mock.instances)\nfn.mockReset() // Clears and removes any mocked return values or implementations\nfn.mockRestore() // Resets and restores the initial implementation\n```\n\n\u003e [!NOTE]  \n\u003e The `mockRestore` works only with mocks created by `jest.spyOn`.\n\nFor all mocks:\n\n```js\njest.clearAllMocks()\njest.resetAllMocks()\njest.restoreAllMocks()\n```\n\n### Accessing the original module when using mocks\n\n```js\njest.mock('fs')\nconst fs = require('fs') // Mocked module\nconst fs = require.requireActual('fs') // Original module\n```\n\n### Timer mocks\n\nWrite synchronous test for code that uses native timer functions (`setTimeout`, `setInterval`, `clearTimeout`, `clearInterval`).\n\n```js\n// Enable fake timers\njest.useFakeTimers()\n\ntest('kill the time', () =\u003e {\n  const callback = jest.fn()\n\n  // Run some code that uses setTimeout or setInterval\n  const actual = someFunctionThatUseTimers(callback)\n\n  // Fast-forward until all timers have been executed\n  jest.runAllTimers()\n\n  // Check the results synchronously\n  expect(callback).toHaveBeenCalledTimes(1)\n})\n```\n\nOr adjust timers by time with [advanceTimersByTime()](https://jestjs.io/docs/en/timer-mocks#advance-timers-by-time):\n\n```js\n// Enable fake timers\njest.useFakeTimers()\n\ntest('kill the time', () =\u003e {\n  const callback = jest.fn()\n\n  // Run some code that uses setTimeout or setInterval\n  const actual = someFunctionThatUseTimers(callback)\n\n  // Fast-forward for 250 ms\n  jest.advanceTimersByTime(250)\n\n  // Check the results synchronously\n  expect(callback).toHaveBeenCalledTimes(1)\n})\n```\n\nUse [jest.runOnlyPendingTimers()](https://jestjs.io/docs/en/timer-mocks#run-pending-timers) for special cases.\n\n\u003e [!NOTE]  \n\u003e You should call `jest.useFakeTimers()` in your test case to use other fake timer methods.\n\n## Data-driven tests (Jest 23+)\n\nRun the same test with different data:\n\n```js\ntest.each([\n  [1, 1, 2],\n  [1, 2, 3],\n  [2, 1, 3],\n])('.add(%s, %s)', (a, b, expected) =\u003e {\n  expect(a + b).toBe(expected)\n})\n```\n\nOr the same using template literals:\n\n```js\ntest.each`\n  a    | b    | expected\n  ${1} | ${1} | ${2}\n  ${1} | ${2} | ${3}\n  ${2} | ${1} | ${3}\n`('returns $expected when $a is added $b', ({ a, b, expected }) =\u003e {\n  expect(a + b).toBe(expected)\n})\n```\n\nOr on `describe` level:\n\n```js\ndescribe.each([['mobile'], ['tablet'], ['desktop']])('checkout flow on %s', (viewport) =\u003e {\n  test('displays success page', () =\u003e {\n    //\n  })\n})\n```\n\n[describe.each() docs](https://jestjs.io/docs/en/api#describeeachtablename-fn-timeout), [test.each() docs](https://jestjs.io/docs/en/api#testeachtablename-fn-timeout),\n\n## Skipping tests\n\nDon’t run these tests:\n\n```js\ndescribe.skip('makePoniesPink'...\ntests.skip('make each pony pink'...\n```\n\nRun only these tests:\n\n```js\ndescribe.only('makePoniesPink'...\ntests.only('make each pony pink'...\n```\n\n## Testing modules with side effects\n\nNode.js and Jest will cache modules you `require`. To test modules with side effects you’ll need to reset the module registry between tests:\n\n```js\nconst modulePath = '../module-to-test'\n\nafterEach(() =\u003e {\n  jest.resetModules()\n})\n\ntest('first test', () =\u003e {\n  // Prepare conditions for the first test\n  const result = require(modulePath)\n  expect(result).toMatchSnapshot()\n})\n\ntest('second text', () =\u003e {\n  // Prepare conditions for the second test\n  const fn = () =\u003e require(modulePath)\n  expect(fn).toThrow()\n})\n```\n\n## Usage with Babel and TypeScript\n\nAdd [babel-jest](https://github.com/facebook/jest/tree/master/packages/babel-jest) or [ts-jest](https://github.com/kulshekhar/ts-jest). Check their docs for installation instructions.\n\n## Resources\n\n- [Jest site](https://facebook.github.io/jest/)\n- [Modern React testing, part 1: best practices](https://blog.sapegin.me/all/react-testing-1-best-practices/) by Artem Sapegin\n- [Modern React testing, part 2: Jest and Enzyme](https://blog.sapegin.me/all/react-testing-2-jest-and-enzyme/) by Artem Sapegin\n- [Modern React testing, part 3: Jest and React Testing Library](https://blog.sapegin.me/all/react-testing-3-jest-and-react-testing-library/) by Artem Sapegin\n- [Modern React testing, part 4: Cypress and Cypress Testing Library](https://sapegin.me/blog/react-testing-4-cypress/) by Artem Sapegin\n- [Modern React testing, part 5: Playwright](https://sapegin.me/blog/react-testing-5-playwright/) by Artem Sapegin\n- [React Testing Examples](https://react-testing-examples.com/)\n- [Testing React Applications](https://youtu.be/59Ndb3YkLKA) by Max Stoiber\n- [Effective Snapshot Testing](https://blog.kentcdodds.com/effective-snapshot-testing-e0d1a2c28eca) by Kent C. Dodds\n- [Migrating to Jest](https://medium.com/@kentcdodds/migrating-to-jest-881f75366e7e#.pc4s5ut6z) by Kent C. Dodds\n- [Migrating AVA to Jest](http://browniefed.com/blog/migrating-ava-to-jest/) by Jason Brown\n- [How to Test React and MobX with Jest](https://semaphoreci.com/community/tutorials/how-to-test-react-and-mobx-with-jest) by Will Stern\n- [Testing React Intl components with Jest and Enzyme](https://medium.com/@sapegin/testing-react-intl-components-with-jest-and-enzyme-f9d43d9c923e) by Artem Sapegin\n- [Testing with Jest: 15 Awesome Tips and Tricks](https://medium.com/@stipsan/testing-with-jest-15-awesome-tips-and-tricks-42150ec4c262) by Stian Didriksen\n- Taking Advantage of Jest Matchers by Ben McCormick: [Part 1](https://benmccormick.org/2017/08/15/jest-matchers-1/), [Part 2](https://benmccormick.org/2017/09/04/jest-matchers-2/)\n\n---\n\n## You may also like\n\n- [Opinionated list of React components](https://github.com/sapegin/react-components)\n\n## Contributing\n\nImprovements are welcome! Open an issue or send a pull request.\n\n## Sponsoring\n\nThis software has been developed with lots of coffee, buy me one more cup to keep it going.\n\n\u003ca href=\"https://www.buymeacoffee.com/sapegin\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/lato-orange.png\" alt=\"Buy Me A Coffee\" height=\"51\" width=\"217\" \u003e\u003c/a\u003e\n\n## Author and license\n\n[Artem Sapegin](https://sapegin.me/), a frontend engineer at [Stage+](https://www.stage-plus.com) and the creator of [React Styleguidist](https://react-styleguidist.js.org/). I also write about frontend at [my blog](https://sapegin.me/blog/).\n\nCC0 1.0 Universal license, see the included [License.md](/License.md) file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsapegin%2Fjest-cheat-sheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsapegin%2Fjest-cheat-sheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsapegin%2Fjest-cheat-sheet/lists"}