簡體   English   中英

如何使用 Jest 為 getData() 編寫測試,使用 module.exports 進行測試,但需要 React/Gatsby 的 ESM 導出語法。 Json 文件無法解析

[英]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

以下是我的功能。 我在我的 React/Gatsby 應用程序中需要這些,因此我使用 ESM 導出語法將其導出。 但我不能用它來測試。

問題:見打印屏幕.. Jest 似乎不喜歡 Json 文件? 在此處輸入圖像描述

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
// };

在此頁面上進行以下設置可以解決問題https://www.gatsbyjs.com/docs/how-to/testing/unit-testing/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM