Skip to content

fix: make LWC unit tests pass on a clean checkout (resolve c/* imports, correct mobileCapabilities mock)#39

Open
dcarnicer wants to merge 1 commit into
SalesforceLabs:mainfrom
dcarnicer:fix/jest-unit-tests
Open

fix: make LWC unit tests pass on a clean checkout (resolve c/* imports, correct mobileCapabilities mock)#39
dcarnicer wants to merge 1 commit into
SalesforceLabs:mainfrom
dcarnicer:fix/jest-unit-tests

Conversation

@dcarnicer

Copy link
Copy Markdown

Fixes #38.

What

Gets the LWC unit test suite to 7/7 passing on a clean checkout. Previously 5 of 7 suites failed to run.

Changes

  1. Resolve c/* imports for components under PackageComponents/lwc (jest.config.js). Added a moduleNameMapper for the c/ namespace. These components are not under a force-app packageDirectory, so the default sfdx-lwc-jest resolver could not resolve c/ imports. Alternative: declare PackageComponents in sfdx-project.json; I kept the change Jest-only to avoid altering deploy scope, but happy to switch if maintainers prefer that approach.
  2. Correct the mock in the lscMobileInline_SimpleWidget test. The component imports getBarcodeScanner from lightning/mobileCapabilities, but the test mocked lightning/barcodeScanner. Updated the mock target so the import resolves.

Test plan

npm test results in: Test Suites: 7 passed, 7 total. Tests: 29 passed, 29 total.

@salesforce-cla

Copy link
Copy Markdown

Thanks for the contribution! Unfortunately we can't verify the commit author(s): David Carnicer <d***@o***.com>. One possible solution is to add that email to your GitHub account. Alternatively you can change your commits to another email and force push the change. After getting your commits associated with your GitHub account, sign the Salesforce Inc. Contributor License Agreement and this Pull Request will be revalidated.

Two independent issues caused 5 of 7 Jest suites to fail:

1. c/* imports for components under PackageComponents/lwc did not
   resolve, because sfdx-project.json only declares force-app as a
   packageDirectory and the sfdx-lwc-jest resolver derives module
   paths from it. Added a moduleNameMapper for the c/ namespace.

2. The lscMobileInline_SimpleWidget test mocked lightning/barcodeScanner,
   but the component imports getBarcodeScanner from
   lightning/mobileCapabilities. Corrected the mock target.

Result: 7 suites / 29 tests pass.

Fixes SalesforceLabs#38
@dcarnicer dcarnicer force-pushed the fix/jest-unit-tests branch from 67acb2a to 2bf2004 Compare June 30, 2026 15:33
@salesforce-cla

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @dcarnicer to sign the Salesforce Inc. Contributor License Agreement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unit tests fail on a clean checkout: c/* LWC imports do not resolve, plus incorrect mock in lscMobileInline_SimpleWidget test

1 participant