0
I’m starting in TDD, I’m using jest
to learn. But I have a question:
I’m testing the following code :
export const search = (query, type) => {
return fetch(`https://api.spotify.com/v1/search?q=${query}&type=${type}`)
.then(dataJson => dataJson.json())
.then(res => res)
.catch(err => err)
}
export const searchArtists = query => search(query, 'artist')
export const searchAlbums = query => search(query, 'albums')
export const searchTracks = query => search(query, 'tracks')
export const searchPlaylists = query => search(query, 'playlists')
Testing :
it('should searchArtist return data from promise', () => {
const resposta = Promise.resolve({ name: 'drake' })
const fakeJson = Promise.resolve({ json: () => resposta })
mockFetch.mockImplementation(() => fakeJson)
expect(searchTracks('drake')).resolves.toEqual({ name: 'drake' })
})
The test thus passes, but if I change the tested code and use async/await
:
export const search = (query, type) => {
const dataJSON = await fetch(`https://api.spotify.co/v1/search?q=${query}type=${type}`)
const response = dataJSON.json()
return response
}
export const searchArtists = query => search(query, 'artist')
export const searchAlbums = query => search(query, 'albums')
export const searchTracks = query => search(query, 'tracks')
export const searchPlaylists = query => search(query, 'playlists')
This error appears :
'Test suite failed to runReferenceError: regeneratorRuntime is not defined'
my . babelrc is so :
{
 "presets": [
 [
 "@babel/preset-env",
 {
 "targets": {
 "node": "current",
 "edge": "14",
 "firefox": "51",
 "chrome": "54",
 "safari": "10"
 }
 }
 ]
 ],
 "plugins": ["@babel/plugin-proposal-class-properties"]
}
– Gabriel Antunes