Lightning · Lightning Testing Service

Lightning Testing Service

As the size of complexity of Lightning Apps grew bigger, we could try to maintain the code quality of our Lightning Component like the same way we maintain in Apex. So the first thing we must start to look at was, what Salesforce provides out of the box for us to gauge the quality, the sad thing is we have “Apex testing frameworks” for testing code coverage of Apex, but there is nothing for lightning right now. So obviously the next thing for us to look at was what tools available outside Salesforce, that developers are already using right now.

Luckily there are lot of tools already available with other Client testing frameworks. Couple of libraries like Jasmine, Mocha are available but the problem is these libraries don’t exactly work out of Lightning Component because lightning works on Aura framework. 

What is Lightning Testing Service?

Lightning Testing Service(LTS) is “Managed Package” developed and released by Salesforce which provides wrapper for us to integrated existing client frameworks to be used with lightning component.

What is Wrapper?

LTS doesn’t aim to reinvent the wheel and provide us with a whole new way of testing Javascript. All LTS aims to do is to provide the resource for us to be able to use what developers are already using as  “Industry standards” that salesforce provides.

Some Key features: 

  • LTS is supported with Jasmine and Mocha which are the two most popular client side testing frameworks today.
  • LTS is able to be used with Salesforce DX and as well as in non Salesforce DX context.

Now Lets see End – to – End flow of how we can use Lightning Component Service:

  1. Create your Lightning Component.
  2. Install LTS.
  3. Write a Javascript test suite.
  4. Create a Test Runner app.
  5. Run your Tests.

Create your Lightning Component

  1. In the Developer Console, click File > New > Lightning Component. Specify QuickContacts as the bundle name and click Submit
  2. Implement the component as follows:

<aura:component implements=”force:appHostable”>

<p>ContactList goes here</p>

</aura:component>

3. The component implements the force:appHostable interface to indicate that it can run in the Salesforce1 applicationLightning components can include other Lightning components and regular HTML marku

4. Click File > Save to save the file

Install Lightning Testing Service

Its time to Install LTS into our Org. LTS can be used with Salesforce DX context and as well as non Salesforce DX context. Testing Lightning component with LTS and testing apex is quite similar.There are two ways you can install the LTS. The simplest is to use the Salesforce DX CLI. If you’re not using Salesforce DX, you can manually install the unmanaged package.

Write your tests using a JavaScript testing framework of your choice. We provide easy-to-use wrappers for Jasmine and Mocha.

A simple Jasmine test spec looks like the following:

/*** This is a ‘hello world’ Jasmine test spec*/

describe(“A simple passing test”, function() {

    it(“checks that true is always true”, function() {

        expect(true).toBe(true);

    });

});

A similar Mocha test looks, well, similar:

/*** This is a ‘hello world’ Mocha test */

var assert = require(‘assert’);

describe(‘Test the nature of truth’, function() {

    describe(‘A simple passing test’, function() {

        it(‘checks that true is always true’, function() {

            assert.equal(true, true);

        });

    });

});

You can write your own wrapper if you prefer a different testing framework. It’s not hard, but plan on half a day to a day of work.

The LTS also provides utilities specific to the Lightning Component framework, which let you test behavior specific to Aura components. Your test suite is deployed in the form of an archive (zip) static resource. Once the LTS is installed and configured, you make changes to your test suite, create the archive, and upload it to your org. Once uploaded you can run the test suite via the command line or via URL.

  1. If your using LTS with Salesforce DX

Salesforce DX includes a one line command for automatically installing the LTS unmanaged package. Once installed, you can work with your test suite in a variety of ways from the command line. This approach also facilitates systematic automated testing.The Salesforce DX CLI also allows you to use the sfdx command line tool to perform automated testing as part of your development process, including automated process, such as continuous integration.

  • For installing Salesforce DX CLI follow the instructions given below for your operating system :
  • Install the Salesforce DX CLI in the Salesforce DX Setup Guide
  • Verify that the Salesforce DX CLI plug-in is installed and updated by running the following command in your shell or terminal application:

→ sfdx update

Additional verification details are available in Verify Your Installation and Install the Plug-In in the Salesforce DX Setup Guide.

  • Install the LTS package with the following command:

          → sfdx force:lightning:test:install

This installs the latest version of the LTS package into your default SFDX org. See the        help for the install command for additional options.

After you install LTS with Salesforce DX, you can run your tests from the command line using the sfdx tool. For example:

→ sfdx force:auth:web:login -s     # connect to your scratch org

→ sfdx force:source:push           # push local source to the scratch org

→ sfdx force:lightning:test:run -a jasmineTests.app   # run the test suite

When you run the 

→ force:lightning:test:run -a jasmineTests.app 

command in a connected workspace you should see something like the following:

This tells you that the command line tools are working, and connected to your development org.

      2. If your not using Salesforce DX

If you’re not using Salesforce DX, you’re missing out a lot, but you can still use LTS.      Installing the LTS package is just like installing any other unmanaged package.

  • Log in to your org. We recommend that you create a new DE org for evaluating the LTS.
  • Go to the project Releases page, and click the package installation URL for the latest release.
  • Authenticate again with the credentials for your DE org.
  • Follow the normal package installation prompts. We recommend installing the package for admins only.

          However you install it, the LTS package includes the following items:

Example test suites

  • Jasmine JavaScript files in archive static resources
  • Mocha JavaScript files in archive static resources
  • Example components to be tested
    • Components, an Apex class, and a custom label
  • LTS infrastructure
    • Jasmine framework and wrapper
    • Mocha framework and wrapper
    • LTS test utilities
    • Test runner component
    • Wrapper test app

Once installed, you can run the example test suite by going to the following URL in your org:

→  https://<myServer>/c/jasmineTests.app (Jasmine) 

→ https://<myServer>/c/mochaTests.app (Mocha)

This page tells you that the package is correctly installed and LTS is working in your org.

Write a Javascript test suite
Components for Testing

The components provided in the package can be accessed as you would any Aura component.

  • In the Developer Console
  • In the Force.com IDE, in the /aura/ directory
  • By converting your org’s metadata into a format that you can work with locally, using Salesforce DX

You can also explore the components directly from the repo. They’re available in the lightning-component-tests/main/default/aura directory.

There are more than a dozen different components, designed to be used in illustrative tests. Each of the components has a description of its behavior in its “.auradoc” documentation file.

Test Suites

The example tests are included in the form of static resources. There are four test suites included in the LTS package, three for Jasmine and one for Mocha:

  • jasmineHelloWorldTests.resource — A very simple Jasmine example including one passing and one failing test.
  • jasmineExampleTests.resource — The main Jasmine example test suite.
  • jasmineLightningDataServiceTests.resource — Some Jasmine examples specific to testing Lightning Data Service-based components.
  • mochaExampleTests.resource — The Mocha example test suite, which provides examples parallel to the main Jasmine test suite.

The remainder of the static resources are infrastructure used by the LTS. They’re briefly described in Use Another JavaScript Test Framework.The jasmineExampleTests.resource and mochaExampleTests.resource files are each a single JavaScript file containing a complete test suite. It’s a single file for convenience in delivery and exploration. Your own test suites can include many such files. The test suites are copiously commented. The code and comments serve as the official documentation for how to write tests.

Run Your Tests

And the final step is to run our tests, 

  1. If your not using DX, we just have to push that to whatever source we want and navigate to actual Aura application and it will run all our tests in a nice UI.
  2. If your using DX its even simpler because we can utilize our CLI command

→ sfdx fore:lightning:test:run

In order to get all the coverage.

Benefits:

  • It increase the code quality which helps to build Customer Trust.
  • It helps in catching problems at the very early stage of development which in turn helps in saving time and money.

Points to Remember:

  • Don’t run tests in your production org. The LTS doesn’t provide an isolated test context or transaction wrapper. DML 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 scratch orgs, using data provided by the test suite itself.
  • Second thing is LTS does not provide any kind of Code Coverage and Matrics.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s