function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Manjunath MManjunath M 

Lightning Web Component Test not working

Lightning Web Component mock data Test is not working with registerLdsTestWireAdapter.

After emiting data is not reflecting on DOM.
Manjunath MManjunath M
//wireGetRecordDynamicContact.html

<template>
    <lightning-card title="My Contact Record" icon-name="standard:contact">
        <template if:true={contact}>
            <div class="slds-m-around_medium">
                <p>{name}</p>
                <p>{title}</p>
                <p><lightning-formatted-phone value={phone}></lightning-formatted-phone></p>
                <p><lightning-formatted-email value={email}></lightning-formatted-email></p>
            </div>
        </template>
    </lightning-card>
</template>

// wireGetRecordDynamicContact.js

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

const FIELDS = [
    'Contact.Name',
    'Contact.Title',
    'Contact.Phone',
    'Contact.Email',
];

export default class WireGetRecordDynamicContact extends LightningElement {
    @api recordId;
    
    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    contact;

    get name() {
        return this.contact.data.fields.Name.value;
    }

    get title() {
        return this.contact.data.fields.Title.value;
    }

    get phone() {
        return this.contact.data.fields.Phone.value;
    }

    get email() {
        return this.contact.data.fields.Email.value;
    }
}

//wireGetRecordDynamicContact.test.js

import { createElement} from 'lwc';
import WireGetRecordDynamicContact from 'c/wireGetRecordDynamicContact';
import { getRecord } from 'lightning/uiRecordApi';
import { registerLdsTestWireAdapter } from '@salesforce/sfdx-lwc-jest';

// Mock realistic data
const mockGetRecord = require('./data/getRecord.json');

// Register as an LDS wire adapter. Some tests verify the provisioned values trigger desired behavior.
const getRecordAdapter = registerLdsTestWireAdapter(getRecord);



describe('c-wire-get-record-dynamic-contact', () => {
    afterEach(() => {
        // The jsdom instance is shared across test cases in a single file so reset the DOM
        while (document.body.firstChild) {
            document.body.removeChild(document.body.firstChild);
        }
    });

    describe('getRecord @wire data', () => {
        it('renders contact details', () => {
            // Create element
            const element = createElement('c-wire-get-record-dynamic-contact', {
                is: WireGetRecordDynamicContact
            });
           
            document.body.appendChild(element);
            getRecordAdapter.emit(mockGetRecord);
            

            // Emit data from @wire
            

            // Return a promise to wait for any asynchronous DOM updates. Jest
            // will automatically wait for the Promise chain to complete before
            // ending the test and fail the test if the promise rejects.
          //  return flushPromises().then(() => {
            return Promise.resolve().then(() => {
                // Select elements for validation
                const nameEl = element.shadowRoot.querySelector('p');
                expect(nameEl.textContent).toBe(
                    mockGetRecord.fields.Name.value
                );

                const phoneEl = element.shadowRoot.querySelector(
                    'lightning-formatted-phone'
                );
                expect(phoneEl.value).toBe(mockGetRecord.fields.Phone.value);

                const emailEl = element.shadowRoot.querySelector(
                    'lightning-formatted-email'
                );
                expect(emailEl.value).toBe(mockGetRecord.fields.Email.value);
            });
        });
    });
});


Mock Data:
{
    "apiName": "Contact",
    "childRelationships": {},
    "eTag": "846fdcd21a53214d447e578adbe263c6",
    "fields": {
        "Email": {
            "displayValue": null,
            "value": "amy@demo.net"
        },
        "Name": {
            "displayValue": null,
            "value": "Amy Taylor"
        },
        "Phone": {
            "displayValue": null,
            "value": "4152568563"
        },
        "Title": {
            "displayValue": null,
            "value": "VP of Engineering"
        }
    },
    "id": "0031700000pHcf8AAC",
    "lastModifiedById": "00517000002fESIAA2",
    "lastModifiedDate": "2019-02-15T10:33:07.000Z",
    "recordTypeInfo": null,
    "systemModstamp": "2019-02-15T10:33:07.000Z"
}

This is the lightning web component with test
Manjunath MManjunath M

I'm getting below error

 TypeError: Cannot read property 'textContent' of null

     / Select elements for validation
     const nameEl = document.body.querySelector('p');
    expect(nameEl.textContent).toBe(
         |                 ^
    mockGetRecord.fields.Name.value);

Pablo Ledesma 8Pablo Ledesma 8
Hello Manjunath M

I have the same problem making the exercises for trailhead chapter Lightning Web Components Tests > Write a Jest Test for Wire Service.
Please, if you find a solution let me know.