0

This is the import statement that I used:

import * as mixpanelTest from '../../../../utils/mixpanel';

This is the mock function I used:

jest.mock('../../../../utils/mixpanel', () => {
  const actualModule = jest.requireActual(
    '../../../../utils/mixpanel',
  );
  return {
    __esModule: true,
    ...actualModule,
  };
});

This is the test case I used:

it('calls mixpanel with correct data when "Publish" button is clicked after creating new plan', async () => {
      renderComponent({
        initialJotaiValues: [
          [currentAdminAtom, ADMIN],
          [actionPlansAtom, [actionPlanWithReminders, activeActionPlan]],
        ],
        apolloMocks: [MOCK_CREATE_ACTION_PLAN_SUCCESS],
        actionPlanFormContainerProps: { ...baseProps, isNewActionPlan: true },
      });

      const nextButton = screen.getByRole('button', {
        name: ACTION_PLAN_FORM_BUTTONS.NEXT,
      });

      userEvent.click(nextButton);

      const publishButton = screen.getByRole('button', {
        name: ACTION_PLAN_FORM_BUTTONS.PUBLISH,
      });

      userEvent.click(publishButton);

      await waitFor(() => {
        expect(mixpanelTrackSpy).toHaveBeenCalledWith(
          MIXPANEL_EVENT_LABELS.PUBLISH_PLAN,
          expect.objectContaining({
            AdminId: ADMIN.id,
            AdminRole: ADMIN.role,
            MemberId: MOCK_USER.id,
            PlanType: actionPlanWithReminders.planType,
            PlanNumber: 3,
            PublishType: MIXPANEL_EVENT_LABELS.PUBLISH_TYPE_NEW_PLAN,
          }),
        );
      });
    });

I tried the above code but I'm getting this error `expect(jest.fn()).toHaveBeenCalledWith(...expected)

Expected: "Publish Action Plan Button Clicked", ObjectContaining {"AdminId": "123", "AdminRole": "coach", "MemberId": "1234", "PlanNumber": 3, "PlanType": "Co-created plan", "PublishType": "New Plan"}

Number of calls: 0`

This is caused because of the implementation of swc/jest over ts/jest.

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • It's not caused by the implementation of `@swc/jest`. The problem is that you are trying to do something is disallowed by the ESM specification. Read https://github.com/swc-project/swc/issues/5205 – kdy Aug 31 '23 at 05:32

0 Answers0