{"id":13728019,"url":"https://github.com/43081j/hanbi","last_synced_at":"2025-04-06T03:07:30.067Z","repository":{"id":45987106,"uuid":"265943067","full_name":"43081j/hanbi","owner":"43081j","description":"A small javascript library for stubbing and spying on methods/functions.","archived":false,"fork":false,"pushed_at":"2025-02-12T11:04:49.000Z","size":434,"stargazers_count":60,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T19:39:33.001Z","etag":null,"topics":["mocks","spies","stubs","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/43081j.png","metadata":{"files":{"readme":"README.md","changelog":null,"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-05-21T20:16:26.000Z","updated_at":"2025-02-12T11:04:51.000Z","dependencies_parsed_at":"2023-12-13T23:22:27.504Z","dependency_job_id":"40c8f5b2-b615-4398-b229-d6b71f56f367","html_url":"https://github.com/43081j/hanbi","commit_stats":{"total_commits":27,"total_committers":2,"mean_commits":13.5,"dds":0.07407407407407407,"last_synced_commit":"6427825645b8a14f39caf834e114424795dca1fa"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/43081j%2Fhanbi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/43081j%2Fhanbi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/43081j%2Fhanbi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/43081j%2Fhanbi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/43081j","download_url":"https://codeload.github.com/43081j/hanbi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427006,"owners_count":20937201,"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":["mocks","spies","stubs","typescript"],"created_at":"2024-08-03T02:00:36.258Z","updated_at":"2025-04-06T03:07:30.052Z","avatar_url":"https://github.com/43081j.png","language":"TypeScript","funding_links":[],"categories":["Packages"],"sub_categories":["Testing"],"readme":"[![npm](https://img.shields.io/npm/v/hanbi)](https://www.npmjs.com/package/hanbi)\n\n# hanbi\n\nhanbi is a rather small and simple library for stubbing and spying on methods\nand functions in JavaScript tests.\n\n# Install\n\n```\n$ npm i -D hanbi\n```\n\n# Usage\n\n## `spy()`\n\nCreates a single \"spy\" function to be used as input into some other\nfunction.\n\n```ts\nconst spy = hanbi.spy();\nwindow.addEventListener('load', spy.handler);\nspy.called; // true once the event fires\n```\n\n## `stub(fn)`\n\nCreates a wrapped version of a given function which tracks any calls.\n\n```ts\nconst fn = () =\u003e 5;\nconst stub = hanbi.stub(fn);\nstub.handler(); // undefined\nstub.called; // true\n```\n\n## `stubMethod(obj, method)`\n\nReplaces a given method on an object with a wrapped (stubbed) version of it.\n\n```ts\nclass Foo {\n  myMethod() {\n    return 5;\n  }\n}\nconst instance = new Foo();\nconst stub = hanbi.stubMethod(instance, 'myMethod');\ninstance.myMethod(); // undefined\nstub.called; // true\n```\n\n## `restore()`\n\nRestores all stubs/spies to their original functions.\n\n```ts\nclass Foo {\n  myMethod() {\n    return 5;\n  }\n}\nconst instance = new Foo();\nconst stub = hanbi.stubMethod(instance, 'myMethod');\ninstance.myMethod(); // undefined\nrestore();\ninstance.myMethod(); // 5\n```\n\n# Stub API\n\nEach of the above mentioned entry points returns a `Stub` which has\nseveral useful methods.\n\n```ts\nclass Stub {\n  /**\n   * Wrapped function\n   */\n  handler;\n\n  /**\n   * Function to be called when stub is restored\n   */\n  restoreCallback;\n\n  /**\n   * Original function\n   */\n  original;\n\n  /**\n   * Whether or not this stub has been called\n   */\n  called;\n\n  /**\n   * List of all calls this stub has received\n   */\n  calls;\n\n  /**\n   * Retrieves an individual call\n   * @param index Index of the call to retrieve\n   * @return Call at the specified index\n   */\n  getCall(index);\n\n  /**\n   * Retrieves the first call\n   * @return Call object\n   */\n  firstCall;\n\n  /**\n   * Retrieves the last call\n   * @return Call object\n   */\n  lastCall;\n\n  /**\n   * Number of times this stub has been called\n   */\n  callCount;\n\n  /**\n   * Specifies the value this stub should return\n   * @param val Value to return\n   */\n  returns(val);\n\n  /**\n   * Specifies a function to call to retrieve the return value of this\n   * stub\n   * @param fn Function to call\n   */\n  callsFake(fn);\n\n  /**\n   * Enables pass-through, in that the original function is called when\n   * this stub is called.\n   */\n  passThrough();\n\n  /**\n   * Resets call state (e.g. call count, calls, etc.)\n   */\n  reset();\n\n  /**\n   * Restores this stub.\n   * This behaviour differs depending on what created the stub.\n   */\n  restore();\n\n  /**\n   * Asserts that the stub was called with a set of arguments\n   * @param args Arguments to assert for\n   * @return Whether they were passed or not\n   */\n  calledWith(...args);\n\n  /**\n   * Asserts that the stub returned a given value\n   * @param val Value to check for\n   * @return Whether the value was ever returned or not\n   */\n  returned(val);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F43081j%2Fhanbi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F43081j%2Fhanbi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F43081j%2Fhanbi/lists"}