jest-fetch-mock icon indicating copy to clipboard operation
jest-fetch-mock copied to clipboard

jest is not defined

Open puzzledbytheweb opened this issue 6 years ago • 9 comments

While trying to use this package i receive this error:

ReferenceError: jest is not defined
    at Object.<anonymous> (/myProject/node_modules/jest-fetch-mock/src/index.js:49:15)

Do you have any idea how to fix this? Is this related to this project peer-dependencies?

Thanks in advance!

puzzledbytheweb avatar Jan 31 '19 15:01 puzzledbytheweb

Do you have a simple setup on how to replicate?

jefflau avatar Feb 05 '19 09:02 jefflau

I have the same error.

package versions:

jest: v24.9.0
jest-fetch-mock: v2.1.2

jest.config.js (left details out, can reproduce with just this)

module.exports = {
  globalSetup: './tests/jest-setup.js'

tests/jest-setup.js

const fetch = require('jest-fetch-mock')

// details left out - error throw inside `jest-fetch-mock` with just this

The error occurs inside jest-fetch-mock/src/index.js on line 49. There is a reference to jest but no import for it.

jperasmus avatar Aug 16 '19 13:08 jperasmus

How are you running your tests? jest is a global that's available whenever a test is being run with jest

yinzara avatar Aug 17 '19 01:08 yinzara

I have a test run-script in package.json file that runs jest. I use yarn instead of npm, but not sure if that makes a difference. Running the local jest directly (./node_modules/.bin/jest) gives me the exact same error.

jperasmus avatar Aug 19 '19 06:08 jperasmus

Hello everyone. I'm so sorry I did not respond, I had this error so long ago I don't even know in which context it appeared :/. Sorry, I hope you guys can solve it :/

puzzledbytheweb avatar Aug 21 '19 22:08 puzzledbytheweb

I just ran into this error and fixed it. In my situation I have ejected from CRA so I have ./scripts/test.js that my package.json scripts use to run tests. I had first put the global.fetch = require('jest-fetch-mock'); in there. It actually needed to go into ./config/jest/setupTests.js which is what jest is configured to use as a setup file.

qswinson avatar Nov 11 '19 22:11 qswinson

I have the same error.

package versions:

jest: v24.9.0
jest-fetch-mock: v2.1.2

jest.config.js (left details out, can reproduce with just this)

module.exports = {
  globalSetup: './tests/jest-setup.js'

tests/jest-setup.js

const fetch = require('jest-fetch-mock')

// details left out - error throw inside `jest-fetch-mock` with just this

The error occurs inside jest-fetch-mock/src/index.js on line 49. There is a reference to jest but no import for it.

seems it should be

 setupFiles: [
    './__test__/jest-setup.ts'
  ],

not globalSetup

anvlkv avatar Feb 03 '20 15:02 anvlkv

I had this issue. All I had to do to fix it was quit and restart the test watcher. It was caused because I reinstalled node_modules while the watcher was running.

narthur avatar Jan 05 '21 20:01 narthur

Had this after upgrading to Next 11. My setup had collocated test & implementation files which I had working via webpack's IgnoreLoader plugin. This plugin had a breaking change in Webpack 5.

config.plugins.push(new webpack.IgnorePlugin(/\.(e2e|spec).(ts|tsx)$/))

to

config.plugins.push(new webpack.IgnorePlugin({ resourceRegExp: /\.(e2e|spec).(ts|tsx)$/ }))

8lane avatar Jul 30 '21 09:07 8lane