{"id":25639574,"url":"https://github.com/userlike/joke","last_synced_at":"2025-10-19T02:45:33.007Z","repository":{"id":118600166,"uuid":"238806322","full_name":"userlike/joke","owner":"userlike","description":"Typesafe mock utility with minimal boilerplate for jest","archived":false,"fork":false,"pushed_at":"2025-02-09T12:16:19.000Z","size":1141,"stargazers_count":18,"open_issues_count":5,"forks_count":1,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-28T13:21:16.616Z","etag":null,"topics":["babel","jest","mock","typescript"],"latest_commit_sha":null,"homepage":"","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/userlike.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2020-02-06T23:32:43.000Z","updated_at":"2025-02-09T08:15:12.000Z","dependencies_parsed_at":"2023-05-20T07:30:24.597Z","dependency_job_id":null,"html_url":"https://github.com/userlike/joke","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/userlike%2Fjoke","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/userlike%2Fjoke/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/userlike%2Fjoke/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/userlike%2Fjoke/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/userlike","download_url":"https://codeload.github.com/userlike/joke/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248817626,"owners_count":21166280,"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":["babel","jest","mock","typescript"],"created_at":"2025-02-23T03:39:06.389Z","updated_at":"2025-10-19T02:45:27.975Z","avatar_url":"https://github.com/userlike.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 🍭 joke\n\n`joke` is a typesafe, boilerplate free version of `jest.mock`.\n\n## Advantages\n\n- Less boilerplate than `jest.mock`.\n- Type-safe imports. No more type-casting.\n- TS/JS Language Server recognizes them when moving files around.\n- Supports partial mocking (`mockSome`).\n\n## Usage\n\n### Install\n\n```\nnpm install --saveDev @userlike/joke @userlike/babel-plugin-joke\n```\n\n```\nyarn add -D @userlike/joke @userlike/babel-plugin-joke\n```\n\n### Babel config\n\nAdd `@userlike/babel-plugin-joke` to your babel plugins.\n\nIf you use [`ts-jest`](https://www.npmjs.com/package/ts-jest), please additionally refer to the \"Usage with `ts-jest`\" section below.\n\n### And use\n\n```typescript\nimport { mock } from \"@userlike/joke\";\n\nconst { fetchUser } = mock(import(\"./service\"));\n\nfetchUser.mockReturnValue(Promise.resolve({ id: 1, name: \"Jane Doe\" }));\n```\n\n---\n\n## Full mocking\n\nAuto-mock the whole module.\n\n```typescript\nimport { mock } from \"@userlike/joke\";\n\nconst { fetchUser } = mock(import(\"./service\"));\n\nfetchUser.mockReturnValue(Promise.resolve({ id: 1, name: \"Jane Doe\" }));\n```\n\n### Full mocking with partial implementation\n\nUse the second argument of `mock` to provide a partial implementation. Behind the scenes, it extends auto-mocked module with the given implementation using `Object.assign`.\n\n```typescript\nimport { mock } from \"@userlike/joke\";\n\nconst { fetchUser } = mock(import(\"./service\"), () =\u003e ({\n  fetchUser: () =\u003e Promise.resolve({ id: 1, name: \"Jane Doe\" })\n}));\n```\n\n---\n\n## Partial mocking\n\nWhen you need to mock a module partially and want to keep the rest of the module unmocked, you can use `mockSome`. Behind the scenes, it uses `jest.requireActual` by extending its actual implementation with the given implementation using `Object.assign`.\n\n```typescript\nimport { mockSome } from \"@userlike/joke\";\nimport { renderUser } from \"./my-component\";\n\n// fetchUser is mocked, getUserId is the real implementation\nconst { fetchUser, getUserId } = mockSome(import(\"./service\"), () =\u003e ({\n  fetchUser: jest.fn()\n}));\n\ntest(async () =\u003e {\n  const user = { id: 1, name: \"Jane Doe\" };\n  fetchUser.mockReturnValue(Promise.resolve(user));\n\n  await renderUser();\n\n  expect(document.getElementById(\"#user-id\").innerText).toBe(getUserId(user));\n});\n```\n\n---\n\n## Full replacement\n\nWhen you want to skip auto-mocking, you can use `mockAll`. It's equivalent to `jest.mock(module, moduleFactory)`.\n\n```typescript\nimport { mockAll } from \"@userlike/joke\";\nimport { renderUser } from \"./my-component\";\n\nconst { fetchUser } = mockAll(import(\"./service\"), () =\u003e ({\n  fetchUser: jest.fn()\n}));\n\ntest(async () =\u003e {\n  const user = { id: 1, name: \"Jane Doe\" };\n  fetchUser.mockReturnValue(Promise.resolve(user));\n\n  await renderUser();\n\n  expect(document.getElementById(\"#user-id\").innerText).toBe(getUserId(user));\n});\n```\n\n---\n\n## Usage with `ts-jest`\n\nIf you use [`ts-jest`](https://www.npmjs.com/package/ts-jest) instead of Babel, you need to additionally ensure each of the following:\n\n- That Babel preprocessing is enabled in your `ts-jest` configuration section.\n- That Babel is configured to use `joke` as a plugin.\n- That the `module` key of `tsconfig.json`'s `compilerOptions` is set to at least `es2020`, or `esnext` to support dynamic imports. You may also need to set `moduleResolution` to `node` for the general `import` syntax to work properly.\n- That the code is transpiled down to the JS syntax `jest` understands (you may use `@babel/preset-env` for that purpose).\n\n**Note**: if you don't want to modify your main `tsconfig.json` file, you can introduce a separate configuration named e.g. `tsconfig.tests.json`.\n\nExample Typescript configuration for tests:\n\n```typescript\n{\n  \"extends\": \"tsconfig.json\",\n  \"compilerOptions\": {\n    \"module\": \"ES2020\",\n    \"moduleResolution\": \"node\"\n  }\n}\n```\n\nTo enable Babel preprocessing in `ts-jest`, as well as to configure the `tsconfig` file you want use for tests, add or update the `globals` section in your jest config.\n\nExample with separate Babel and Typescript configuration files:\n\n```typescript\n\"globals\": {\n  'ts-jest': {\n    \"babelConfig\": \"true\",\n    \"tsConfig\": \"tsconfig.test.json\"\n  }\n}\n```\n\nExample with inline Typescript and Babel configuration:\n\n```typescript\n\"globals\": {\n  'ts-jest': {\n    \"babelConfig\": {\n      \"plugins\": [\"@userlike/babel-plugin-joke\"],\n      \"presets\": [\n        [\n          \"@babel/preset-env\",\n          {\n            \"targets\": {\n              \"node\": 'current'\n            }\n          }\n        ]\n      ]\n    },\n    \"tsConfig\": {\n      \"module\": \"es2020\",\n      \"moduleResolution\": \"node\",\n    }\n  }\n}\n```\n\nFor details, see [`ts-jest` configuration docs](https://kulshekhar.github.io/ts-jest/user/config/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuserlike%2Fjoke","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuserlike%2Fjoke","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuserlike%2Fjoke/lists"}