Setup JEST Testing for Salesforce LWC

 Setup JEST Framework for LWC

Before we install lwc-JEST for LWC, we need to install 2 other softwares to support.
LWC-JEST

  1. Node.js : This page may have two versions of node files. we recommend to use LTS(Long Term Support) version. Download and install it.
  2. npm:When you install Node.js, npm also installs.
Make sure you have Node and NPM installed by running simple commands to see what version of each is installed.
Node: Open command prompt and run node -v It will show verision like this: v12.14.0
NPM: Open command prompt and run npm -v It will show verision like this: 6.13.4

If Node and npm are successfully installed, then open your vs code project and follow the below steps to install lwc-jest.

VS code Setup for JEST LWC

Step:1
Open terminal in vs code by view --> Terminal or shortcut keys for windows (control + ~ )
vs-code-terminal


Step:2
Run npm init command to create package.json file in the project root folder. This command will ask some details you can leave blank and hit enter to finish.
lwc-jest-npm-init


Step:3
Now run following two commands one by one to install node modules.
npm install
npm install @salesforce/sfdx-lwc-jest --save-dev

lwc-jest-npm-install


Step:4
Now open the package.json file created in step no 2 and add below lines in the script block.
"test:unit": "sfdx-lwc-jest",
"test:unit:coverage": "sfdx-lwc-jest --coverage",
"test:unit:watch": "sfdx-lwc-jest --watch",
"test:unit:debug": "sfdx-lwc-jest --debug"

lwc-jest-commands

We can also add more commands from this list sfdx-lwc-jest commands.
Next post we will create simple lwc components with JEST test class.

Source:
hhttps://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.unit_testing_using_jest_installation

Labels:
LWC
Join the conversation