{"id":13769547,"url":"https://github.com/testdouble/testdouble-jest","last_synced_at":"2025-04-15T06:15:09.975Z","repository":{"id":49972648,"uuid":"121058508","full_name":"testdouble/testdouble-jest","owner":"testdouble","description":"A testdouble.js extension to add support for Jest module mocking","archived":false,"fork":false,"pushed_at":"2021-06-02T08:57:04.000Z","size":57,"stargazers_count":37,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-15T06:15:04.687Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/testdouble.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-10T22:06:38.000Z","updated_at":"2023-04-08T18:30:06.000Z","dependencies_parsed_at":"2022-09-07T03:40:34.159Z","dependency_job_id":null,"html_url":"https://github.com/testdouble/testdouble-jest","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testdouble%2Ftestdouble-jest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testdouble%2Ftestdouble-jest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testdouble%2Ftestdouble-jest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testdouble%2Ftestdouble-jest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/testdouble","download_url":"https://codeload.github.com/testdouble/testdouble-jest/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249016641,"owners_count":21198833,"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-08-03T17:00:28.242Z","updated_at":"2025-04-15T06:15:09.949Z","avatar_url":"https://github.com/testdouble.png","language":"JavaScript","readme":"# testdouble-jest\n\nSupport for [testdouble.js](https://github.com/testdouble/testdouble.js) for\nusers of [Jest](https://github.com/facebook/jest)!\n\nNote that testdouble-jest requires testdouble@3.6.0 and jest@21.0.0 or higher to work.\n\n## Installation\n\n```\n$ npm i -D testdouble-jest\n```\n\nThen, from a test helper (we recommend setting a\n[setupFilesAfterEnv](https://jestjs.io/docs/en/configuration.html#setupfilesafterenv-array)\nmodule), invoke the module and pass in both `td` and `jest`, like so:\n\n```js\nglobal.td = require('testdouble')\nrequire('testdouble-jest')(td, jest)\n```\n\nFor an example of a helper that sets up testdouble.js, testdouble-jest, and\nensures `td.reset()` is called after each test, look at\n[example/helper.js](/example/helper.js) in this repo.\n\n## Usage\n\nWhen you invoke `testdouble-jest`, it does two things: (1) adds support for\nusing `td.replace()` for module replacement in Jest tests, and (2) adds a new\ntop-level `td.mock()` function that mirrors the `jest.mock()` API.\n\nWe recommend using `td.replace()`, since it's terser (by returning the fake\ninstead of the `jest` object) and your use of testdouble.js will remain portable\neven if you were to move to a different test runner.\n\n**`td.replace(moduleName[, manualStub])`**\n\nOnce you've initialized testdouble-jest in your test run, `td.replace()` will be\nable to replace modules just as it does in any other test runner (as of \ntestdouble@3.5.0). Functionally, it's delegates to `td.mock()`, but behaves just \nas it [always\nhas](https://github.com/testdouble/testdouble.js#module-replacement-with-nodejs)\nfor module replacement.\n\nHere's a trivial example:\n\n```js\nlet loadInvoices, subject\ndescribe('td.replace', () =\u003e {\n  beforeEach(() =\u003e {\n    loadInvoices = td.replace('./load-invoices')\n    subject = require('./calculate-payment')\n  })\n  it('calculates payments', () =\u003e {\n    td.when(loadInvoices(2018, 7)).thenReturn([24,28])\n\n    const result = subject('2018-07')\n\n    expect(result).toEqual(52)\n  })\n})\n```\n\nFor a runnable example, check\n[example/td-replace.test.js](/example/td-replace.test.js).\n\n**`td.mock(moduleName[, moduleFactory, options])`**\n\n`td.mock()` is designed to have the same API as\n[`jest.mock()`](https://facebook.github.io/jest/docs/en/es6-class-mocks.html).\nIf you just pass a module name to `td.mock()`, it will imitate the real\ndependency and use Jest's own module replacement facility to ensure that any\n`require()` calls by your test subject receive the testdouble fake, as opposed\nto the real dependency. There's an example in this repo at\n[example/td-mock.test.js](/example/td-mock.test.js).\n\nIf you've used `jest.mock()` before, `td.mock()` will seem pretty familiar.\n`td.mock()` returns the `jest` object (since that's what\n`jest.mock()` does), so your test will also need to `require()` the thing you\njust faked if you want to set up any stubbings or invocation assertions.\n\nNote that if you provide a `moduleFactory` and/or `options` argument, `td.mock`\nwill simply delegate to `jest.mock`, since it won't have anything\ntestdouble.js-specific to do.\n","funding_links":[],"categories":["Packages"],"sub_categories":["Library extensions"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestdouble%2Ftestdouble-jest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftestdouble%2Ftestdouble-jest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestdouble%2Ftestdouble-jest/lists"}