{"id":22900584,"url":"https://github.com/extend-chrome/jest-chrome","last_synced_at":"2025-10-05T13:15:49.001Z","repository":{"id":48281281,"uuid":"236098056","full_name":"extend-chrome/jest-chrome","owner":"extend-chrome","description":"A complete mock of the Chrome API for Chrome extensions for use with Jest.","archived":false,"fork":false,"pushed_at":"2024-04-16T09:58:26.000Z","size":870,"stargazers_count":130,"open_issues_count":26,"forks_count":26,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T00:09:02.199Z","etag":null,"topics":["chrome-api","chrome-extension","javascript","jest","typescript"],"latest_commit_sha":null,"homepage":null,"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/extend-chrome.png","metadata":{"funding":{"ko_fi":"jacksteam"},"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":"2020-01-24T23:01:01.000Z","updated_at":"2025-02-10T16:59:08.000Z","dependencies_parsed_at":"2024-06-18T13:58:29.544Z","dependency_job_id":"45c4e7ab-949c-47c2-9313-d583fb2b73f6","html_url":"https://github.com/extend-chrome/jest-chrome","commit_stats":{"total_commits":61,"total_committers":5,"mean_commits":12.2,"dds":"0.39344262295081966","last_synced_commit":"52865e980f4e206d3efb9ee6e6f225f3c6830ec6"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extend-chrome%2Fjest-chrome","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extend-chrome%2Fjest-chrome/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extend-chrome%2Fjest-chrome/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/extend-chrome%2Fjest-chrome/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/extend-chrome","download_url":"https://codeload.github.com/extend-chrome/jest-chrome/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247271530,"owners_count":20911587,"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":["chrome-api","chrome-extension","javascript","jest","typescript"],"created_at":"2024-12-14T01:28:54.724Z","updated_at":"2025-10-05T13:15:43.975Z","avatar_url":"https://github.com/extend-chrome.png","language":"TypeScript","funding_links":["https://ko-fi.com/jacksteam"],"categories":[],"sub_categories":[],"readme":"# `jest-chrome`\n\nA complete mock of the Chrome API for Chrome extensions, for use\nwith Jest.\n\nTypeScript support is built in. Each function and event is based\non the\n[`@types/chrome`](https://www.npmjs.com/package/@types/chrome)\npackage.\n\n## Installation\n\n```sh\nnpm i jest-chrome -D\n```\n\nSet `chrome` in the global scope during setup so that it is\nmocked in imported modules. Add a setup file to `jest.config.js`:\n\n```javascript\n// jest.config.js\n\nmodule.exports = {\n  // Add this line to your Jest config\n  setupFilesAfterEnv: ['./jest.setup.js'],\n}\n```\n\nUse the setup file to assign the mocked `chrome` object to the\n`global` object:\n\n```javascript\n// jest.setup.js\n\nObject.assign(global, require('jest-chrome'))\n```\n\nImport `chrome` from `jest-chrome` for Intellisense and linting.\nThis is the same object as `chrome` in the global scope.\n\n```javascript\nimport { chrome } from 'jest-chrome'\n```\n\n## Usage\n\n\u003e All of the following code blocks come from\n\u003e [`tests/demo.test.ts`](tests/demo.test.ts).\n\n### Events\n\nEach mocked Event has all the normal methods (`addListener`,\n`hasListener`, `hasListeners`, and `removeListener`) plus two\nmore: `callListeners` and `clearListeners`.\n\n`callListeners` triggers a specific Event. Call `callListeners`\nwith the arguments you expect Chrome to pass to your event\nlisteners. Each event listener for that Event will be called with\nthose arguments.\n\n`clearListeners` removes all listeners for a specific Event.\n\n```javascript\ntest('chrome api events', () =\u003e {\n  const listenerSpy = jest.fn()\n  const sendResponseSpy = jest.fn()\n\n  chrome.runtime.onMessage.addListener(listenerSpy)\n\n  expect(listenerSpy).not.toBeCalled()\n  expect(chrome.runtime.onMessage.hasListeners()).toBe(true)\n\n  chrome.runtime.onMessage.callListeners(\n    { greeting: 'hello' }, // message\n    {}, // MessageSender object\n    sendResponseSpy, // SendResponse function\n  )\n\n  expect(listenerSpy).toBeCalledWith(\n    { greeting: 'hello' },\n    {},\n    sendResponseSpy,\n  )\n  expect(sendResponseSpy).not.toBeCalled()\n})\n```\n\n### Synchronous functions\n\nSome Chrome API functions are synchronous. Use these like any\nmocked function:\n\n```javascript\ntest('chrome api functions', () =\u003e {\n  const manifest = {\n    name: 'my chrome extension',\n    manifest_version: 2,\n    version: '1.0.0',\n  }\n\n  chrome.runtime.getManifest.mockImplementation(() =\u003e manifest)\n\n  expect(chrome.runtime.getManifest()).toEqual(manifest)\n  expect(chrome.runtime.getManifest).toBeCalled()\n})\n```\n\n### Functions with callbacks\n\nMost Chrome API functions do something asynchronous. They use\ncallbacks to handle the result. The mock implementation should be\nset to handle the callback.\n\n\u003e Mocked functions have no default mock implementation!\n\n```javascript\ntest('chrome api functions with callback', () =\u003e {\n  const message = { greeting: 'hello?' }\n  const response = { greeting: 'here I am' }\n  const callbackSpy = jest.fn()\n\n  chrome.runtime.sendMessage.mockImplementation(\n    (message, callback) =\u003e {\n      callback(response)\n    },\n  )\n\n  chrome.runtime.sendMessage(message, callbackSpy)\n\n  expect(chrome.runtime.sendMessage).toBeCalledWith(\n    message,\n    callbackSpy,\n  )\n  expect(callbackSpy).toBeCalledWith(response)\n})\n```\n\n### Callbacks and `chrome.runtime.lastError`\n\nWhen something goes wrong in a callback, Chrome sets\n`chrome.runtime.lastError` to an object with a message property.\nIf you need to test this, set and clear `lastError` in the mock\nimplementation.\n\n\u003e Remember that `lastError` is always undefined outside of a\n\u003e callback!\n\n`lastError` is an object with a\n[getter function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)\nfor the `message` property. If `message` is not checked, Chrome\nwill log the error to the console. To emulate this, simply set\n`lastError` to an object with a getter that wraps a mock, as seen\nbelow:\n\n```javascript\ntest('chrome api functions with lastError', () =\u003e {\n  const message = { greeting: 'hello?' }\n  const response = { greeting: 'here I am' }\n\n  // lastError setup\n  const lastErrorMessage = 'this is an error'\n  const lastErrorGetter = jest.fn(() =\u003e lastErrorMessage)\n  const lastError = {\n    get message() {\n      return lastErrorGetter()\n    },\n  }\n\n  // mock implementation\n  chrome.runtime.sendMessage.mockImplementation(\n    (message, callback) =\u003e {\n      chrome.runtime.lastError = lastError\n\n      callback(response)\n\n      // lastError is undefined outside of a callback\n      delete chrome.runtime.lastError\n    },\n  )\n\n  // callback implementation\n  const lastErrorSpy = jest.fn()\n  const callbackSpy = jest.fn(() =\u003e {\n    if (chrome.runtime.lastError) {\n      lastErrorSpy(chrome.runtime.lastError.message)\n    }\n  })\n\n  // send a message\n  chrome.runtime.sendMessage(message, callbackSpy)\n\n  expect(callbackSpy).toBeCalledWith(response)\n  expect(lastErrorGetter).toBeCalled()\n  expect(lastErrorSpy).toBeCalledWith(lastErrorMessage)\n\n  // lastError has been cleared\n  expect(chrome.runtime.lastError).toBeUndefined()\n})\n```\n\n### Contributions\n\nThe `chrome` object is based on schemas from the Chromium\nproject, with thanks to\n[`sinon-chrome`](https://github.com/acvetkov/sinon-chrome) for\ncompiling the schemas.\n\nSpecial thanks to [@shellscape](https://github.com/shellscape)\nfor transferring the NPM package name `jest-chrome` to us!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextend-chrome%2Fjest-chrome","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fextend-chrome%2Fjest-chrome","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fextend-chrome%2Fjest-chrome/lists"}