How to check code that uses whatwg-fetch with mocha?

I use https://github.com/github/fetch in my application that works fine, but I would like to test my code with Mocha and babel ever since I write ES2016.

This does not work. I get:

1) testApi.js Test api error handling:
 ReferenceError: fetch is not defined
  at callApi (callApi.js:10:10)
  at Context.<anonymous> (testApi.js:8:40)

Because well, the sample is not defined. When I create a selection for the browser, webpack is exposed.

I tried using https://github.com/bitinn/node-fetch , but the api is slightly different and requires, for example, a full URL instead of relative paths.

Is there a solution to this problem?

+4
source share
2 answers

, - :

  • isomorphic-fetch
  • webpack , ​​ 'web'
  • import 'isomorphic-fetch' index.js
  • import 'isomorphic-fetch' fetch ( , )
  • node -fetch, webpack whatwg-fetch
+1

Mocha node-fetch:

// Support server-side fetch for tests.
let fetch = (typeof window === 'undefined')
  ? require('node-fetch')
  : window.fetch
;

https://github.com/bitinn/node-fetch

0

All Articles