0

I create LWC component that contains wire service with importing getRelatedListRecords function. and its work normal. but the problem is when I create the test using jest-test I don't know why I always getting this following error error when running npm test I already follow the documentation but always still got those module not found error. and here my lwc-test:

import { createElement } from 'lwc';
import RelatedRecordDetailList from 'c/relatedRecordDetailList';
import { getRelatedListRecords } from 'test/jest-mocks/lightning/uiRelatedListApi';

const mockGetRecord = require("./data/getRelatedListRecords.json");

describe('c-related-record-detail-list', () => {
    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);
        }
    });

    test('display_realted_list_record', () => {
        // Arrange
        const element = createElement('c-related-record-detail-list', {
            is: RelatedRecordDetailList
        });

        // Act
        document.body.appendChild(element);
        getRelatedListRecords.emit(mockGetRecord);
        // Assert
        // const div = element.shadowRoot.querySelector('div');
        return Promise.resolve().then(() => {
            const content = element.shadowRoot.querySelector(".slds-box");
            expect(content.length).toBe(3);
          });
    });
});

Anyone facing something like that? can you help me guys, please?

Thanks in advance

1 Answer 1

2

Your modules are not set to resolve correctly. If they were, your test would be able to import the module correctly.

// This is the wrong way to import the module
// import { getRelatedListRecords } from 'test/jest-mocks/lightning/uiRelatedListApi';
// Instead, import it this way.
import { getRelatedListRecords } from 'lightning/uiRelatedListApi';

To get this to resolve correctly, make sure your project's root directory contains a JSON file called jest.config.js, which should include content similar to the following:

const { jestConfig } = require('@salesforce/sfdx-lwc-jest/config');
module.exports = {
...jestConfig,
moduleNameMapper: {
  // Adjust path name accordingly
  '^lightning/uiRelatedListApi$': '<rootDir>/force-app/test/jest-mocks/lightning/uiRelatedListApi'
 // Any other mappings go here, too
};

Now, this should correspond to a file in `force-app/test/jest-mocks/lightning/uiRelatedListApi.js' that should look similar to the following:

import { createLdsTestWireAdapter } from '@salesforce/wire-service-jest-util';
export const getRelatedListRecords = createLdsTestWireAdapter(jest.fn());

Assuming everything is lined up, your code should now be fully functional.

1
  • Hi @sfdcfox, thank you for your explanation. But I will try it later. Because I already remove the test first for now. Thank you so much
    – naticap
    Feb 2, 2023 at 12:24

You must log in to answer this question.

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