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

317
Visualizações
React app testing, how to test an axios post request with Jest?

App.js looks like this, how would I test it with jest?

 

    handleClick = e => {
        axios
          .post(
            "https://api.openweathermap.org/data/2.5/weather?q=" +
              this.state.term +
              "&units=metric&appid=" +
              ApiKey
             
          ){...}

I know I have to create a mocking axios file and then test it like that, but it uses setState in the main App.js. I just want to test the fetch request/axios post with jest.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just return promise of axios code check below code snippet

    // apiActions.js
    const axios = require('axios');
    
    // your api code goes here
    const fetchData = () => {
      return axios
        .get('https://jsonplaceholder.typicode.com/sample_url')
        .then(response => {
          return response.data;
        });
    };

    exports.fetchData = fetchData;

    // testSuit.js
    import fetchData from './apiActions.js';
    // your testing code goes here
    test('testing api call', () => {
    const sampleObj = {name:'',age:39....}
    fetchData().then(resp => {
      expect(resp).toMatchObject(sampleObj)
    })




        
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