With the increasing complexities of the Lightning Applications, it is highly important to maintain and improve the quality of our Lightning component code in the same manner as we do in Apex. So, the first thing we must consider is what Salesforce offers to assess quality. This is where the Lightning Testing Service comes into the picture.
What is a Lightning Test Service (LTS)?
Lightning Testing Service is an amazing testing framework to test your Salesforce Lightning components or Lightning Web Component (LWC). It is highly recommended to utilize the Lightning Testing service to ensure that your components are functioning as expected. LTS is a complete suite of tools and services that are integrated with Salesforce DX to make testing easier and faster.
Key Features of LTS
- LTS is supported by Mocha and Jasmine that are currently the two innovative and famous client testing frameworks.
- LTS can be utilized with Salesforce DX and with non-Salesforce DX contexts.
How Lightning Testing Service Works?
LTS simplifies the process of building test suites and is fully integrated with Salesforce DX.
LTS comprises of two main components:
- Unmanaged LTS packages that are listed on the project Releases page by the release version.
- LTS installation command for the Salesforce Command Line Interface (CLI).
In the case of complex development processes, use the LTS command that is included with the Salesforce DX CLI plugin. With Salesforce CLI, you can integrate LTS into automated testing, continuous integration, and other source-based development processes.
The command-line tool automates the execution of your test suites. This opens the same URL that you can open manually and monitors the test suites using WebDriver. The results are then analyzed and packaged for use in the command-line development process.
Installing LTS is a one-line command in Salesforce DX CLI. After installation, you can use the command line to work with the testing suite in several ways.
Things to consider when working with Lightning Test Service
Testing Lightning Web Components and Aura methods are not sufficient to make sure that your LWCs are functioning and operating properly.
- Wrappers are just offered for Jasmine and Mocha. To use another testing framework, you may need to develop your own wrapper that will most probably take a day to work.
- Do not install or utilize Lightning Test Service in production. Yes, you read that absolutely right! This can be only for the development conditions and more suitable for scratch orgs that you can simply create using Salesforce DX.
- Data Manipulation Language operations you perform in your tests won’t be rolled back at the end of the test. We recommend that you run your LTS test suites only in development environments or scratch orgs that you simply create using Salesforce DX and data provided by the test suite itself.
- Lightning Testing Service involves sample code to help you get begun. This involves not only the Jasmine and Mocha wrappers but also a Salesforce Lightning Application called jasmineTests.app which is an app that you can simply use to begin your testing. You can run this command to check the results:
force:lightning:test:run -a jasmineTests.app
Important tips: Don’t use the Lightning Testing Service to check your server code. You need to perform client-side and server-side testing separately. When executing Apex server methods that insert records with LTS, the information that is created will not be rolled back. You have to be solely responsible for rolling back the information yourself. This is recommended as the best method for you to check the server code in your Salesforce lightning components. You can still craft unit tests to check your Aura methods operate as expected.
To know more about what LTS contains and how you can employ it, we counsel you to refer to this Getting Started With Lightning Testing Service post. This post will definitely help you get started with building your first test suite.
Lightning Testing Service is a suite of tools used to increase code quality and efficiency by applying best coding practices. It also helps in catching problems at the very early stage of development that, in turn, helps in saving time and money. Using this tool will be very important in ensuring that your components work as expected.