salesforce / sfdx-lwc-jest

Run Jest against LWC components in SFDX workspace environment
MIT License
165 stars 82 forks source link

lightning/confirm is not support #277

Closed vzhut closed 2 years ago

vzhut commented 2 years ago

Description

I have a component where connected lightning confirm component(https://developer.salesforce.com/docs/component-library/bundle/lightning-confirm/documentation). When I run a test for my component, I am getting error that Cannot find module 'lightning/confirm'. But it is feature is part of salesforce library and there should not be any problem with it.

Steps to Reproduce

  1. Create lwc component and connect lightning confirm component.
  2. Create test and run it

My component:

import { LightningElement, api, track } from "lwc";
import LightningConfirm from "lightning/confirm";
import { labels } from "./labels";
export default class testComponent extends LightningElement {
.....
async handlerPicklistAttributesChange(event) {
        const result = await LightningConfirm.open({
            message: this.labels
                .confirmMessageThatRecordTypeMappingWillBeLost,
            variant: "header",
            label: this.labels.confirm,
        });
        if (result) {
            this.currentCodeList = [];
            this.existedRecordTypeMappingMap[this.selectedCountry] = [];
            this.attributesPicklistAction(event);
        } else {
            this.utils.setCheckAttrubute(
                attribute.value,
                !attribute.checked
            );
        }
}

Test as generated by default

import { createElement } from 'lwc';
import testComponent from 'c/...';
import LightningConfirm from 'lightning/confirm';

describe('testComponent', () => {
    jest.mock('lightning/confirm');
    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);
        }
    });

    it('TODO: test case generated by CLI command, please fill in test logic', () => {
        LightningConfirm.open = jest.fn().mockResolvedValue(true);
        // Arrange
        const element = createElement('testComponent', {
            is: testComponent
        });

        // Act
        document.body.appendChild(element);

        // Assert
        // const div = element.shadowRoot.querySelector('div');
        expect(1).toBe(1);
    });
});

Error in test: Cannot find module 'lightning/confirm' from 'src/lwc/testComponent/testComponent.js'

Version

p3v9d5ui commented 2 years ago

Any solution to this? This is blocking me as well.

ekashida commented 2 years ago

Thanks for reporting this--fixed in @salesforce/sfdx-lwc-jest@1.1.2