'Jest test: TypeError: Cannot read property 'then' of undefined

I am currently contacting the facebook api to send data through an axios call:

if (req.body.event === 'PageView') {
    try {
      await axios
        .post(facebookURL, {
          data: [
            {
              event_name: req.body.event,
              event_time: eventTime,
              action_source: 'website',
              event_source_url: `${req.body.serverAndPort}${req.body.url}`,
              user_data: {
                em: [email],
                fn: [firstName],
                ln: [lastName],
              },
              custom_data: {
                content_type: req.body.content_type,
              },
            },
          ],
        })
        .then(() => {
          return res.status(200).send();
        })
        .catch(error => {
          return res
            .status(500)
            .send('Error sending data to Facebook: ', error);
        });
    } catch (error) {
      throw new Error(error);
    } 

I added the .then() and catch() statement in as I was not receiving a repsonse. When I come to run my tests, I am now receiving the following error:

enter image description here

This is my current test:

const pageViewRequest = {
  body: {
    event: 'PageView',
    eventTime: 16452051,
    action_source: 'website',
    event_id: 'PR10032FF',
    locale: 'en-GB',
    content_type: 'PageView',
    firstName: 'Jesse',
    lastName: 'Heisenberg',
    email: '[email protected]',
    serverAndPort: 'localhost:1337',
    url: '/aPageViewTestUrl/',
  },
};


describe('submitFacebookEvent', () => {
  beforeEach(() => {
    axios.mockClear();
  });

  it('Should make post to correct url if event type is PageView', async () => {
    const req = createRequest(pageViewRequest);

    await submitFacebookEvent(req);

    expect(axios.post).toHaveBeenCalledWith(
      'https://graph.facebook.com/v12.0/PixelUK/events?access_token=accessTokenUK',
      {
        data: [
          {
            action_source: 'website',
            custom_data: {
              content_type: 'PageView',
            },
            event_name: 'PageView',
            event_source_url: 'localhost:1337/aPageViewTestUrl/',
            event_time: jasmine.anything(),
            user_data: {
              em: [
                '6c1e3f9b78926530cc06958ad80b7d0e986d8fe4a770a8276c1a7d226ddfdbf2',
              ],
              fn: [
                '1ecb9e6bdf6cc8088693c11e366415fe5c73662ecdf08f3df337924d8ea26adc',
              ],
              ln: [
                'fe81e342a460fec7bb935c27903620066d56bd5d09b3c96010ba7619d2020d8b',
              ],
            },
          },
        ],
      }
    );
  });

Any suggestions on how to correct this would be great. Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source