Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

665
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda