'Failed to mock imports in typescript

I'm trying to mock '@unleash/proxy-client-react' by using the mocked function of ts-jest/utils

I did the following

I've created a separate file with content

jest.mock('@unleash/proxy-client-react', () => ({
  useUnleashContext: jest.fn(),
  useFlag: jest.fn()
}));

This file is added to the list setupFilesAfterEnv of the jest configuration

In test code:

import { useUnleashContext } from '@unleash/proxy-client-react';
jest.mock('@unleash/proxy-client-react');

and in the beforeEach I call

mocked(useUnleashContext).mockClear();

This works but when I try to do the same for useFlag I get the following error:

TypeError: (0 , utils_1.mocked)(...).mockClear is not a function

Can someone help me understand what is happening?

Thanks in advance, Marco



Solution 1:[1]

The only way I was able to make it works is like this:

jest.mock('@unleash/proxy-client-react', () => ({
  useUnleashContext: () => jest.fn(),
  useFlag: jest.fn()
}));

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Daniel DeschĂȘnes