salesforce / sfdx-lwc-jest

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

sfdx-lwc-jest v1.2.1 is using an outdated version of @lwc/engine-dom #298

Closed BatemanVO closed 1 year ago

BatemanVO commented 1 year ago

Description

When installing @salesforce/sfdx-lwc-jest to the latest version (1.2.1), running any test causes the following error message to print to the console:

console.error
    [LWC error]: LWC WARNING: current engine is v2.20.6, but template was compiled with v2.23.2.
    Please update your compiled code or LWC engine so that the versions match.
    No further warnings will appear.

      at log (node_modules/@salesforce/sfdx-lwc-jest/node_modules/@lwc/engine-dom/dist/engine-dom.cjs.js:768:24)
      at Object.<anonymous> (node_modules/@salesforce/sfdx-lwc-jest/src/lightning-stubs/spinner/spinner.html.compiled:13:32)

Steps to Reproduce

rm -rf node_modules npm i -D @salesforce/sfdx-lwc-jest Run any LWC test

Expected Results

No error should be printed to the console

Actual Results

A version mismatch is printed to the console

Version

@salesforce/sfdx-lwc-jest: 1.2.1

Possible Solution The issue appears to be that when @salesforce/sfdx-lwc-jest is installed, a dependency is installed of @lwc/engine-dom that ultimately has a file in the dist/engine-dom.cjs.js path with line 306 reading const LWC_VERSION = "2.23.2". However, the path @salesforce/sfdx-lwc-jest/node_modules/@lwc/engine-dom/dist/engine-dom.cjs.js has the same line read const LWC_VERSION = "2.20.6".

The reason is because the package.json file for @salesforce/sfdx-lwc-jest specifies @lwc/engine-dom to be version 2.20.6 exactly.

BatemanVO commented 1 year ago

In addition to the above warning, a number of tests also fail when using Apex wire adapters because connector.connect is not a function and connector.disconnect is not a function errors are thrown when performing document.body.appendChild and document.body.removeChild on elements that have had wire methods executed.

Adding @lwc/engine-dom: "2.20.6" to our repo's package.json file appears to resolve the test failures and causes the @salesforce/sfdx-lwc-jest/node_modules/@lwc/engine-dom directory to not be installed, though we are still seeing the warning of version mismatches.

BatemanVO commented 1 year ago

While investigating why the warning was still showing after explicitly installing @lwc/engine-dom: "2.20.6", it may have actually been an issue with package-lock.json specifying version mismatches.

Deleting package-lock.json and the node_modules directory, then reinstalling @salesforce/sfdx-lwc-jest fresh appears to have resolved the issues.