项目作者: extend-chrome

项目描述 :
A complete mock of the Chrome API for Chrome extensions for use with Jest.
高级语言: TypeScript
项目地址: git://github.com/extend-chrome/jest-chrome.git
创建时间: 2020-01-24T23:01:01Z
项目社区:https://github.com/extend-chrome/jest-chrome

开源协议:MIT License

下载


jest-chrome

A complete mock of the Chrome API for Chrome extensions, for use
with Jest.

TypeScript support is built in. Each function and event is based
on the
@types/chrome"">@types/chrome
package.

Installation

  1. npm i jest-chrome -D

Set chrome in the global scope during setup so that it is
mocked in imported modules. Add a setup file to jest.config.js:

  1. // jest.config.js
  2. module.exports = {
  3. // Add this line to your Jest config
  4. setupFilesAfterEnv: ['./jest.setup.js'],
  5. }

Use the setup file to assign the mocked chrome object to the
global object:

  1. // jest.setup.js
  2. Object.assign(global, require('jest-chrome'))

Import chrome from jest-chrome for Intellisense and linting.
This is the same object as chrome in the global scope.

  1. import { chrome } from 'jest-chrome'

Usage

All of the following code blocks come from
tests/demo.test.ts.

Events

Each mocked Event has all the normal methods (addListener,
hasListener, hasListeners, and removeListener) plus two
more: callListeners and clearListeners.

callListeners triggers a specific Event. Call callListeners
with the arguments you expect Chrome to pass to your event
listeners. Each event listener for that Event will be called with
those arguments.

clearListeners removes all listeners for a specific Event.

  1. test('chrome api events', () => {
  2. const listenerSpy = jest.fn()
  3. const sendResponseSpy = jest.fn()
  4. chrome.runtime.onMessage.addListener(listenerSpy)
  5. expect(listenerSpy).not.toBeCalled()
  6. expect(chrome.runtime.onMessage.hasListeners()).toBe(true)
  7. chrome.runtime.onMessage.callListeners(
  8. { greeting: 'hello' }, // message
  9. {}, // MessageSender object
  10. sendResponseSpy, // SendResponse function
  11. )
  12. expect(listenerSpy).toBeCalledWith(
  13. { greeting: 'hello' },
  14. {},
  15. sendResponseSpy,
  16. )
  17. expect(sendResponseSpy).not.toBeCalled()
  18. })

Synchronous functions

Some Chrome API functions are synchronous. Use these like any
mocked function:

  1. test('chrome api functions', () => {
  2. const manifest = {
  3. name: 'my chrome extension',
  4. manifest_version: 2,
  5. version: '1.0.0',
  6. }
  7. chrome.runtime.getManifest.mockImplementation(() => manifest)
  8. expect(chrome.runtime.getManifest()).toEqual(manifest)
  9. expect(chrome.runtime.getManifest).toBeCalled()
  10. })

Functions with callbacks

Most Chrome API functions do something asynchronous. They use
callbacks to handle the result. The mock implementation should be
set to handle the callback.

Mocked functions have no default mock implementation!

  1. test('chrome api functions with callback', () => {
  2. const message = { greeting: 'hello?' }
  3. const response = { greeting: 'here I am' }
  4. const callbackSpy = jest.fn()
  5. chrome.runtime.sendMessage.mockImplementation(
  6. (message, callback) => {
  7. callback(response)
  8. },
  9. )
  10. chrome.runtime.sendMessage(message, callbackSpy)
  11. expect(chrome.runtime.sendMessage).toBeCalledWith(
  12. message,
  13. callbackSpy,
  14. )
  15. expect(callbackSpy).toBeCalledWith(response)
  16. })

Callbacks and chrome.runtime.lastError

When something goes wrong in a callback, Chrome sets
chrome.runtime.lastError to an object with a message property.
If you need to test this, set and clear lastError in the mock
implementation.

Remember that lastError is always undefined outside of a
callback!

lastError is an object with a
getter function
for the message property. If message is not checked, Chrome
will log the error to the console. To emulate this, simply set
lastError to an object with a getter that wraps a mock, as seen
below:

  1. test('chrome api functions with lastError', () => {
  2. const message = { greeting: 'hello?' }
  3. const response = { greeting: 'here I am' }
  4. // lastError setup
  5. const lastErrorMessage = 'this is an error'
  6. const lastErrorGetter = jest.fn(() => lastErrorMessage)
  7. const lastError = {
  8. get message() {
  9. return lastErrorGetter()
  10. },
  11. }
  12. // mock implementation
  13. chrome.runtime.sendMessage.mockImplementation(
  14. (message, callback) => {
  15. chrome.runtime.lastError = lastError
  16. callback(response)
  17. // lastError is undefined outside of a callback
  18. delete chrome.runtime.lastError
  19. },
  20. )
  21. // callback implementation
  22. const lastErrorSpy = jest.fn()
  23. const callbackSpy = jest.fn(() => {
  24. if (chrome.runtime.lastError) {
  25. lastErrorSpy(chrome.runtime.lastError.message)
  26. }
  27. })
  28. // send a message
  29. chrome.runtime.sendMessage(message, callbackSpy)
  30. expect(callbackSpy).toBeCalledWith(response)
  31. expect(lastErrorGetter).toBeCalled()
  32. expect(lastErrorSpy).toBeCalledWith(lastErrorMessage)
  33. // lastError has been cleared
  34. expect(chrome.runtime.lastError).toBeUndefined()
  35. })

Contributions

The chrome object is based on schemas from the Chromium
project, with thanks to
sinon-chrome for
compiling the schemas.

Special thanks to @shellscape
for transferring the NPM package name jest-chrome to us!