{"id":17349420,"url":"https://github.com/iambumblehead/esmock","last_synced_at":"2025-04-13T18:38:05.476Z","repository":{"id":42711579,"uuid":"328097453","full_name":"iambumblehead/esmock","owner":"iambumblehead","description":"ESM import and globals mocking for unit tests","archived":false,"fork":false,"pushed_at":"2025-01-24T21:58:33.000Z","size":751,"stargazers_count":205,"open_issues_count":6,"forks_count":20,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-06T16:07:49.930Z","etag":null,"topics":["es6","esm","fetch","globals","import","mock","mocking","mocking-modules","module","test","unit-test"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iambumblehead.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2021-01-09T07:12:44.000Z","updated_at":"2025-03-24T05:34:44.000Z","dependencies_parsed_at":"2023-10-14T16:54:03.429Z","dependency_job_id":"9dd36fce-150f-4405-936a-dd4c7f8f32f4","html_url":"https://github.com/iambumblehead/esmock","commit_stats":{"total_commits":834,"total_committers":14,"mean_commits":59.57142857142857,"dds":0.09112709832134291,"last_synced_commit":"85d386d4daf732b06e0ca57b8b2cadadef7b7fce"},"previous_names":[],"tags_count":96,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambumblehead%2Fesmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambumblehead%2Fesmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambumblehead%2Fesmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iambumblehead%2Fesmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iambumblehead","download_url":"https://codeload.github.com/iambumblehead/esmock/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248762615,"owners_count":21157757,"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":["es6","esm","fetch","globals","import","mock","mocking","mocking-modules","module","test","unit-test"],"created_at":"2024-10-15T16:55:58.297Z","updated_at":"2025-04-13T18:38:05.457Z","avatar_url":"https://github.com/iambumblehead.png","language":"JavaScript","readme":"```diff\n+                                                ██╗\n+ ██████╗  ███████╗ █████═████╗  ██████╗  ██████╗██║   ██╗\n+██╔═══██╗██╔═════╝██╔══██╔══██╗██╔═══██╗██╔════╝██║  ██╔╝\n+████████║╚██████╗ ██║  ██║  ██║██║   ██║██║     ██████╔╝\n+██╔═════╝ ╚════██╗██║  ██║  ██║██║   ██║██║     ██╔══██╗\n+╚███████╗███████╔╝██║  ██║  ██║╚██████╔╝╚██████╗██║  ╚██╗\n+ ╚══════╝╚══════╝ ╚═╝  ╚═╝  ╚═╝ ╚═════╝  ╚═════╝╚═╝   ╚═╝\n```\n[![npm][9]][7] [![coverage][8]][2] [![install size][6]][5] [![downloads][10]][7]\n\n**esmock provides native ESM import and globals mocking for unit tests.** Use examples below as a quick-start guide, see the [descriptive and friendly esmock guide here,][4] or browse [esmock's test runner examples.][3]\n\n_**Note: For versions of node prior to v20.6.0,** \"--loader\" command line arguments must be used with `esmock` as demonstrated [in the wiki.][4] Current versions of node do not require \"--loader\"._\n\n_**Note: due to --loader issues,** support for `ava` and `jest` are dropped._\n\n`esmock` has the below signature\n```js\nawait esmock(\n  './to/module.js', // path to target module being tested\n  { ...childmocks }, // mock definitions imported by target module\n  { ...globalmocks }) // mock definitions imported everywhere\n```\n\n`esmock` examples\n```js\nimport test from 'node:test'\nimport assert from 'node:assert'\nimport esmock from 'esmock'\n\ntest('package, alias and local file mocks', async () =\u003e {\n  const cookup = await esmock('../src/cookup.js', {\n    addpkg: (a, b) =\u003e a + b,\n    '#icon': { coffee: '☕', bacon: '🥓' },\n    '../src/breakfast.js': {\n      default: () =\u003e ['coffee', 'bacon'],\n      addSalt: meal =\u003e meal + '🧂'\n    }\n  })\n\n  assert.equal(cookup('breakfast'), '☕🥓🧂')\n})\n\ntest('full import tree mocks —third param', async () =\u003e {\n  const { getFile } = await esmock('../src/main.js', {}, {\n    // mocks *every* fs.readFileSync inside the import tree\n    fs: { readFileSync: () =\u003e 'returned to 🌲 every caller in the tree' }\n  })\n\n  assert.equal(getFile(), 'returned to 🌲 every caller in the tree')\n})\n\ntest('mock fetch, Date, setTimeout and any globals', async () =\u003e {\n  // https://github.com/iambumblehead/esmock/wiki#call-esmock-globals\n  const { userCount } = await esmock('../Users.js', {\n    '../req.js': await esmock('../req.js', {\n      import: { // define globals like 'fetch' on the import namespace\n        fetch: async () =\u003e ({\n          status: 200,\n          json: async () =\u003e [['jim','😄'],['jen','😊']]\n        })\n      }\n    })\n  })\n\n  assert.equal(await userCount(), 2)\n})\n\ntest('mocks \"await import()\" using esmock.p', async () =\u003e {\n  // using esmock.p, mock definitions are kept in cache\n  const doAwaitImport = await esmock.p('../awaitImportLint.js', {\n    eslint: { ESLint: cfg =\u003e cfg }\n  })\n\n  // mock definition is returned from cache, when import is called\n  assert.equal(await doAwaitImport('cfg🛠️'), 'cfg🛠️')\n  // a bit more info are found in the wiki guide\n})\n\ntest('esmock.strict mocks', async () =\u003e {\n  // replace original module definitions and do not merge them\n  const pathWrapper = await esmock.strict('../src/pathWrapper.js', {\n    path: { dirname: () =\u003e '/path/to/file' }\n  })\n\n  // error, because \"path\" mock above does not define path.basename\n  assert.rejects(() =\u003e pathWrapper.basename('/dog.🐶.png'), {\n    name: 'TypeError',\n    message: 'path.basename is not a function'\n  })\n})\n```\n\n[0]: https://www.bumblehead.com \"bumblehead\"\n[1]: https://github.com/iambumblehead/esmock/workflows/nodejs-ci/badge.svg \"nodejs-ci pipeline\"\n[2]: https://github.com/iambumblehead/esmock \"esmock\"\n[3]: https://github.com/iambumblehead/esmock/tree/main/tests \"tests\"\n[4]: https://github.com/iambumblehead/esmock/wiki\n[5]: https://packagephobia.now.sh/result?p=esmock\n[6]: https://packagephobia.now.sh/badge?p=esmock\n[7]: https://www.npmjs.com/package/esmock\n[8]: https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/iambumblehead/166d927bd0089d7bfdee4e98a537712c/raw/esmock__heads_master.json\n[9]: https://img.shields.io/npm/v/esmock\n[10]: https://badgen.now.sh/npm/dm/esmock\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiambumblehead%2Fesmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiambumblehead%2Fesmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiambumblehead%2Fesmock/lists"}