Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
React Js API call Unit Test Case

I m trying to write test cases for my API I call but I don't know what I am doing wrong that I am getting errors like this.

car.test.js

import { getCar } from "../Service/MainScreen/Cars/Car";

describe("Car  API Call function", () => {
  test("it should pass the test", () => {
    const testData = {
      id: 186,
      plate_number: "WW12345",
      status_display: "Aktywny",
      driver_name: "Jakub Nowak",
      driver_id: 181,
      created_at: "2021-08-12T09:07:30Z",
      vin: "WBADM123123123123",
      brand: "Ford",
      model: "Focus",
      "Brand-Model": "Ford/Focus",
      production: "2016",
      registration: "01-12-2020",
      insurance_date: "06-06-2023",
      has_registration_docs: true,
      has_policy_status: true,
      has_certificate: true,
      has_green_card: true,
      has_vehicle_card: true,
      has_car_picture: true,
      fleet_entry: "20-04-2018",
      monitoring_start: "12-08-2021",
      status: "Aktywny",
      Estimaed_distance: 29500,
      Estimaed_fuel: 1475,
      average_fuel: 5,
      claim: 1,
      cost: 1000,
    };

    const response = { json: jest.fn().mockResolvedValueOnce(testData) };
    global.fetch = jest.fn().mockResolvedValueOnce(response);

    return getCar().then((data) => {
      expect(data[0]).toEqual(testData);
    });
  });
});

GetCar import config from "../../../Config/Api";

export async function getCar() {
  return fetch(config.fakeapi.car, config.fakeHead)
    .then((response) => {
      return response.json();
    })
    .catch((reject) => console.log(reject));
}

Here is the error in the image that I have attached can someone please help me to resolve my test case that I can write also for another API call enter image description here.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

testData is NOT array, that's why data[0] gives you undefined. It should be:

const response = { json: jest.fn().mockResolvedValueOnce(testData) };
global.fetch = jest.fn().mockResolvedValueOnce(response);

return getCar().then((data) => {
  expect(data).toEqual(testData);
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda