sinon-chrome icon indicating copy to clipboard operation
sinon-chrome copied to clipboard

Usage with Jest - ReferenceError: chrome is not defined

Open rbutera opened this issue 6 years ago • 9 comments

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

Can anyone help me get this working?

rbutera avatar Sep 27 '18 10:09 rbutera

bump for this aswell.

RobertJGabriel avatar Oct 28 '18 03:10 RobertJGabriel

@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!

rbutera avatar Oct 29 '18 09:10 rbutera

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.

marcus-sa avatar Nov 07 '18 11:11 marcus-sa

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

dvlden avatar Feb 21 '19 22:02 dvlden

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

dvlden avatar Feb 25 '19 14:02 dvlden

Personally had to

import * as chrome from "sinon-chrome";

before I could use the module with typescript

iamogbz avatar Apr 17 '19 00:04 iamogbz

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

ryan0122 avatar Jul 16 '19 21:07 ryan0122

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

rbutera avatar Jul 24 '19 12:07 rbutera

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;

dustinbyershax avatar Feb 23 '23 17:02 dustinbyershax