Skip to main content

Testing Lightning Web Components using Jest

About Us
Published by JET BI
02 December 2024
21

Testing LWC with Jest

OVERVIEW

Jest tests are written, saved, and run differently than Jasmine or Mocha tests used for the Lightning Testing Service (LTS). Jest tests are local and run independently of Salesforce. Here are some best practices for Developers.

WHAT IS JEST TESTING FOR LIGHTNING WEB COMPONENTS?

Jest is a JavaScript testing framework. It helps in creating, structuring, and running tests for LWC. You can use Jest to write unit tests for Lightning Web Components. Jest also collects code coverage information and supports mocking, which helps isolate tests from complex dependencies.

SETTING UP THE JEST TEST FRAMEWORK

Node.js is JavaScript that runs on the Chrome browser engine. Npm is a package manager used for distributing reusable code modules.

  • Install Node.js and npm: Download from Node.js (the long-term support version).
  • Install the sfdx-lwc-jest Node module.

In the VS Code terminal, run the following command in the top-level directory of your Salesforce DX Project:

sfdx force:lightning:lwc:test:setup

Tests are written in local JavaScript files. Commit them to version control with the Lightning Web Component. Jest tests are local, saved, and run independently of Salesforce.

Salesforce CLI COMMAND

Use this command to create a test directory and a boilerplate test file:

sfdx force:lightning:lwc:test:create	


Component Folder Structure

A folder named __tests__ is created at the top level of the component’s bundle directory: force-app/main/default/lwc/<Lightning Web Component Name>/__tests__/ 

All tests are saved in this folder.
 

TEST FILE NAMING CONVENTIONS

Jest runs JavaScript files in the __tests__ directory. Test files should end with .test.js.


EXAMPLE COMPONENT FILES



JEST TESTS FOR LIGHTNING WEB COMPONENTS

helloWorldJestTest.test.js

 

UNDERSTANDING THE JEST TEST FRAMEWORK

All tests should follow this structure:

  • Imports: Import the createElement method to create the component under test.
  • Describe() block: Defines the test suite.
  • Cleanup: The afterEach() method resets the DOM after each test. This ensures one test’s output doesn’t affect another.
  • it() block: Describes a single test.
  • Asserts: Use assertions to check expected outcomes.
     

Conclusion

Jest offers a powerful and efficient framework for testing Lightning Web Components. By enabling developers to write unit tests locally, Jest enhances the reliability and maintainability of LWC applications. Understanding the setup process, folder structure, and test conventions is essential for effective testing. With Jest, developers can ensure their components function correctly and meet quality standards, ultimately leading to better performance and user experience in Salesforce applications.


Yuriy Rudenko
Salesforce developer
image
Expertise
Question to the expert
image

We have available resources to start working on your project within 5 business days

1 UX Designer

image

1 Admin

image

2 QA engineers

image

1 Consultant

image
Related Articles
All articles
image
How to Customize Salesforce Field Service Mobile for Your Technicians
Salesforce Field Service Mobile provides a standard technician experience that companies can extend to fit their own service processes. This article looks at areas where a custom mobile workflow can help: AI repair assistance, field documentation, external tools, back-office integrations, quotes and payments on site, barcode scanning and role-based mobile layouts. It also outlines four practical steps for planning a mobile workflow around the technician’s work.
21 October 2026
image
Salesforce Field Service Mobile: New Capabilities for Technicians
Salesforce Field Service Mobile continues to evolve with new capabilities for technicians working in the field. Explore how updates to job preparation, mobile forms, voice input, mapping, and Work Order views are changing the technician experience, and learn why configuring these capabilities around your service operations matters.
14 October 2026
image
Closing the Gap Between Field Service and Business Needs
Standard Salesforce Field Service configuration covers many service processes, but individual businesses can have different workflows, working conditions, scheduling rules, and operational requirements. This article looks at three areas where standard configuration can fall short: how technicians work, the conditions of field service operations, and business-specific processes. It also provides a practical checklist of signs that a Salesforce Field Service setup could benefit from customization.
07 October 2026