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

473
Views
TypeError: no se pueden leer las propiedades de undefined (leyendo 'entonces') - mientras Mocking Fetch llama en Jest

Este es el archivo que contiene la llamada de búsqueda, que solo envía un archivo json almacenado localmente.

 // eslint-disable-next-line import/prefer-default-export export const get = () => { // eslint-disable-next-line no-undef return fetch('data/posts.json').then((res) => res.json()); };

Mi prueba en la que me burlo del método de fetch y Promise.resolve una matriz simulada.

 import { get } from './posts'; const mockData = [ { "id": "ig-1", "accountId": "IG", "accountIcon": "/images/ig-icon.svg", "accountName": "IG account", "accountImageInitial": "J", "imageUrl": "/images/social_logo.png", "caption": "test", "timestamp": 1635510651638 }, { "id": "fb-1", "accountId": "FB", "accountIcon": "/images/fb-icon.svg", "accountName": "FB account", "accountImageInitial": "J", "imageUrl": "/images/social_logo.png", "caption": "test", "timestamp": 1635510051638 } ]; global.fetch = jest.fn(() => Promise.resolve({ json: () => Promise.resolve(mockData) }) ); describe('The posts API controller', () => { test('get() returns expected default payload', async () => { const result = await get(); expect(fetch).toHaveBeenCalledTimes(1); expect(result).toBeTruthy(); }); });

Error

TypeError: no se pueden leer las propiedades de undefined (leyendo 'entonces')

 TypeError: Cannot read properties of undefined (reading 'then') 2 | export const get = () => { 3 | // eslint-disable-next-line no-undef > 4 | return fetch('data/posts.json').then((res) => res.json()); | ^ 5 | };

ingrese la descripción de la imagen aquí

¿No está seguro de por qué recibo este error, ya que parece que me he burlado de la búsqueda correctamente?

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

0

Sugeriría simplemente devolver el res.json() , que en realidad ya está haciendo implícitamente. Además, después de .then() es una buena práctica tener .catch() en caso de que encuentre un error durante la búsqueda.

Entonces, intenta seguir.

 export const get = () => { fetch('https://jsonplaceholder.typicode.com/posts') .then((res) => res.json()) // this is an implicit return .catch((err) => console.log(err)); };

o usando async/await

 const get = async () => { try { const res = await fetch('https://jsonplaceholder.typicode.com/posts'); return res.json(); } catch (error) { console.log(error); } };
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!