jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
883 stars 116 forks source link

jest is not defined #104

Open puzzledbytheweb opened 5 years ago

puzzledbytheweb commented 5 years ago

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!

jefflau commented 5 years ago

Do you have a simple setup on how to replicate?

jperasmus commented 5 years ago

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.

yinzara commented 5 years ago

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

jperasmus commented 5 years ago

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.

puzzledbytheweb commented 5 years ago

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 :/

qswinson commented 4 years ago

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.

anvlkv commented 4 years ago

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

narthur commented 3 years ago

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.

8lane commented 3 years ago

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)$/ }))