We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I am using FetchMock with Mockery as Node 4 module, hence I need to get a handle on the mocked fetch() function.
Registering the node-fetch mock with myMock.getMock() as shown below leads to an undefined fetch function in the code under test.
myMock.getMock()
'use strict'; var fetchMock = require('fetch-mock'); var mockery = require('mockery'); describe('fetch mockery', function () { beforeEach(function () { mockery.enable({useCleanCache: true}); var myMock = fetchMock .mock('http://auth.service.com/user', '{"foo": 1}'); mockery.registerMock('node-fetch', myMock.getMock()); <-- undefined }); afterEach(function () { fetchMock.restore(); mockery.deregisterMock('node-fetch'); mockery.disable(); }); it('should mock a request', function () { var goFetch = require('../lib/fetchsample'); // just a call to fetch in a separate module return goFetch('http://auth.service.com/user').then(function (response) { return response.json(); }).then(function (json) { console.log(json); }); }); });
As a workaround, the necessary fetch function mock is availabe as myMock.mockedContext.fetch:
myMock.mockedContext.fetch
mockery.registerMock('node-fetch', myMock.mockedContext.fetch);
Maybe the fix would be to change getMock() in fetch-mock.js like so:
getMock()
/** * getMock * Returns a reference to the stub function used to mock fetch * @return {Function} */ getMock () { return this.mockedContext.fetch; }
Or am I using fetch-mock incorrectly here?
The text was updated successfully, but these errors were encountered:
thanks for pointing this out. Not sure what in v4 would've broken it, or why the tests still pass. I'll fix the tests and add your solution
Sorry, something went wrong.
Ah no, you're missing fetchMock.useNonGlobalFetch(require('node-fetch')); before your tests. That should fix it.
fetchMock.useNonGlobalFetch(require('node-fetch'));
No branches or pull requests
I am using FetchMock with Mockery as Node 4 module, hence I need to get a handle on the mocked fetch() function.
Registering the node-fetch mock with
myMock.getMock()
as shown below leads to an undefined fetch function in the code under test.As a workaround, the necessary fetch function mock is availabe as
myMock.mockedContext.fetch
:Maybe the fix would be to change
getMock()
in fetch-mock.js like so:Or am I using fetch-mock incorrectly here?
The text was updated successfully, but these errors were encountered: