Help with jest ERROR : 'Test suite failed to runReferenceError: regeneratorRuntime is not defined'

Asked

Viewed 275 times

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'

1 answer

0

You can post your package.json and . babelrc?

I found this reply on an Issue on Github that can solve your problem.

From what I saw jest used @Babel/polyfill to include the regeneratorRuntime function, however in the latest versions this lib was removed and it was up to the developer to add it to the project.

In my project I have no package.json o jest: 23.5.0 and @Babel/polyfill: 7.2.5 working perfectly.

  • 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"]
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.