reactjstestingjestjsgatsbyimport-module

How to write test with Jest for getData() using module.exports for testing but requires ESM export syntax for React/Gatsby. Json file cannot parse


Below are my functions. I need these in my React/Gatsby app therefore I export it with ESM export syntax. but I cannot use it for testing.

problem to: see printscreen.. Jest doesn't seem to like the Json file? enter image description here

import gameOfThrones from "../data/gameOfThrones.json";

const getGameOfThronesData = ()=> {
    console.log(gameOfThrones.gameOfThrones.episodes)
}
    
const getEpisodesPerSeason = (season)=> gameOfThrones.gameOfThrones.episodes.filter(episode=> episode.season === season)
const getEpisodeData = (episodeId)=> gameOfThrones.gameOfThrones.episodes.find(episode=> episode.id === episodeId)
const getNextEpisode = episodeId => null
const getPrevEpisode = episodeId => null
    
export {getGameOfThronesData, getEpisodesPerSeason, getEpisodeData, getNextEpisode, getPrevEpisode}
    
// module.exports = {
//     getGameOfThronesData, getEpisodesPerSeason, getEpisodeData, getNextEpisode, getPrevEpisode
// };

Solution

  • Following setup on this page does the trick https://www.gatsbyjs.com/docs/how-to/testing/unit-testing/