I have the following example test:
import { assert } from 'chai'
function starWarsMovies () {
fetch('http://swapi.co/api/films/')
.then((res) => {
return res.json()
})
.then((res) => res.count)
}
describe('Get star war movies', () => {
it('should get 7', () =>{
assert.equal(starWarsMovies(), 7)
})
})
But I getting
ReferenceError: fetch is not defined
What do I have to use in order to test a fetch request.
UPDATE
I also tried:
import { polyfill } from 'es6-promise'
import fetch from 'isomorphic-fetch'
but then I get:
AssertionError: expected undefined to equal 7
and I don't understand why.
Even if you use node-fetch or isomorphic-fetch the issue here is that you're checking for equality between a number and the result of a function that doesn't return anything. I was able to make this work!
Note that I'm using async await syntax here. There are lots of different ways we could do this (callbacks, promises, async/await), but the key is that in making a call out to an API we have to wait for the result. Also, the response you get from the star wars API seems to be a huge object, so I took the liberty of assuming that you were just checking for the count!