Skip to content

@salesforce/apex mocked methods cannot be checked for .toHaveBeenCalled() #259

@nwcm

Description

@nwcm

I'm trying to check expect(apexMethod).toHaveBeenCalled() and .toHaveBeenCalledWith() while using @lwc/transformer however, i'm unable to utilise the provided mocked methods. Not sure if i'm missing something here. Currently looking at apex methods called imperatively not using wire.

Source

import apexMethod from "@salesforce/apex/ApexClass.apexMethod";

Test expect

expect(apexMethod).toHaveBeenCalled();

Error

expect(received).toHaveBeenCalled()

    Matcher error: received value must be a mock or spy function

    Received has type:  function
    Received has value: [Function apexMethod]

I can manually mock in each test but I'm looking to avoid that where possible

jest.mock(
 "@salesforce/apex/ApexClass.apexMethod",
 () => ({ default: jest.fn() }),
 { virtual: true }
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions