Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nwcm opened this issue Jun 3, 2024 · 2 comments
Open

Comments

@nwcm
Copy link
Contributor

nwcm commented Jun 3, 2024

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 }
);
@nwcm
Copy link
Contributor Author

nwcm commented Jun 4, 2024

I found the issue on this, code importing apex method with a import name different import this from @salesforce/apex/Class.bar;

@nwcm nwcm closed this as completed Jun 4, 2024
@nwcm nwcm reopened this Jun 4, 2024
@nwcm
Copy link
Contributor Author

nwcm commented Jun 4, 2024

It seems to me the @lwc/jest-transformer incorrectly returns a function for @salesforce/apex/Class.method imports rather than a jest.fn().

This requires every test to mock every apex import. I'll raise a PR with what seems to me a simple way to resolve this. But i'll need guidance from some of the contibuters if this will be fix for a change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant