{"id":16347959,"url":"https://github.com/ai/nanospy","last_synced_at":"2025-04-05T05:08:16.278Z","repository":{"id":44611024,"uuid":"430216251","full_name":"ai/nanospy","owner":"ai","description":"Spy and mock methods in tests with great TypeScript support","archived":false,"fork":false,"pushed_at":"2023-06-21T23:45:10.000Z","size":134,"stargazers_count":138,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-24T17:54:35.182Z","etag":null,"topics":[],"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/ai.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":"2021-11-20T21:48:24.000Z","updated_at":"2024-06-02T08:46:13.000Z","dependencies_parsed_at":"2024-06-18T15:37:49.628Z","dependency_job_id":"1d981e44-8e39-4b08-99e1-d49ac4faff9d","html_url":"https://github.com/ai/nanospy","commit_stats":{"total_commits":48,"total_committers":6,"mean_commits":8.0,"dds":"0.16666666666666663","last_synced_commit":"696f5f34440f29cc9507b00baed0460b4fdfa91c"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fnanospy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fnanospy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fnanospy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Fnanospy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ai","download_url":"https://codeload.github.com/ai/nanospy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247261591,"owners_count":20910107,"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":"2024-10-11T00:47:28.766Z","updated_at":"2025-04-05T05:08:16.244Z","avatar_url":"https://github.com/ai.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nano Spy\n\nA tiny Node.js library to spy and mock methods in tests\nwith great **TypeScript** support.\n\nIt will take only **[6 KB](https://packagephobia.com/result?p=nanospy)**\nin your `node_modules` and have **0 dependencies**.\n\n```js\nimport { spyOn, restoreAll } from 'nanospy'\n\ntest.after.each(() =\u003e {\n  restoreAll()\n})\n\ntest('calls increase', () =\u003e {\n  const spy = spyOn(counter, 'increase')\n  counter.increase(5)\n  assert.equal(spy.callCount, 1)\n  assert.equal(spy.calls, [[5]])\n})\n```\n\n\u003ca href=\"https://evilmartians.com/?utm_source=nanospy\"\u003e\n  \u003cimg src=\"https://evilmartians.com/badges/sponsored-by-evil-martians.svg\"\n       alt=\"Sponsored by Evil Martians\" width=\"236\" height=\"54\"\u003e\n\u003c/a\u003e\n\n\n## Usage\n\n### Method Spy\n\nSpy tracks method calls, but do not change method’s behavior.\n\n```js\nimport { spyOn, restoreAll } from 'nanospy'\n\ntest.after.each(() =\u003e {\n  restoreAll()\n})\n\ntest('calls increase', () =\u003e {\n  const spy = spyOn(counter, 'increase')\n  counter.increase(5)\n  assert.is(spy.called, true)\n  assert.equal(spy.callCount, 1)\n  assert.equal(spy.calls, [[5]])\n  assert.equal(spy.results, [true])\n})\n```\n\n\n### Mock\n\nMock change the method’s behavior.\n\n```js\nconst spy = spyOn(global, 'fetch', async () =\u003e {\n  return {\n    json: () =\u003e ({ posts })\n  }\n})\n```\n\nOr change next function call:\n\n```js\nspy.nextResult({ ok: false })\n```\n\n```js\nspy.nextError(error)\n```\n\n\n### Functions\n\n`spy` can be used to track callback calls.\n\n```js\nimport { spy } from 'nanospy'\n\nconst fn = spy()\nfn('a', 10)\nfn.callCount //=\u003e 1\nfn.calls //=\u003e [['a', 10]]\n```\n\nYou can pass `spy`’s callback:\n\n```js\nlet fn = spy((name: string) =\u003e {\n  console.log(`Hello, ${name}!`)\n})\nfn('Ivan') //=\u003e Hello, Ivan!\n```\n\nOr change next function call:\n\n```js\nfn.nextResult({ ok: false })\n```\n\n```js\nfn.nextError(error)\n```\n\n\n### Promises\n\nYou can use helpers to test promises:\n\n```js\n\nimport { spy } from 'nanospy'\n\nconst fn = spy()\n\nlet resolve = fn.nextResolve()\nfn().then(arg =\u003e {\n  console.log('Resolved ' + arg)\n})\n\nawait resolve(1) // =\u003e Resolved 1\n```\n\nFor testing errors, you can use `fn.nextReject()`.\n\n\n### Remocking\n\nYou can reassign mocked function with `onCall` method:\n\n```js\nconst obj = {\n  mark: str =\u003e str + '!',\n}\nconst spy = spyOn(obj, 'mark')\n\nobj.mark('a')\nassert.equal(spy.results, ['a!'])\n\nspy.onCall(str =\u003e str + '?')\nobj.mark('a')\nassert.equal(spy.results, ['a!', 'a?'])\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fai%2Fnanospy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fai%2Fnanospy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fai%2Fnanospy/lists"}