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

201
Views
Caso de prueba de unidad de llamada API de React Js

Estoy tratando de escribir casos de prueba para mi API a la que llamo, pero no sé qué estoy haciendo mal para obtener errores como este.

coche.prueba.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); }); }); });

Configuración de importación de GetCar desde "../../../Config/Api";

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

Aquí está el error en la imagen que he adjuntado, ¿alguien puede ayudarme a resolver mi caso de prueba que puedo escribir también para otra llamada a la API? ingrese la descripción de la imagen aquí .

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

testData NO es una matriz, es por eso que data[0] te da undefined . Debería ser:

 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 Report
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!