{"id":24529816,"url":"https://github.com/learosema/understanding-jest-mocks-typescript","last_synced_at":"2025-07-31T19:03:59.321Z","repository":{"id":39914276,"uuid":"229812529","full_name":"learosema/understanding-jest-mocks-typescript","owner":"learosema","description":"`jest.mock` confusion in TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-05T03:34:48.000Z","size":255,"stargazers_count":1,"open_issues_count":19,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-22T07:52:42.823Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/learosema.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":"2019-12-23T19:38:09.000Z","updated_at":"2023-03-05T03:24:44.000Z","dependencies_parsed_at":"2023-02-03T09:01:41.999Z","dependency_job_id":null,"html_url":"https://github.com/learosema/understanding-jest-mocks-typescript","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/learosema%2Funderstanding-jest-mocks-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learosema%2Funderstanding-jest-mocks-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learosema%2Funderstanding-jest-mocks-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learosema%2Funderstanding-jest-mocks-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/learosema","download_url":"https://codeload.github.com/learosema/understanding-jest-mocks-typescript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243770589,"owners_count":20345317,"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":[],"created_at":"2025-01-22T07:52:51.532Z","updated_at":"2025-03-15T18:20:28.658Z","avatar_url":"https://github.com/learosema.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Understanding Jest mocks in TypeScript\n\n## Why\n\nI created this repository because I was totally confused about how to use `jest.mock()` with TypeScript.\n\n## Confusions\n\nFirst, I struggled with the official guide about how to [use TypeScript in jest](https://jestjs.io/docs/en/getting-started#using-typescript), which involved installing lots of babel packages and configuring it inside your project.\n\nThe next struggle was about how to access properties of the mock object from inside the test without type errors. \nOne thing that worked was telling TypeScript to shut up via `//@ts-ignore`. But that didn't feel quite right.\n\nThe solution was to use [ts-jest](https://github.com/kulshekhar/ts-jest) and the `mocked` utility function from `ts-jest/utils` as described in this [Github issue comment](https://github.com/kulshekhar/ts-jest/issues/576#issuecomment-458178545). Saved my day :).\n\n## The program to be tested\n\nThis program consumes the `people/:id` endpoint from the [Star Wars API](http://swapi.co) and logs the output.\nThe test tests the implementation of the api function `getPeople`. \nThe fetch call in the test is mocked.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearosema%2Funderstanding-jest-mocks-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flearosema%2Funderstanding-jest-mocks-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearosema%2Funderstanding-jest-mocks-typescript/lists"}