-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
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
Cannot mock relative URLs in v3.0.4 #46
Comments
I see. So I need to use whatwg-fetch's Request implementation probably. Will have a look in the new year |
Or here https://github.com/wheresrhys/fetch-mock/blob/master/src/fetch-mock.js#L65 I can most likely just return a Request-like object as I'm only interested in comparing a few properties, not the internals |
fixed in 3.1.1. Cheers for raising the issue |
Thanks for this! |
Sorry to open this old issue, but I came across the same error (
This is the stack trace:
I get the same error with |
try importing isomorphic fetch before fetch mock. And you don't need to assign it to a local variable - isomorphic fetch will already set itself as a global |
No luck unfortunately :( Thanks for your reply, will probably try to mock the whole thing. |
If your code is public on github I don't mind having a look to see what the problem is. There's probably something in the way you're requiring your js that I haven't considered, and which is causing node-fetch to be included instead of whatwg-fetch and I'd like to either fix it or add to the troubleshooting guide. |
I'm sure I am. It's not public, but this is basically it:
|
I had exactly this problem, using fetch-mock 4.1.1 and isomorphic-fetch 2.2.1. To resolve it I had to do all of the following:
(edited following further simplification) Hope this helps. |
@dr-nick sorry but I am very confused:
Thank you very much for your time |
@ericat my bad, I'd been through so many permutations trying to make it work that I lost the power of writing!
Here's some barebones working code: fetch.js import 'whatwg-fetch'; // don't specify `as` or `from` as this polyfills into global namespace anyway
...
export function saveQuery(query) {
return dispatch => {
dispatch(storeQuery(query));
fetch(`SAVE_URL${query}`))
.then(
resp => dispatch(receiveResults(resp)),
error => dispatch(logError(error))
);
};
} FetchTest.js import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import fetchMock from 'fetch-mock';
import 'isomorphic-fetch'; // don't specify `as` or `from` as this polyfills into global namespace anyway
import * as actions from 'actions/fetch';
...
describe('fetch async creators', () => {
const middlewares = [ thunk ];
const mockStore = configureMockStore(middlewares);
afterEach(() => {
fetchMock.restore();
});
it('should dispatch expected actions for saving a query', (done) => {
fetchMock.mock(/SAVE_URL.+/, {
savedTerm: true,
});
const priorState = {};
const expectedActions = [
{
type: STORE_QUERY,
query: 'some query',
}
];
const store = mockStore(priorState, expectedActions, done);
store.dispatch(actions.saveQuery('some query'));
});
}); tl;dr: replace |
@ericat, did you ever manage to resolve this issue? I am running into it now with my test suite... @dr-nick's suggestion didn't solve the problem but it did bring about a different error - after reorganising my imports in tests and swapping to whatwg-fetch in my action creators I then got |
Getting fetch(myApiRoute, {method: 'POST'}) Edit: fetchMock.useNonGlobalFetch(fetch)
fetchMock.mock(myApiRoute, 'POST', payload) |
I plan to make the behaviour a bit better anyway. A colleague of mine raised this issue a few days ago too |
The |
@wheresrhys it seems like some people have resolved this issue but I'm doing everything the same and still getting the error from node-fetch: Error: only absolute urls are supported The only thing I've noticed about the example from @dr-nick that's different than mine (and I understand it is just a snippet and not full working code) is that it's not testing the response from the async action (using a then statement). Once I try to return the response from the async request, I still get the error, and I don't see a way that error wouldn't be thrown judging by the stack trace and the construct of the Request object. Sorry to reopen this but I just wanted to know if this was considered resolved or if there was still being more done to solve it. Here's a snippet of my test that's failing: describe("async", function() {
beforeEach(function() {
fetchMock.useNonGlobalFetch(fetch);
fetchMock.mock(API_URL, { body: { response: ['value'] }});
})
afterEach(function() {
fetchMock.restore();
})
it("runs fetch and collects response", function() {
const store = mockStore({entities: {}});
return store.dispatch(actions.action())
.then(() => {
expect(true).to.equal(true);
})
})
}) |
@sstrong-pica9 - did you ever figure this out? I'm still getting the error as well. |
Reopening as it's obviously still an ongoing problem. But to investigate I need somebody to post a minimal test case i.e. a repo I can clone, install and attempt to run a test on. It's not an issue I've ever been able to recreate. I have recently (v4.5.4) fixed a bug whereby mocking fetch was not properly scoped to a test/test-group, which may have some bearing on this issue |
Thanks @wheresrhys . I'll see if I can extract to a repo this weekend... I am using 4.5.4, and will test with an older version as well. Let you know how it goes. |
I think there's too many outside dependencies on my side to confirm this is a fetch-mock issue. (I switched to another fetch mocking utility and got the same result). Going to dig further, there's definitely an issue, just not sure where its coming from. |
I am having the same issues... "[Error: only absolute urls are supported]" @dr-nick 's solution worked at first, but then I pulled in https://github.com/redux-effects/redux-effects-fetch and my tests broke with this error again... |
@sallakarppinen sorry for the late reply! I am using nock at the moment, everything appears to be working fine. I have put the test in a gist, in case it's useful to someone https://gist.github.com/Ericat/84151d8abdde2e1f228f39bea58b3f04 |
I ran into this in v5.0 as well. As far as I can tell, the issue seems to be related to use of a non-global
Note that Using |
There are lots of notes on use with non-global fetch here http://www.wheresrhys.co.uk/fetch-mock/installation. If they don't address your problem please provide a minimal test case of your problem (ideally as a repo I can clone, install and attempt to run a test in) |
Oh, I don't actually have a problem anymore, using global everywhere solved it. Was just hoping to help others that ran into the same problem. It's just weird to be using globals again after so long, but that's the fetch standard for you =) I don't really think it's a bug if |
same error, does anyone figure it out ? I am using NOCK as the API mock tool, and using thunk as async middleware action |
I can confirm that for me at least, in my entry point to my app,
import 'isomorphic-fetch'
import fetchMock from 'fetch-mock' totally works. |
As of v3.0.4, mocking a relative URL results in an
Error
being thrown from the dependency modulenode-fetch
:In my use case, I am writing tests for a Redux application which uses
fetch
to grab some i18n data defined in a JSON file. The code I am testing looks like this:The tests are written in
mocha
and are executed through Node.js, so I useisomorphic-fetch
assigned to theglobal
object inside the test, to fill in for the browser'sfetch
function, and usefetch-mock
to mock the response:This code worked fine in v3.0.3 as
fetch-mock
wasn't usingRequest
fromnode-fetch
, which sets the absolute URL limitation (LIMITS.md innode-fetch
repository).Related issue in
node-fetch
: node-fetch/node-fetch#43The text was updated successfully, but these errors were encountered: