What is LTS?
Lightning Testing Service or LTS is an unmanaged package developed and released by Salesforce that provides a wrapper for us to integrate client frameworks to be used with lightning components.
Or we can just say that LTS is a set of tools and services that let you create test suites for your lightning components.
The LTS supports testing with standard JavaScript test frameworks and contains easy-to-use wrappers for Jasmine and Mocha but if you prefer to use an alternative test framework, you can wrap it yourself.
Why LTS?
Automated tests are the best way to assess the quality of your custom code, and verify that your components work as expected after you write them and continue to work when some changes are made or when something they depend on (the framework, another component, etc) gets updated.
As we know, tests for our Apex classes are really important if we want to deploy our code to the production environment, but for now, there are no Salesforce guidelines to test your client-side lightning components, but since we started to keep most of the data at client side as javascript objects this can be implemented in the future.
Key Features?
- As we already said, LTS is supported by Jasmine and Mocha which are two of the most popular testing frameworks today
- LTS is able to be used in DX context and non-DX context (we highly recommend that you use it with DX)
One more thing to add that is no really a feature but it is nice to mention, testing Lightning Components with LTS is similar to testing Apex.
If you are familiar with testing Apex, after going through LTS documentation and examples you will for sure be able to test your Lightning Components with LTS.
Best Use Cases?
- When we push apex code from sandbox to prod environment, Salesforce will run all of our tests and code will be deployed only if all tests passed and we have enough code coverage.
We can apply the same concept with LTS, but if you want the full functionality of LTS, you should really try to use it in combination with Salesforce DX (Salesforce Developer Experience).
With Salesforce DX you can run your tests with just one command, and we can use that command with some continuous integration tool (Jenkins, Bamboo, etc), for example, we can run all our test’s whenever we want to make deploy or push which will help us to catch and solve problems early. - Where LTS is also really useful is the packaging. For example, you have managed a package that’s on App Exchange and that heavily relays on javascript, if you provide tests for your javascript code you will show that you really care about the quality of your code and product which will for sure have a big positive impact on your potential clients.
How to Install LTS?
There are two ways to install LTS.
One way is with the use of Salesforce DX CLI (LTS works really well in combination with Salesforce DX), but if you are not using Salesforce DX, you can install the LTS package manually just like any other unmanaged package and run your tests in the browser window.
Install LTS with Salesforce DX:
- Install Salesforce CLI and verify the installation
- Verify that the Salesforce DX CLI plug-in is updated
Command:
sfdx update - Install the LTS package with the following command:
Command:
sfdx force:lightning:test:install
After installing LTS, you can run tests with the following command (for Jasmine):
sfdx force:lightning:test:run -a jasmineTest.app
Install LTS unmanaged package manually:
- Log in to your org
- Go to LTS github and click the package installation URL for the latest release
- Authenticate again with the credentials
- Follow the normal package installation prompts. We recommend installing the package for admins only.
- You can also run the test suite from the browser by accessing following URL (for Jasmine):
Base URL is your org’s base URL
https://BASE_URL/c/jasmineTest.app
Limitations
In Apex test class if we have some DML statement(insert Contact, delete Case, etc), all these changes will be rolled back after the test is completed.
But if we have some action in our LTS suite and our controller does some DML statement, these statements will not be rolled back at the end of the test, so do not install LTS in Production!
The workaround is to run LTS tests just in your scratch org’s or if you want to create more advanced use cases, you can mock controller actions, which means you won’t do anything on the server-side.
One more thing to be aware of is that LTS does not provide any kind of code coverage metric.
Conclusion
As a conclusion to this post about Lightning Testing Service, we have to say that by starting to use LTS your code quality and stability will greatly improve, which will not only going to have a big impact on your personal development as Salesforce developer but will also have a big positive impact on your clients and your company’s business.
We hope we have been able to introduce you to Lightning Testing Service in Salesforce and that soon, your lightning components will have their test suite.
If you would like to learn more about LTS or share your ideas feel free to contact us.
We would love to hear your thoughts on this topic.
Useful links:
Introduction to LTS
LTS on Github
Jasmine
Mocha
Salesforce DX