[英]jest.mock('./targetJSFile') is not importing the right functions
I'm trying to understand why Jest is not allowing me to import functions from _ mocks _/http.js.我试图理解为什么 Jest 不允许我从 _ mocks _/http.js 导入函数。
This is my catchAnalysis.js file这是我的 catchAnalysis.js 文件
import { fetchNewsData } from './http'
async function catchAnalysis(params) {
console.log("::: Form Submitted :::")
fetchNewsData(params)
.then(response => /* Blablabla */)
.catch(error => console.log('error', error));
}
This works great.这很好用。 Below is my catchAnalysis.test.js file:下面是我的 catchAnalysis.test.js 文件:
jest.mock('./http')
import { fetchNewsData } from './http'
test("Mock API works", () =>{
let testing = fetchNewsData()
console.log(`fetchNewsData value is ${testing}`)
/* Blablabla */
})
fetchNewsData() is undefined when I run JEST tests with npm.当我使用 npm 运行 JEST 测试时,fetchNewsData() 未定义。
Am I using jest.mock('') incorrectly?我是否错误地使用了 jest.mock('') ? I don't know where to start to debug.我不知道从哪里开始调试。
For context, this is the _ mocks _/http.js file.对于上下文,这是 _ mocks _/http.js 文件。 The real file is a simple fetch API call.真正的文件是一个简单的 fetch API 调用。
function fetchNewsData(){
console.log("::: MOCK API :::")
let responseData = {
"agreement": "DISAGREEMENT",
"confidence": "86",
"irony": "NONIRONIC",
"model": "general_en",
"score_tag": "P",
"status": {
"code": "0",
"msg": "OK",
"credits": "3",
"remaining_credits": "19849"
},
"subjectivity": "SUBJECTIVE"
}
console.log(`response Text is ${response.text()}`)
return Promise.resolve(response)
}
export {
fetchNewsData
}
Thank you!谢谢!
I found the answer...我找到了答案...
Misread document -> the mock folder has to be in the same directory as the file being mocked.误读文档 ->模拟文件夹必须与被模拟的文件位于同一目录中。 I had put it in src.我已经把它放在了 src 中。
I didn't expect folder location to be the issue!我没想到文件夹位置会成为问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.