question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with Jest - ReferenceError: chrome is not defined

See original GitHub issue

I’m getting ReferenceError: chrome is not defined when trying to use sinon-chrome with Jest.

Can anyone help me get this working?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

5reactions
dvldencommented, Feb 25, 2019

I think @marcus-sa was referring to something like this:

import chrome from 'sinon-chrome/extensions'
import browser from 'sinon-chrome/webextensions'

describe('your test', () => {
  beforeAll(() => {
    global.chrome = chrome
    global.browser = browser
  })

  it('should ...', () => {
    // ..
  })

  afterAll(() => {
    chrome.flush()
    browser.flush()
  })
})

However, this was not my issue. My issue is that I am using webextension-polyfill in most of my classes for the extension project and I couldn’t test a single one, because chrome is not defined.

Just importing the class would throw such error, without any other needs.

How I managed to fix this? - Easy.

Below all your module imports, mock the polyfill with this package.

jest.mock('webextension-polyfill', () => require('sinon-chrome/webextensions'))
3reactions
ryan0122commented, Jul 16, 2019

I was getting the same error so I had to declare window.chrome = chrome before including my .js file to be tested.

const chrome = require('sinon-chrome');
window.chrome = chrome;

require('../src/check');
Read more comments on GitHub >

github_iconTop Results From Across the Web

jest-chrome - npm
Start using jest-chrome in your project by running `npm i jest-chrome`. There are no other projects in the npm registry using jest-chrome.
Read more >
Usage with Jest - ReferenceError: chrome is not defined
I'm getting ReferenceError: chrome is not defined when trying to use sinon-chrome with Jest. Can anyone help me get this working? See More....
Read more >
Mocking in Jest (Typescript - Mock chrome namespace)
ReferenceError : chrome is not defined. Because one of the components calls this namespace to add a listener ( chrome.runtime.onMessage.
Read more >
Jest mock referenceerror cannot access before initialization
Oct 10, 2021 · If the module you are mocking is a Node Jest uses a custom ... ReferenceError: x is not defined...
Read more >
ReferenceError: "x" is not defined - JavaScript - MDN Web Docs
There is a non-existent variable referenced somewhere. This variable needs to be declared, or you need to make sure it is available in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found