{"id":16487377,"url":"https://github.com/tjenkinson/interceptor","last_synced_at":"2025-03-21T07:31:20.789Z","repository":{"id":37819009,"uuid":"274486928","full_name":"tjenkinson/interceptor","owner":"tjenkinson","description":"A library that helps with testing asynchronous function calls.","archived":false,"fork":false,"pushed_at":"2025-02-14T11:06:44.000Z","size":960,"stargazers_count":1,"open_issues_count":11,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-01T04:11:22.642Z","etag":null,"topics":["async","asynchronous","callback","testing"],"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/tjenkinson.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-06-23T19:03:52.000Z","updated_at":"2024-11-01T11:36:34.000Z","dependencies_parsed_at":"2024-06-18T03:28:30.407Z","dependency_job_id":"523b65ea-dda6-40a7-88f2-772a824bf3f0","html_url":"https://github.com/tjenkinson/interceptor","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjenkinson%2Finterceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjenkinson%2Finterceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjenkinson%2Finterceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tjenkinson%2Finterceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tjenkinson","download_url":"https://codeload.github.com/tjenkinson/interceptor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244120342,"owners_count":20401118,"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":["async","asynchronous","callback","testing"],"created_at":"2024-10-11T13:33:59.684Z","updated_at":"2025-03-21T07:31:20.454Z","avatar_url":"https://github.com/tjenkinson.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm version](https://badge.fury.io/js/%40tjenkinson%2Finterceptor.svg)](https://badge.fury.io/js/%40tjenkinson%2Finterceptor)\n\n# Interceptor\n\nA library that helps with testing asynchronous function calls.\n\n## Installation\n\n```sh\nnpm install --save @tjenkinson/interceptor\n```\n\nor available on JSDelivr at \"https://cdn.jsdelivr.net/npm/@tjenkinson/interceptor@1\".\n\n## API\n\n### intercept(fn)\n\nThis takes a function to intercept and returns an object with the following properties:\n\n#### fn\n\nA function which takes the same arguments as the input `fn`, and returns a promise. If the input `fn` returned a promise then this has the same signature and can be used in its place.\n\n#### nextCall(): Promise\u003cvoid\u003e\n\nWhen the returned `fn` is called it will be proxied to the input `fn` and the returned promise will resolve.\n\n#### nextCall\u003cT\u003e(callback: ({ input, makeCall, sendResult }) =\u003e T): Promise\u003cT\u003e\n\nThis takes a callback which will be called synchronously when the input `fn` is called. If `fn` has already been called then it will be called immediately for the oldest call.\n\nIt receives an object with the following properties:\n\n- `callArgs`: An array containing the arguments passed to the `fn` call.\n- `makeCall`: A function which calls the input `fn` with the original arguments and passes through the return value.\n- `sendResult`: A function which takes a value that the promise returned from the input `fn` call should resolve with.\n\nThe promise returned from this function resolves with the return value from the callback.\n\n#### numCallsWaiting(): number\n\nA function which returns the number of calls that have not been handled with `nextCall` yet.\n\n## Example\n\nThis example uses jest. Other frameworks are availble ;)\n\n### Waiting for a function call\n\n```ts\nimport { intercept } from '@tjenkinson/interceptor';\n\nfunction doSomethingAsync(callback) {\n  setTimeout(() =\u003e callback('it works'), 200);\n}\n\nit('calls the callback with the correct value', async () =\u003e {\n  const fn = jest.fn();\n  const intercepted = intercept(fn);\n\n  doSomethingAsync(intercepted.fn);\n\n  await intercepted.nextCall();\n  expect(fn).toHaveBeenCalledWith('it works');\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftjenkinson%2Finterceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftjenkinson%2Finterceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftjenkinson%2Finterceptor/lists"}