In previous post, we discussed what is Jasmine and simple hello world program to see how it works. In this blog post, we will discuss how Jasmine can be used in Salesforce to test Lightning Component’s Javascript code.
Note : Everything we are discussing here is completely covered as part of Live coding in Apex Salesforce Saturday organized by Amit. Would like to thank Mohith for his support and answers during the demo. It also shows How SalesforceDX can be used daily by developers to perform development.
Jasmine and Mocha support is provided as a part of Lightning Test Service (LTS) by Salesforce. Jasmine and Mocha, both are well known and industry wide solution accepted in Javascript world. of course, Salesforce don’t want to stay behind in terms of its offering to developers thats why we have new baby in town – Lightning Test Service (LTS).
You can install Lightning Test Service (LTS) in your org as a part of unamanged package from here. You can also install Lightning Test Service in SFDX using command
sfdx force:lightning:test:install
Once Jasmine is installed, you can navigate in your browser to open Jasmine test runner page using URL https://<YourDomain>.lightning.force.com/c/jasmineTests.app
SFDX Command to open Jasmine Test runner in Salesforce
sfdx force:org:open -p /c/jasmineTests.app
Let’s consider below Lightning component to be used by Lightning Test Service (LTS)
This Lightning Component uses lightning:datatable component and populates it on basis of input provided.
Now we need to create a Javascript file containing Jasmine test method and upload it as a static resource.
Once, static resource is uploaded in Salesforce, we need to add reference of this static resource in JasmineTests.app . In our case, static resource name is JasmineTestDatatable. This Test runner application comes as part of Lightning Test Service. Same way, MochaTest runner app also comes as part of LTS.
It is time to open Jasmine Test runner using URL https://<YourDomain>.lightning.force.com/c/jasmineTests.app. We would be able to see below output
Youtube Video of Lightning Test Service (LTS)
Leave a Reply