Skip to content
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

Unable to mock when using isomorphic-fetch #41

Closed
vyorkin opened this issue Dec 8, 2015 · 5 comments
Closed

Unable to mock when using isomorphic-fetch #41

vyorkin opened this issue Dec 8, 2015 · 5 comments
Assignees

Comments

@vyorkin
Copy link

vyorkin commented Dec 8, 2015

Here is a minimal repo to reproduce the issue (there is an npm test task). I just can't intercept the request. The example is almost the same as the first one from README. Here is the karma ouput:

START:
Hash: 8344a6c0a9b3c44a5636
Version: webpack 1.12.9
Time: 10ms
webpack: bundle is now VALID.
webpack: bundle is now INVALID.
Hash: 205e381c2cff60ba2ea2
Version: webpack 1.12.9
Time: 431ms
         Asset    Size  Chunks             Chunk Names
tests/index.js  164 kB       0  [emitted]  tests/index.js
chunk    {0} tests/index.js (tests/index.js) 57.8 kB [rendered]
    [0] ./tests/index.js 398 bytes {0} [built]
    [1] ./tests/framework/fetch.test.js 611 bytes {0} [optional] [built]
    [2] ./tests/framework/plugins.test.js 559 bytes {0} [optional] [built]
    [3] ./~/es6-promise/dist/es6-promise.js 32.3 kB {0} [built]
    [4] ./~/fetch-mock/es5/client.js 215 bytes {0} [built]
    [5] ./~/fetch-mock/es5/fetch-mock.js 10.8 kB {0} [built]
    [6] ./~/isomorphic-fetch/fetch-npm-browserify.js 233 bytes {0} [built]
    [7] ./~/process/browser.js 2.06 kB {0} [built]
    [8] (webpack)/buildin/amd-define.js 85 bytes {0} [built]
    [9] (webpack)/buildin/module.js 251 bytes {0} [built]
   [10] ./~/whatwg-fetch/fetch.js 9.93 kB {0} [built]
   [11] ./src ^.+\.jsx?$ 160 bytes {0} [built]
   [12] ./tests .+\.test\.jsx?$ 222 bytes {0} [built]
   [13] vertx (ignored) 15 bytes {0} [optional]
webpack: bundle is now VALID.
09 12 2015 00:08:05.365:INFO [karma]: Karma v0.13.15 server started at http://localhost:9876/
09 12 2015 00:08:05.371:INFO [launcher]: Starting browser PhantomJS
09 12 2015 00:08:07.449:INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket l6IlGACQCBhgBzfKAAAA with id 12541479
  fetch-mock
    ✖ mocks simplest http get request (slow: 2.002 secs)
  karma plugins
    ✔ exposes "expect" globally
    ✔ exposes "should" globally
    ✔ has chai-as-promised helpers

Finished in 2.031 secs / 2.005 secs

SUMMARY:
✔ 3 tests completed
⚠ 1 test slow
✖ 1 test failed

FAILED TESTS:
  fetch-mock
    ✖ mocks simplest http get request (slow: 2.002 secs)
      PhantomJS 1.9.8 (Mac OS X 0.0.0)
    timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

npm ERR! Test failed.  See above for more details.
@wheresrhys
Copy link
Owner

I don't see where isomorphic fetch is being required in your code. Have you pushed all your files to github?

@vyorkin
Copy link
Author

vyorkin commented Dec 8, 2015

the isomorphic-fetch is defined globally using the webpack ProvidePlugin

@vyorkin
Copy link
Author

vyorkin commented Dec 8, 2015

here https://github.com/vyorkin-play/fetch-mock-example/blob/master/webpack.config.js#L30. I did it this way because the readme says:

When using isomorphic-fetch or node-fetch ideally fetch should be added as a global

@vyorkin vyorkin changed the title Unable to mock when using isomophic-fetch Unable to mock when using isomorphic-fetch Dec 8, 2015
@wheresrhys
Copy link
Owner

require('isomorphic-fetch') will already add fetch as a global (https://github.com/matthew-andrews/isomorphic-fetch/blob/master/fetch-npm-browserify.js#L1), so no need for a webpack loader. To avoid having to require isomorphic-fetch in every file you could npm install whatwg-fetch (this is the polyfill isomorphic fetch is built around) then in karma.conf.js include the fetch polyfill before your tests.

I'll add some clearer usage guides to the docs sometime

@vyorkin
Copy link
Author

vyorkin commented Dec 8, 2015

thanks @wheresrhys! got it working, btw I think I've found a typo here, as I can see it returns an object (not array) if no argument is given: https://github.com/wheresrhys/fetch-mock/blob/master/README.md#callsmatcher
https://github.com/wheresrhys/fetch-mock/blob/master/src/fetch-mock.js#L366

@vyorkin vyorkin closed this as completed Dec 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants