Write Tests for Your Lightning Components
After completing this unit, you’ll be able to:
- Explain the role of the Lightning Test Service, its helpers, and execution methods.
- Write unit tests for Lightning components.
Lightning components are the building blocks of modern Salesforce applications. You build them with the Lightning Component framework, a UI framework for developing dynamic web apps for mobile and desktop devices. This is a modern framework for building single-page applications engineered for growth.
In this step, we discuss the Jasmine testing framework. The package you installed in unit 1 of this module includes some example Jasmine tests, as well as the Lightning Testing Service.
Jasmine is a behavior-driven development testing framework. With this framework, you don’t make assertions about what a calculation is or which exception is thrown. Instead, Jasmine has you make expectations about which behavior occurs. So, instead of asserting that the output of method A should be XYZ, we tell Jasmine to expect method A to be called once, and its return value should be XYZ.
To bridge the Lightning Component framework and Jasmine (or Mocha), the platform provides the Lightning Test Service. The Lightning Testing Service provides a utility object $T, which lets you create components and manipulate data. This facilitates testing.
First, open the Jasmine example tests.
- Click and select Developer Console.
- Select File > Open Resource.
- Select JasmineExampleTests.js and have a look at the code.
Jasmine organizes its tests into suites using the describe keyword. Each individual test starts with the IT function call.
In the Developer Console’s JasmineExampleTest.js tab, scroll down to the test suite starting on line 69. This test demonstrates invoking a client-side action and inspecting its behavior. One of the first things that happens in the test is a call to $T,
the test helper object. In this case, you’re using $T to create an instance of the component c:egClientSideAction. $T is promise enabled, meaning the real work of your test is contained in the
.then(component) method starting on line 72.
You make expectations against the component object, and you can interact with it to set attribute values. Specifically, you expect the component to have markup with the aura:id of “accountList” and that it initially has 0 items in the array. Because the
$T object creates an instance of the egClientSideAction component, which has a default Aura:Id and an empty array, the expectation is true. After executing the
component.searchAccounts() method, however, you expect there to be 10 elements
in the account list, the first of which is named Salesforce.
When you write tests for your Lightning components, you need a way to execute them. The Lightning Test Service has two methods to execute your tests. Once you have the tests written, you can run them by visiting: http://<your trailhead playground instance>.lightning.force.com/c/jasminetests.app. This page loads and runs your Jasmine tests, writing the pass/fail information to the screen.
If you’d like to execute your Lightning Test Service–based tests from the command line, you’re in luck. If you install the Lightning Test Service from the DX command line, you can execute your tests directly from the command line. For more information on how that works, see the Lightning Test Service readme on github. See the link in the Resources section.