0

I'm writing tests for my TypeScript project and I'm having issues when mocking two functions in the same file.
This is my structure:

  • api-helper.ts
  • api-helper.test.ts

The functions sendRequest and checkAccountActivatedByEmail are inside api-helper.ts

This is my test:

describe('Test for api-helper.ts', () => {
  let context: Context;

  beforeEach(() => {
    context = {
      log: () => {},
      executionContext: {
        invocationId: '1',
      },
    } as unknown as Context;
    jest.clearAllMocks();
    jest.resetAllMocks();
  });

  test('checkAccountActivatedByEmail: valid and activated', async () => {
    // jest.mock('./api-helper', () => ({
    //   sendRequest: jest.fn().mockResolvedValue({
    //     status: 200,
    //     json: () => ({ id: 'xx', activated: true }),
    //   }),
    // }));

    jest.spyOn(utils, 'sendRequest').mockResolvedValue({
      status: 200,
      json: () => ({ id: 'xx', activated: true }),
    } as any);
    const resp = await utils.checkAccountActivatedByEmail(context, 'xx');
    expect(resp).toStrictEqual({ activatedByEmail: true, oktaId: 'xx' });
  });
});

The commented lines above are just to show you different ways I'm trying to mock it. Seeing the console I do realize checkAccountActivatedByEmail it's calling the real sendRequest function.

What am I doing wrong for the sendRequest not to be mocked?

I have already tried to mock in different ways and also mockImplementation.

0 Answers0