Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: Response is not defined

Short Version:

I am writing a test case for my Vue component using vue-test-utils and jest.

I am trying to create a Response object in one of my test cases, but I keep getting error ReferenceError: Response is not defined

Long Version

I am trying to mock an API response when a Vuex action is dispatched using jest. I have created a Vuex action like this

let fakeAccountList = [
  {"id":"abcd","title":"Random Account 1"},
  {"id":"pqrs","title":"Random Account 2"}
]
actions = {
   getAccounts: jest.fn(()=>{

     return new Promise((resolve, reject)=>{
       resolve(mockResponse(200,'ok',fakeAccountList))
      })
    })
 }

The getAccounts action returns a Promise which resolves to a list of accounts. The mockResponse function is as shown:

mockResponse = (status, statusText, response) => {

  return new Response(response, {
    status: status,
    statusText: statusText,
    headers: {
      'Content-type': 'application/json'
    }
  });
};

When mockResponse is called, I get the error ReferenceError: Response is not defined

I found this very old issue: https://github.com/facebook/jest/issues/930 The comments suggest that it was fixed,but, it is not working for me. I do not want to import extra libraries such as isomorphic-fetch just for this.

Here is my devDependencies in package.json:

"devDependencies": {
    "@vue/cli-plugin-babel": "3.0.3",
    "@vue/cli-plugin-e2e-cypress": "3.0.3",
    "@vue/cli-plugin-eslint": "3.0.3",
    "@vue/cli-plugin-unit-jest": "3.0.3",
    "@vue/cli-service": "3.0.3",
    "@vue/test-utils": "1.0.0-beta.25",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "23.6.0",
    "less": "3.0.4",
    "less-loader": "4.1.0",
    "lodash": "4.17.10",
    "node-sass": "4.9.2",
    "sass-loader": "7.0.3",
    "vue-template-compiler": "2.5.16",
  }

Any help would be appreciated. Thanks in advance

like image 294
Ankit Kante Avatar asked Sep 20 '18 08:09

Ankit Kante


People also ask

How do you fix ReferenceError require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

What is a ReferenceError in Javascript?

The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced. ReferenceError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .

What is uncaught ReferenceError in Javascript?

The Javascript ReferenceError occurs when referencing a variable that does not exist or has not yet been initialized in the current scope. Reference errors in Javascript are of a few different types, with variations of each, that may get triggered in code.

What is uncaught ReferenceError?

Ad. While programming in JavaScript, jQuery or Angular JS, coders often encounter an error called Uncaught ReferenceError: $ is not defined. This usually happens when the $, that may be a variable or a method is not properly defined but is being used.


1 Answers

I am using a react application and just adding the below statement in your mock file resolved the issue. Hope this might be helpful for you or others.

import 'isomorphic-fetch'

like image 82
bhanu prakash Avatar answered Sep 30 '22 04:09

bhanu prakash