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

124
Vistas
React. how to mock api call NOT IN TESTS

E.g. I have a problem when some endpoints are down or causing errors, but I still need to develop. I have something like UserService.getUsers I am aware of mocking functions in jest and so on, but I have no idea how I'd better get fake data which I can specify myself.

I thought about a HOC, which redefines functions I need to mock like

UserService.getUsers=Promise.resolve({});

But I don't think this is the best way to do this

NOTE: That's not related to testing stuff

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

0

If you're using Webpack, you could use the resolve property of the Webpack configuration to alias your imports to some local mocked version.

https://webpack.js.org/configuration/resolve/

For example, if you had some import like this:

import UserService from 'some-package-or-path';

const myData = UserService.getUsers();

You can alias some-package-or-path to a local file:

// webpack.config.js
const path = require('path');

module.exports = {
  //...
  resolve: {
    alias: {
     'some-package-or-path': path.resolve(__dirname, 'src/mocks/userServiceMock'),
    },
  },
};

// userServiceMock index.js
const mock = {
  getUserData: () => {
    return {
      mockedDataProperties: 'whatever'
    }
  }
};

export default mock;

Obviously your implementation will differ a bit but that's the gist of it.

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