{"id":17068829,"url":"https://github.com/janhesters/mock-function","last_synced_at":"2025-04-12T18:53:58.999Z","repository":{"id":39313197,"uuid":"321050059","full_name":"janhesters/mock-function","owner":"janhesters","description":"A simple mock function to mock functions.","archived":false,"fork":false,"pushed_at":"2023-03-29T07:01:26.000Z","size":881,"stargazers_count":5,"open_issues_count":5,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T13:11:20.285Z","etag":null,"topics":["jest","mocking","mocking-utility","riteway","testing","testing-tool"],"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/janhesters.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}},"created_at":"2020-12-13T11:35:49.000Z","updated_at":"2023-05-02T09:50:49.000Z","dependencies_parsed_at":"2023-02-13T18:31:14.821Z","dependency_job_id":null,"html_url":"https://github.com/janhesters/mock-function","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janhesters%2Fmock-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janhesters%2Fmock-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janhesters%2Fmock-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/janhesters%2Fmock-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/janhesters","download_url":"https://codeload.github.com/janhesters/mock-function/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248602275,"owners_count":21131613,"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","mocking","mocking-utility","riteway","testing","testing-tool"],"created_at":"2024-10-14T11:15:10.623Z","updated_at":"2025-04-12T18:53:58.974Z","avatar_url":"https://github.com/janhesters.png","language":"TypeScript","readme":"# mock-function\n\nA simple mock function to mock functions.\n\nInspired by\n[`jest.fn`](https://jestjs.io/docs/en/jest-object#jestfnimplementation).\n\n## Installation\n\nInstall the package.\n\n```\n# NPM\nnpm i --save-dev mock-function\n# yarn\nyarn add --dev mock-function\n```\n\nImport it in your tests.\n\n```ts\nimport fn from 'mock-function';\n```\n\n## Usage\n\n```ts\nimport mockFunction from 'mock-function';\n\nconst add = (a: number, b: number) =\u003e a + b;\nconst mockedAdd = mockFunction(add);\n\nmockedAdd.hasBeenCalled();\n// ↵ false\n\nmockedAdd.hasBeenCalledWith(21, 21);\n// ↵ false\n\nmockedAdd.hasBeenCalledTimes;\n// ↵ 0\nmockedAdd.calls;\n// ↵ []\n\nmockedAdd(21, 21);\n// ↵ 42\nmockedAdd(9000, 1);\n// ↵ 9001 (😱 OVER 9000)\n\nmockedAdd.hasBeenCalled();\n// ↵ true\n\nmockedAdd.hasBeenCalledWith(2000, 12);\n// ↵ false\nmockedAdd.hasBeenCalledWith(21, 21);\n// ↵ true\n\nmockedAdd.hasBeenCalledTimes;\n// ↵ 2\nmockedAdd.calls;\n// ↵ [[21, 21], [9000, 1]]\n```\n\n## Caveats\n\n[Mocking is a code smell](https://medium.com/javascript-scene/mocking-is-a-code-smell-944a70c90a6a)\nfor tight coupling and as such a surface indication that you might be able to\nimprove your tests and / or underlying code. Think about whether you can isolate\nyour side-effects or non-deterministic functions and make your code more\nmodular. Mocking can be okay, for example in integration tests.\n\nFor example, the pure function `add` in [Usage](#usage) should be tested using\nplain unit tests that\n[assert the actual and expected output](https://medium.com/javascript-scene/rethinking-unit-test-assertions-55f59358253f).\nI chose `add` to simplify the example, but it is generally a bad use-case for\nmocking.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanhesters%2Fmock-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanhesters%2Fmock-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanhesters%2Fmock-function/lists"}