0

I have been working through trailhead from link below https://trailhead.salesforce.com/en/content/learn/modules/test-lightning-web-components

and noticed I got a console.warn error

console.warn registerApexTestWireAdapter is deprecated. More details: https://github.com/salesforce/wire-service-jest-util/blob/master/docs/migrating-from-version-2.x-to-3.x.md

I looked over the documentation was a little unsure of how to proceeded with it. If registerApexTestWireAdapter is deprecated would I be safe to assume that it should not be used in a project? Also if I wanted to do a jest test for a LWC component that was say as simple as the code shown below. What would be the desired alternative to not receive an console.warn message?

@wire(Contacts)contacts;

what would be the desired alternative to

import { registerApexTestWireAdapter } from '@salesforce/sfdx-lwc-jest';
import getContacts from '@salesforce/apex/practiceLWCController.getContacts';

// ...test below here

it('displays contacts', () => {
        const element = createElement('practice-lwc', {
            is: PracticeLWC
        });

        document.body.appendChild(element);
        getContactsAdapter.emit(mockContacts);

        return Promise.resolve().then(() => {
            const contacts = Array.from(element.shadowRoot.querySelectorAll('.contact'));
            expect(contacts.length).toBe(3);
        })

    });

1 Answer 1

4

createApexTestWireAdapter was added to replace registerApexTestWireAdapter. From the documentation you linked

Notice that when testing an apex method used with @wire, you should change your apex method mock to use createApexTestWireAdapter

You can remove your references to registerApextestWireAdapter and do the following to mock a wired custom apex call

import getContacts from '@salesforce/apex/practiceLWCController.getContacts';


jest.mock(
    "@salesforce/apex/practiceLWCController.getContacts",
    () => {
        const { createApexTestWireAdapter } = require("@salesforce/sfdx-lwc-jest");
        return {
            default: createApexTestWireAdapter(jest.fn()),
        };
    },
    { virtual: true }
);

//later in your tests

getContacts.emit(mockContacts);

Another good reference would be the lwc-recipes which demonstrates this as well for your specific scenario - apexWireMethodToProperty.test.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.