Skip to content

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

Description

@dcarnicer

Bug Report

Describe the bug
On a fresh clone, npm test (sfdx-lwc-jest) fails 5 of 7 test suites. There are two independent root causes.

To Reproduce

  1. Clone the repo.
  2. Run npm install.
  3. Run npm test.

Expected: all suites pass. Actual: 5 suites fail to run.

Root cause 1: c/ imports do not resolve (4 suites)*
The LWC components live under PackageComponents/lwc/, but sfdx-project.json declares only force-app as a packageDirectory. The sfdx-lwc-jest resolver derives module paths from packageDirectories (using the <path>/**/lwc glob), so an import from the c/ namespace cannot be resolved for components under PackageComponents. Affected suites: sampleEditAccountQuickAction, sampleViewAccountQuickAction, subscribeToEvents, lscMobileInline_ParentInfo (each fails with Cannot find module 'c/...'). Suites that import the component via a relative path (e.g. lscMobileInline_CarouselLwc) pass.

Root cause 2: wrong mock in lscMobileInline_SimpleWidget test (1 suite)
The component imports getBarcodeScanner from lightning/mobileCapabilities, but the test mocks lightning/barcodeScanner. With no stub for lightning/mobileCapabilities, the import fails with Cannot find module 'lightning/mobileCapabilities'.

Suggested fix

  • Root cause 1: add PackageComponents to packageDirectories in sfdx-project.json (which also makes the SF CLI aware of the components), or add a Jest moduleNameMapper mapping the c/ namespace to PackageComponents/lwc/<name>/<name>.
  • Root cause 2: mock lightning/mobileCapabilities exposing getBarcodeScanner.

Happy to follow up with a PR that gets the suite to 7/7.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions