2

I am mocking custom permission successfully like this:

jest.mock(
    "@salesforce/customPermission/my-custom-permission",
    () => {
        return {
            default: true
        }; 
    },
    { virtual: true }
);

In another test of the same component I would like to change return value of this mock or clear only this mock. I have tried to mockReturnValue, mockImplementation and everything I could find from the documentations. Does anyone have an idea how this could be solved?

Thanks!!

1 Answer 1

1

The lwc-recipes shows using different test classes to test different values of the imported permission. It shows

Where the negative version sets the mocked import to return false.

jest.mock(
    '@salesforce/customPermission/accessRestrictedUIPermission',
    () => ({
        __esModule: true,
        default: false
    }),
    { virtual: true }
);

and the other test sets it to true.

jest.mock(
    '@salesforce/customPermission/accessRestrictedUIPermission',
    () => ({
        __esModule: true,
        default: true
    }),
    { virtual: true }
);
2
  • 1
    I couldn't get this to work - that is I couldn't change the response from the mocked imported module after it had first being initialised. All I could do was split the test into two distinct files and initialise each with default: true or default: false based on which I was testing. It's worth noting that this is what the recipe repo now does since this Pull Request: github.com/trailheadapps/lwc-recipes/pull/774/files Commented Mar 24, 2023 at 9:10
  • 1
    Thanks for the heads up - I updated the answer to match with the recipe change. Commented Mar 24, 2023 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .