{"id":19256678,"url":"https://github.com/karlhadwen/jest-playground","last_synced_at":"2025-10-30T19:33:16.866Z","repository":{"id":39191218,"uuid":"249052976","full_name":"karlhadwen/jest-playground","owner":"karlhadwen","description":"Playing around with Jest - Subscribe to my YouTube channel: https://bit.ly/CognitiveSurge","archived":false,"fork":false,"pushed_at":"2023-01-05T10:28:58.000Z","size":726,"stargazers_count":23,"open_issues_count":21,"forks_count":19,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T14:12:56.536Z","etag":null,"topics":["jest","jest-mocking","jest-playground","jest-snapshots","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karlhadwen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-21T20:13:03.000Z","updated_at":"2024-04-03T04:06:33.000Z","dependencies_parsed_at":"2023-02-04T01:17:27.416Z","dependency_job_id":null,"html_url":"https://github.com/karlhadwen/jest-playground","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karlhadwen%2Fjest-playground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karlhadwen%2Fjest-playground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karlhadwen%2Fjest-playground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karlhadwen%2Fjest-playground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karlhadwen","download_url":"https://codeload.github.com/karlhadwen/jest-playground/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250080541,"owners_count":21371525,"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":["jest","jest-mocking","jest-playground","jest-snapshots","react"],"created_at":"2024-11-09T19:06:34.853Z","updated_at":"2025-10-30T19:33:16.781Z","avatar_url":"https://github.com/karlhadwen.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Jest playground\n```\nimport pizzas from '../data';\n\n// very basic test to notify the user if our pizza data has changed\ntest('the pizza data is correct', () =\u003e {\n  expect(pizzas).toMatchSnapshot();\n  expect(pizzas).toHaveLength(4);\n  expect(pizzas.map(pizza =\u003e pizza.name)).toEqual([\n    'Chicago Pizza',\n    'Neapolitan Pizza',\n    'New York Pizza',\n    'Sicilian Pizza',\n  ]);\n});\n\n// let's test that each item in the pizza data has the correct properties\nfor (let i = 0; i \u003c pizzas.length; i += 1) {\n  it(`pizza[${i}] should have properties (id, name, image, desc, price)`, () =\u003e {\n    expect(pizzas[i]).toHaveProperty('id');\n    expect(pizzas[i]).toHaveProperty('name');\n    expect(pizzas[i]).toHaveProperty('image');\n    expect(pizzas[i]).toHaveProperty('desc');\n    expect(pizzas[i]).toHaveProperty('price');\n  });\n}\n\n// default jest mock function\ntest('mock implementation of a basic function', () =\u003e {\n  const mock = jest.fn(() =\u003e 'I am a mock function');\n\n  expect(mock('Calling my mock function!')).toBe('I am a mock function');\n  expect(mock).toHaveBeenCalledWith('Calling my mock function!');\n});\n\n// let's mock the return value and test calls\ntest('mock return value of a function one time', () =\u003e {\n  const mock = jest.fn();\n\n  // we can chain these!\n  mock.mockReturnValueOnce('Hello').mockReturnValueOnce('there!');\n\n  mock(); // first call 'Hello'\n  mock(); // second call 'there!'\n\n  expect(mock).toHaveBeenCalledTimes(2); // we know it's been called two times\n\n  mock('Hello', 'there', 'Steve'); // call it with 3 different arguments\n  expect(mock).toHaveBeenCalledWith('Hello', 'there', 'Steve');\n\n  mock('Steve'); // called with 1 argument\n  expect(mock).toHaveBeenLastCalledWith('Steve');\n});\n\n// let's mock the return value\n// difference between mockReturnValue \u0026 mockImplementation\ntest('mock implementation of a function', () =\u003e {\n  const mock = jest.fn().mockImplementation(() =\u003e 'United Kingdom');\n  expect(mock('Location')).toBe('United Kingdom');\n  expect(mock).toHaveBeenCalledWith('Location');\n});\n\n// spying on a single function of an imported module, we can spy on its usage\n// by default the original function gets called, we can change this\ntest('spying using original implementation', () =\u003e {\n  const pizza = {\n    name: n =\u003e `Pizza name: ${n}`,\n  };\n  const spy = jest.spyOn(pizza, 'name');\n  expect(pizza.name('Cheese')).toBe('Pizza name: Cheese');\n  expect(spy).toHaveBeenCalledWith('Cheese');\n});\n\n// we can mock the implementation of a function from a module\ntest('spying using mockImplementation', () =\u003e {\n  const pizza = {\n    name: n =\u003e `Pizza name: ${n}`,\n  };\n  const spy = jest.spyOn(pizza, 'name');\n  spy.mockImplementation(n =\u003e `Crazy pizza!`);\n\n  expect(pizza.name('Cheese')).toBe('Crazy pizza!');\n  spy.mockRestore(); // back to original implementation\n  expect(pizza.name('Cheese')).toBe('Pizza name: Cheese');\n});\n\n// let's test pizza return output\ntest('pizza returns new york pizza last', () =\u003e {\n  const pizza1 = pizzas[0];\n  const pizza2 = pizzas[1];\n  const pizza3 = pizzas[2];\n  const pizza = jest.fn(currentPizza =\u003e currentPizza.name);\n\n  pizza(pizza1); // chicago pizza\n  pizza(pizza2); // neapolitan pizza\n  pizza(pizza3); // new york pizza\n\n  expect(pizza).toHaveLastReturnedWith('New York Pizza');\n});\n\n// let's match some data against our object\ntest('pizza data has new york pizza and matches as an object', () =\u003e {\n  const newYorkPizza = {\n    id: 3,\n    name: 'New York Pizza',\n    image: '/images/ny-pizza.jpg',\n    desc:\n      'New York-style pizza has slices that are large and wide with a thin crust that is foldable yet crispy. It is traditionally topped with tomato sauce and mozzarella cheese.',\n    price: 8,\n  };\n  expect(pizzas[2]).toMatchObject(newYorkPizza);\n});\n\n// async example, always return a promise (can switch out resolves with reject)\ntest('expect a promise to resolve', async () =\u003e {\n  const user = {\n    getFullName: jest.fn(() =\u003e Promise.resolve('Karl Hadwen')),\n  };\n  await expect(user.getFullName('Karl Hadwen')).resolves.toBe('Karl Hadwen');\n});\n\ntest('expect a promise to reject', async () =\u003e {\n  const user = {\n    getFullName: jest.fn(() =\u003e\n      Promise.reject(new Error('Something went wrong'))\n    ),\n  };\n  await expect(user.getFullName('Karl Hadwen')).rejects.toThrow(\n    'Something went wrong'\n  );\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarlhadwen%2Fjest-playground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarlhadwen%2Fjest-playground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarlhadwen%2Fjest-playground/lists"}