phkavitha
phkavitha

Reputation: 847

fetch-mock: GLOBAL is not defined

I'm using fetch-mock for writing unit test cases for functions which use isomorphic-fetch. Here is the sample test suite:

 import connect from '../../src/utils/connect';
 import fetchMock from 'fetch-mock';

 describe('Unit | Utils | connect function', () => {
  it('should make a request', (done) => {
     const urlToTest = 'http://localhost:3000/api/v1/users';
     fetchMock
      .mock(urlToTest, 200)
      .getMock();

    const response = connect(urlToTest);
    expect(response);
    done();
  });
});

When running the test cases, I get the error

/my_app/app/node_modules/fetch-mock/src/server.js: GLOBAL is not defined

I'm not able to debug this issue. Can anyone please let me know what am I doing incorrectly. Thanks

Upvotes: 2

Views: 3066

Answers (1)

Yuri Vorontsov
Yuri Vorontsov

Reputation: 41

If you are using jest, add setupTestFrameworkScriptFile section to package.json:

{
  ...
  "jest": {
    ...
    "setupTestFrameworkScriptFile": "setupTestFrameworkScriptFile.js"
  }
}

Add this line to setupTestFrameworkScriptFile.js:

global.GLOBAL = global;

Upvotes: 1

Related Questions