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
Is Salesforce Winning the Public Sector Race?
An analysis of Salesforce's rapid expansion into the U.S. public sector, tracing its path from cautious early government licensing deals in the 2010s through the launch of Government Cloud in 2012, its pivotal role in COVID-19 vaccine rollouts, and its 2025–2026 push into military and intelligence work via Agentforce and Missionforce. The piece covers major 2026 contracts — including a $5.6 billion Army deal, a $1.6 billion VA agreement, and Pentagon Impact Level 5 authorization — alongside real-world case studies like California's REAL ID processing and the UK's NHS back-office operations. It also examines the structural obstacles still facing Salesforce and other vendors in government tech: legacy IT systems decades old, outdated federal procurement rules, budget constraints, and organizational caution around AI adoption, plus the competitive pressure from Palantir, Microsoft, and Oracle in the race for public sector AI spending.
28 August 2026
image
Why Your Salesforce Flows Are Agentforce's Biggest Problem
This article argues that the most underestimated risk in Agentforce deployments isn't data quality — it's the automation layer: years of overlapping Flows, Process Builder processes, Apex triggers, and managed package logic that no one has reviewed end-to-end. It explains why AI agents inherit automation complexity without the tribal knowledge human admins carry, why technical debt only becomes visible after an agent hits it in production, and why a clean demo is no indicator of production readiness. The article closes with a concrete, tool-by-tool inventory approach using Flow Trigger Explorer, Salesforce Optimizer, Setup Audit Trail, Apex Debug Logs, Agent Builder, and Health Check — scoped to the specific processes the agent will actually use rather than the whole org.
23 July 2026
image
How to Wire Multiple Salesforce Projects in One Org Without Breaking Everything
This article maps the real integration patterns that emerge when multiple Salesforce projects — both managed packages and unpackaged code — share a single org. It covers four concrete patterns: attaching custom triggers to package-owned objects, calling global members exposed by managed packages, writing directly into another project's objects, and runtime-guarded reads of package data. It then addresses access control for authenticated and guest users, including the Master-Detail wall and the without sharing elevation pattern. The piece closes with eight concrete risks (compile-time dependencies that block uninstall, upgrade coupling, silent cascade failures, access invisible to admins) and six actionable recommendations for keeping cross-project coupling manageable.
08 July 2026