I am learning unit testing with Jest, This is the function I trying to test.
import axios from "axios";
export const call = async () => {
return axios.get("https://www.example.com").then((resp) =>{
const response = resp.data
let userData = {
title : response.title ? response.title : "",
url : response.url ? response.url : "",
date : response.date ? response.date : " ",
id : response.id ? response.id : "",
email: response.email ? response.email : ""
}
return Promise.resolve(userData)
})
}
here is the test.js file ---
import axios from "axios"
import {call} from './components/call'
jest.mock('axios')
const expectedResult = {
title:"hello"
}
const resp ={
data : expectedResult
}
describe("test", ()=>{
test("demo-test", async ()=>{
axios.get.mockResolvedValueOnce(resp)
const response = await call()
expect(response).toEqual(expectedResult)
console.log("axios.get() returns >>>", axios.get.mock.results[0]);
expect(axios.get).toHaveBeenCalledWith("https://www.example.com")
})
})
Here is the error I am getting
expect(received).toEqual(expected) // deep equality
- Expected - 0
+ Received + 4
Object {
+ "date": " ",
+ "email": "",
+ "id": "",
"title": "hello",
+ "url": "",
}
16 | axios.get.mockResolvedValueOnce(resp)
17 | const response = await call()
> 18 | expect(response).toEqual(expectedResult)
| ^
19 |
20 | console.log("axios.get() returns >>>", axios.get.mock.results[0]);
21 | expect(axios.get).toHaveBeenCalledWith("https://google.com")
FAIL src/call.test.js (7.597 s)
In the call.js, when userData dosen't have ternary conditions then the test passes. But when I put ternary condition the test gets failed with the above error. Please help me. How to resolve this issue.