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

312
Views
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 answers
Answer question

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