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

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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