0

I'm new to testing and I'm trying to find a way to throw an error with an async function.

Here's the test file

import Yelp from './Yelp';
import axios from 'axios';

jest.mock('axios');

describe('testing searchRestaurantsInfo', () => {

  // Other tests

  test('returns error', async () => {
    await expect(
      Yelp.searchRestaurantsInfo('q_IoMdeM57U70GwqjXxGJw')
    ).rejects.toThrow('Error');
  });
});

I get the error

testing searchRestaurantsInfo › returns error

    expect(received).rejects.toThrow()

    Received promise resolved instead of rejected
    Resolved to value: "Error"

      102 | 
      103 |   test('returns error', async () => {
    > 104 |     await expect(
          |           ^
      105 |       Yelp.searchRestaurantsInfo('q_IoMdeM57U70GwqjXxGJw')
      106 |     ).rejects.toThrow(TypeError);
      107 |   });

And here's Yelp.js

import axios from 'axios';

let YELP_API_KEY = process.env.REACT_APP_YELP_API_KEY;

const Yelp = {
  // Provides info about a single restaurant
  async searchRestaurantsInfo(id) {
    try {
      let response = await axios.get(
        `https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/${id}`,
        {
          headers: {
            Authorization: `Bearer ${YELP_API_KEY}`,
            'X-Requested-With': 'XMLHttpRequest',
            'Access-Control-Allow-Origin': '*',
          },
        }
      );

      let responseRew = await axios.get(
        `https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/${id}/reviews`,
        {
          headers: {
            Authorization: `Bearer ${YELP_API_KEY}`,
            'X-Requested-With': 'XMLHttpRequest',
            'Access-Control-Allow-Origin': '*',
          },
        }
      );

      const parameters = {
        name: response.data.name,
        address: response.data.location.display_address[0],
        coordinates: {
          lat: response.data.coordinates.latitude,
          lng: response.data.coordinates.longitude,
        },
        city: response.data.location.display_address[1],
        rating: response.data.rating,
        photos: response.data.photos,
        phone: response.data.phone,
        price: response.data.price,
        categories: response.data.categories[0].title,
        url: response.data.url,
        reviews: responseRew.data.reviews,
      };

      return parameters;
    } catch (e) {
      console.log(e);
      return 'Error';
    }
  },
}

I've been looking for a solution, and actually the one I came up with comes from here: Can you write async tests that expect toThrow? but it's not working for me and I don't get what I'm doing wrong.

Thanks for your help!

Mugg84
  • 585
  • 1
  • 7
  • 15
  • 2
    This is directly tied to the comment from your previous question. If you expect an error to be thrown, don't catch it. – Estus Flask Aug 04 '20 at 14:23

1 Answers1

0

Assuming you're mocking axios.get to reject before you assert a promise rejection in your test, there's an issue in Yelp.js in the catch block. You need to rethrow the error:

catch (e) {
      console.log(e);
      throw e;
}

In case you haven't mocked axios to throw an error, add this before you call the function:

axios.get.mockRejectedValue('error');

Let me know if this worked for you.

qaismakani
  • 441
  • 2
  • 7