1 year ago

#68896

test-img

Nabeel Munir

Testing @shopify/shopify-api with jest

I am using shopify-api module in my application and I want to test it by mocking it with jest.

The shopify-api is a class based module and in that module, I want to mock the post method in a way that I want this post function to return some data. Below is how I have tried it.

it('imports product into shopify', async () => {
    const client = new Shopify.Clients.Rest(
      shopifyMocks.shop,
      shopifyMocks.accessToken
    );
    client.post.mockResolvedValue(shopifyMocks.shopifySuccessResponse);
    const shopifyResponse = await importIntoShopify(
      shopifyMocks.shop,
      shopifyMocks.accessToken,
      amazonMocks.shopifyParsedResponse
    );
    expect(shopifyResponse).toMatchObject(shopifyMocks.shopifySuccessResponse);
  });

The whole test module is below.

import { importIntoShopify } from '../server/Shopify';
import { shopifyMocks } from '../__mock__/shopifyMocks';
import { amazonMocks } from '../__mock__/amazonMock';
import Shopify, { DataType } from '@shopify/shopify-api';

jest.mock('@shopify/shopify-api');

describe('shopify functions test', () => {
  const unmockedFetch = global.fetch;

  beforeAll(() => {
    jest.useFakeTimers();
    jest.setTimeout(60000);
  });

  afterAll(() => {
    global.fetch = unmockedFetch;
    jest.clearAllTimers();
    jest.clearAllMocks();
  });

 
it('imports product into shopify', async () => {
    const client = new Shopify.Clients.Rest(
      shopifyMocks.shop,
      shopifyMocks.accessToken
    );
    client.post.mockResolvedValue(shopifyMocks.shopifySuccessResponse);
    const shopifyResponse = await importIntoShopify(
      shopifyMocks.shop,
      shopifyMocks.accessToken,
      amazonMocks.shopifyParsedResponse
    );
    expect(shopifyResponse).toMatchObject(shopifyMocks.shopifySuccessResponse);
  });

  it('throws when shopify request is unsucsessful', async () => {
    global.fetch = () =>
      Promise.resolve({
        json: () => Promise.resolve(shopifyMocks.shopifyUnsuccessfulResponse),
      });
    await expect(
      async () => await importIntoShopify(shopifyMocks.shop, 'dsdasdasd', {})
    ).rejects.toThrow('Internal server error.');
  });
});

However, I cannot get the post method to return anything. Once I mock the shopify-api method, it returns undefined for all of the methods and I cannot override a method to return data that I want this mock method to return.

I get the following error once i run the test

shopify functions test › imports product into shopify
expect(received).toMatchObject(expected)
Matcher error: received value must be a non-null object
Received has value: undefined

Below is the code for importIntoShopify() function.

export const importIntoShopify = async (shop, accessToken, data) => {
  try {
    const client = new Shopify.Clients.Rest(shop, accessToken);
    const response = await client.post({
      path: 'products',
      data: data,
      type: DataType.JSON,
    });
    return response;
  } catch (err) {
    throw new Api500Error(err);
  }
};

How can I mock the post method from shopify-api to return something other than undefined?

javascript

unit-testing

jestjs

shopify

shopify-api

0 Answers

Your Answer

Accepted video resources