{"id":15825391,"url":"https://github.com/mcous/testdouble-vitest","last_synced_at":"2026-02-09T09:34:14.876Z","repository":{"id":147237052,"uuid":"569020424","full_name":"mcous/testdouble-vitest","owner":"mcous","description":"Use testdouble.js with vitest for a happier, more productive TDD experience!","archived":false,"fork":false,"pushed_at":"2025-02-19T20:05:08.000Z","size":246,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-30T18:51:19.664Z","etag":null,"topics":["mock","tdd"],"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/mcous.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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":"2022-11-21T22:54:27.000Z","updated_at":"2025-02-19T20:05:11.000Z","dependencies_parsed_at":"2025-04-05T08:12:15.597Z","dependency_job_id":"54253d3e-7230-4bcf-a9e0-08342eb4821c","html_url":"https://github.com/mcous/testdouble-vitest","commit_stats":{"total_commits":15,"total_committers":3,"mean_commits":5.0,"dds":0.2666666666666667,"last_synced_commit":"055fad285be4b1746934ce30fc30085887960284"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/mcous/testdouble-vitest","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcous%2Ftestdouble-vitest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcous%2Ftestdouble-vitest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcous%2Ftestdouble-vitest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcous%2Ftestdouble-vitest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcous","download_url":"https://codeload.github.com/mcous/testdouble-vitest/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcous%2Ftestdouble-vitest/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29260752,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-09T04:11:57.159Z","status":"ssl_error","status_checked_at":"2026-02-09T04:11:56.117Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["mock","tdd"],"created_at":"2024-10-05T09:09:07.462Z","updated_at":"2026-02-09T09:34:14.860Z","avatar_url":"https://github.com/mcous.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# testdouble-vitest\n\n[![npm package][npm badge]][npm package]\n[![CI Status][ci badge]][ci status]\n\nUse [testdouble.js] with [Vitest] for a happier, more productive TDD experience!\n\nThis module ties Vitest's [import mocking system] together with [td.imitate] to give you a version of [td.replaceEsm] that works in vitest.\n\n[testdouble.js]: https://github.com/testdouble/testdouble.js\n[td.imitate]: https://github.com/testdouble/testdouble.js#tdimitate\n[td.replaceesm]: https://github.com/testdouble/testdouble.js#tdreplace-and-tdreplaceesm-for-replacing-dependencies\n[vitest]: https://vitest.dev\n[vitest-when]: https://github.com/mcous/vitest-when\n[import mocking system]: https://vitest.dev/guide/mocking.html#modules\n[npm package]: https://www.npmjs.com/package/testdouble-vitest\n[npm badge]: https://img.shields.io/npm/v/testdouble-vitest?style=flat-square\n[ci status]: https://github.com/mcous/testdouble-vitest/actions/workflows/ci.yaml?query=branch%3Amain\n[ci badge]: https://img.shields.io/github/actions/workflow/status/mcous/testdouble-vitest/ci.yaml?branch=main\u0026style=flat-square\n\n## Setup\n\nInstall vitest, testdouble, and testdouble-vitest using your package manager of choice...\n\n```shell\nnpm install --save-dev vitest testdouble testdouble-vitest\n```\n\n..and you should be ready to go!\n\n## Recommended usage\n\nInstead of using `td.replaceEsm()` and `td.reset()`, use the `replaceEsm()` and `reset()` functions from testdouble-vitest.\n\nFrom there, follow recommended testdouble.js usage. Use your `beforeEach()` hook to mock out your test subject's dependencies and import your subject using a dynamic `import()`, then reset in `afterEach()`. See [example] for more details.\n\n```ts\nimport { vi, describe, beforeEach, afterEach, it } from 'vitest'\nimport { replaceEsm, reset } from 'testdouble-vitest'\nimport * as td from 'testdouble'\n\nimport type * as dependencyModule from '../dependency'\nimport type * as subjectModule from '../subject'\n\ndescribe('collaborator subject', () =\u003e {\n  let dependency: typeof dependencyModule\n  let subject: typeof subjectModule\n\n  beforeEach(async () =\u003e {\n    dependency = await replaceEsm('../dependency')\n    subject = await import('../subject')\n  })\n\n  afterEach(() =\u003e {\n    reset()\n  })\n\n  it('should replace the dependency with a testdouble imitation', async () =\u003e {\n    td.when(dependency.load('abc123')).thenResolve({ id: 'abc123' })\n\n    const result = await subject.getReport('abc123')\n\n    expect(result).to.eql({ id: 'abc123' })\n  })\n})\n```\n\nLike vanilla `td.replaceEsm()`, this module's `replaceEsm()` also allows you to specify the replacement explicitly.\n\n```ts\ndependency = await replaceEsm(\n  '../dependency',\n  { doSomething: td.func('doSomething') }, // named exports object\n  td.func('defaultExport'), // default export\n)\n```\n\n[example]: https://github.com/mcous/testdouble-vitest/tree/main/example\n\n## Usage with `vi.mock`\n\nIf you prefer to use `vi.mock()` at the top level of your test files, but you would still like to use testdouble.js fakes, you can use this module's `imitateEsm()` function. Compared to the recommended usage, using `vi.mock`:\n\n- Slightly increases the risk of cross-test module state pollution\n- Does not differentiate `import` statements for mocked modules and real imports\n\nThese tradeoffs might be worth it if you or your team is more comfortable with the typical `jest.mock()` / `vi.mock()` style.\n\n\u003e [!TIP]\n\u003e For a lightweight `td.when`-style stubbing syntax with vanilla Vitest mocks,\n\u003e check out [vitest-when]\n\n```ts\nimport { vi, describe, beforeEach, afterEach, it } from 'vitest'\nimport { imitateEsm, reset } from 'testdouble-vitest'\nimport * as td from 'testdouble'\n\nimport * as dependency from '../dependency'\nimport * as subject from '../subject'\n\nvi.mock('../dependency', () =\u003e imitateEsm('../dependency'))\n\ndescribe('collaborator subject', () =\u003e {\n  afterEach(() =\u003e {\n    reset()\n  })\n\n  it('should replace the dependency with a testdouble imitation', async () =\u003e {\n    td.when(dependency.load('abc123')).thenResolve({ id: 'abc123' })\n\n    const result = await subject.getReport('abc123')\n\n    expect(result).to.eql({ id: 'abc123' })\n  })\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcous%2Ftestdouble-vitest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcous%2Ftestdouble-vitest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcous%2Ftestdouble-vitest/lists"}