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

Update @salesforce/apex/* imports to return jest.fn() #260

Merged
merged 5 commits into from
Aug 2, 2024

Conversation

nwcm
Copy link
Contributor

@nwcm nwcm commented Jun 4, 2024

After looking for ways to simplify mocking apex methods in a SFDX project, I found this transformer is applied. However the apex method transforms don't make the imports available within jests.

This change allows for jests import and use apex methods cleanly without having to mock in each test file.

This doesn't apply to wired apex methods which would need createApexTestWireAdapter() from @salesforce/sf

// foo.test.js
import apexMethod from "@salesforce/apex/ApexClass.apexMethod";

describe("c-foo", () => {
  afterEach(() => {
    // resetEnvironment();
  });
  
  
  it("Test mock", async () => {
     apexMethod.mockResolvedValue('value');
  }
}

Otherwise any apex method requires

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

Copy link

salesforce-cla bot commented Jun 4, 2024

Thanks for the contribution! Before we can merge this, we need @nwcm to sign the Salesforce Inc. Contributor License Agreement.

@nolanlawson
Copy link
Contributor

Thanks for the contribution! Could you clarify: is this a breaking change?

@nolanlawson
Copy link
Contributor

FYI @pozil @Templarian do you have strong opinions about this? Should we just return a jest.fn() so that people can mock directly?

@nwcm
Copy link
Contributor Author

nwcm commented Jun 17, 2024

Thanks for the contribution! Could you clarify: is this a breaking change?

Hey @nolanlawson, I don't believe so but can't be 100%. This change did not break any tests across our source which usually directly mock apex in each test file

@pozil
Copy link

pozil commented Jun 18, 2024

I like the idea of having a mock directly but I have not clue about the potential impact of such a change.

Copy link
Contributor

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be a breaking change. It looks reasonable enough to me.

OTOH could we please add documentation for this so that it's more discoverable? A good place would be this README. Thank you! 🙇

@nwcm
Copy link
Contributor Author

nwcm commented Jul 26, 2024

@nolanlawson I have made additions to the documentation as you suggest. Feel free to provide feedback, there wasn't anything too similar in the readme to align with

Copy link
Contributor

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small change to account for injectGlobals=false and I think this is good to go! nwcm#1

fix: import jest from `@jest/globals`
@nwcm
Copy link
Contributor Author

nwcm commented Jul 30, 2024

One small change to account for injectGlobals=false and I think this is good to go! nwcm#1

Good catch! Merged

Copy link
Contributor

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a bunch! Really appreciate the time you took to add docs and such. 🙇 Will release this in a bit.

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

Successfully merging this pull request may close these issues.

3 participants