Salesforce Decode
Salesforcedecode
Back to questions
LWCIntermediatejestsfdx-lwc-jestlabels

Troubleshoot LWC Jest tests failing on @salesforce/label imports

Real World Scenario

CI pipeline fails on LWC unit tests with "Cannot find module @salesforce/label/c.My_Label" while local IDE tests pass intermittently.

Expected Answer

• Ensure jest config maps label imports to jest-mocks or manual mocks • Add __mocks__ for each label module or generic label mock in sfdx-lwc-jest config • Verify jest config path in package.json matches repo structure for monorepos • Pin @salesforce/sfdx-lwc-jest version aligned with project API version • Run jest with --clearCache in CI before test suite • Avoid dynamic label imports breaking static analysis • Document label mock pattern in contributor README

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Direct answer: Ensure jest config maps label imports to jest-mocks or manual mocks Also consider: Add __mocks__ for each label module or generic label mock in sfdx-lwc-jest config In practice: Verify jest config path in package.json matches repo structure for monorepos Balance speed of delivery with maintainability.

Architect Perspective

Label mocks are infrastructure — set up once in shared jest setup file, not per component.