Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with Jest - ReferenceError: chrome is not defined #82

Open
rbutera opened this issue Sep 27, 2018 · 9 comments
Open

Usage with Jest - ReferenceError: chrome is not defined #82

rbutera opened this issue Sep 27, 2018 · 9 comments

Comments

@rbutera
Copy link

rbutera commented Sep 27, 2018

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

Can anyone help me get this working?

@RobertJGabriel
Copy link

bump for this aswell.

@rbutera
Copy link
Author

rbutera commented Oct 29, 2018

@RobertJGabriel can you let me know if you find a fix? For now I'm just stubbing out chrome using jest.fn() but would rather get this working of course!

@marcus-sa
Copy link

marcus-sa commented Nov 7, 2018

You can extend the Jest behavior by providing a test setup file. You can find everything about this in their documentation.
Simply apply the sinon-chrome module to this.global.chrome in the setup file.

@dvlden
Copy link

dvlden commented Feb 21, 2019

Any updates on this? I am unsure what @marcus-sa meant, I am first time using any testing library.

@dvlden
Copy link

dvlden commented 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'))

@iamogbz
Copy link

iamogbz commented Apr 17, 2019

Personally had to

import * as chrome from "sinon-chrome";

before I could use the module with typescript

@ryan0122
Copy link

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');

@rbutera
Copy link
Author

rbutera commented Jul 24, 2019

Thanks everyone. I'm thinking about submitting a PR to add these to the docs

@dustinbyershax
Copy link

dustinbyershax commented Feb 23, 2023

Anyone else running into this today, I solved by

// jest.config.ts

import { pathsToModuleNameMapper, type JestConfigWithTsJest } from 'ts-jest';
const chrome = require('sinon-chrome/extensions');

const config: JestConfigWithTsJest = {
  extensionsToTreatAsEsm: ['.ts'],
  verbose: true,
  preset: 'ts-jest/presets/default-esm',
  testEnvironment: 'node',
  transform: {
    '^.+\\.(ts)?$': ['ts-jest', { useESM: true }],
  },
  testPathIgnorePatterns: ['./dist'],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */),
  // setupFilesAfterEnv: ['./jest.setup.js'],
  globals: {
    chrome,
  },
};

export default config;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants