LWC Jest – Mocking Apex Wire Adapter Emit Function Issue

lightning-web-componentslwc-jest

I am trying to set up a very simple test of a LWC apex wire adapter. This includes mocking the apex response with the contents of a JSON file as shown in the LWC docs.

...
import MyComponent from 'c/myComponent';
import myApexMethod from '@salesforce/apex/MyController.myApexMethod';

const mockResponse = require('./data/mockResponse.json');

describe('c-my-component', () => {
    
    afterEach(() => {
        while(document.body.firstChild) {
            document.body.removeChild(document.body.firstChild);
        }
        // Prevent data saved on mocks from leaking between tests
        jest.clearAllMocks();
    });

    describe('myApexMethod @wire data', () => {

        it('renders a table', () => {

            const element = createElement('c-my-component', {
                is: MyComponent
            });
            document.body.appendChild(element);

            // *** Failing here with "myApexMethod.emit is not a function" ***
            myApexMethod.emit(mockResponse);

            return Promise.resolve().then(() => {
                // Assertions
            })
        });
    });
});

However, I am getting a myApexMethod.emit is not a function error and can't figure out what the issue is.

  • I am definitely importing the method correctly.
  • The @salesforce/sfdx-lwc-jest library is installed.
  • I have checked that my sfdx cli is up to date.

My understanding is that I don't have to register the wire adapter any more. What have I missed?

Thanks

Best Answer

The answer was that I needed to upgrade the @salesforce/sfdx-lwc-jest package version. This also meant upgrading the rest of the devDependencies in the package.json file, and the api version in the sfdx-project.json file.