Unable to create Jest tests for generation 2 package

lightning-web-componentslwc-jest

I have created an LWC app, and am now trying to add code test coverage. But unfortunately, Jest (@salesforce/[email protected]) is failing in two different ways:

  1. I use the getRecords wire adapter, which I think was added fairly recently (we're using api version 55). But Jest throws this error:
TypeError: Invalid adapterId, it must be extensible.

The other wire adapters, such as getRecord, are fine. Only the inclusion of this adapter causes this problem.

  1. The uiRelatedListApi and LightningConfirm libraries result in errors like this:
Cannot find module 'lightning/uiRelatedListApi' from '<snip>'

No other lightning/* module causes this problem.

Any ideas what is causing this?

One thing I have noticed is that my project (derived from a recipe project), includes a Typescript type declaration file called lds.d.ts, and each of the types above which are causing problems do not exist in this file. The others, which are working fine, do exist.

Best Answer

Even though I can't see your test code and wonder what a TypeScript type declaration file exists for, here are some hints that might help you:

Please check the official LWC Recipes repository for a whole set of LWC Jest unit test examples. It uses @salesforce/[email protected] at the moment. Many of the standard Lightning APIs and dependencies are replaced with jest-mocks here. For example, also the getRecord and getRecords actions from the uiRecordApi you mentioned:

https://github.com/trailheadapps/lwc-recipes/blob/main/force-app/test/jest-mocks/lightning/uiRecordApi.js

For the LightningConfirm you can also find an example on how to use / mock this in your Jest tests in the official LWC Component Reference:

https://developer.salesforce.com/docs/component-library/bundle/lightning-confirm/documentation

I hope this helps you to resolve your test issues.

Related Topic