Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

678
Views
expect(received).toEqual(expected) // deep equality - Throws this error when the response has the ternary condition

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!