LWC Testing with Jest Framework

Share on facebook
Share on twitter
Share on linkedin
Jest is a JavaScript testing Framework which focuses on simplicity. It works with Babel, TypeScript, Node, React, Angular, Vue, and more projects. Jest tests are local only and are saved and run independently of Salesforce. Jest tests are fast as they don’t run in a browser or connect to an org.

Jest is a JavaScript testing Framework which focuses on simplicity. It works with Babel, TypeScript, Node, React, Angular, Vue, and more projects. Jest tests are local only and are saved and run independently of Salesforce. Jest tests are fast as they don’t run in a browser or connect to an org.

Set up Jest Test Framework:

To test Lightning Web Components, need to do the following prerequisites to set up the environment.

  1. Open Visual Studio Code Project.
  2. Use below link to install Node.js and npm. When Node.js gets installed, npm also installs.
    • Node.js – LTS (Long-Term Support) version is recommended
  3. Generally, Salesforce DX Project does not have a package.json file. Execute the following command from the terminal of the Visual Studio Code. It will create a package.json file in the project directory. After the execution, system will be asking the project information, keep the defaults and the press Enter key until package.json file has been created.
    • npm init
  4. Execute the following npm commands at the top-level directory of your Salesforce DX project:
    • npm install
  5. Then run below command to install sfdx-lwc-jest and its dependencies into each Salesforce DX project. sfdx-lwc-jest works in Salesforce DX projects only.
    • npm install @salesforce/sfdx-lwc-jest –save-dev
  6. By default, an SFDX project includes these script entries in the scripts block of its package.json file. If the project’s file doesn’t include, add them.
Write a Basic Test:
  1. In Visual Studio Code, right click on the hello directory and select New Folder.
  2. Name the folder as “__tests__” and press Enter key to proceed.
  3. After that right-click on the __tests__ directory and select New File.
  4. Name the file as “hello.test.js” and then press Enter key.
  5. Hello LWC component looks like below,
    • hello.html
    • hello.js
  6. Write the following code into the new test file,

All tests must use below structure,

  1. Imports:
    • First, test the imports with createElement method and is available only in tests. The code should import the component to test, which in this case is c/hello. Using these imports the component under test is created later.
  2. Cleanup:
    • The Jest afterEach() method is used to reset the DOM at the end of the test. Every test file has a single instance of jsdom, and changes aren’t reset between tests inside the file. As a best practice, it is important to clean up between tests, so that one test’s output doesn’t affect another test.
  3. Describe() block:
    • A describe block() determines a test suite. A test suite has one or more tests that belong together from a functional point of view. For hello.test.js, a single describe is sufficient.
  4. it() block:
    • A single it() block describes a single test.
  •  
  1. Asserts:
    • Get the elements from DOM using querySelector() method. Use expect statement which is an assertion of the results. Jest supports lots of matchers like toBe and toMatchObject. Please check all the methods using below link,
    • https://jestjs.io/docs/en/expect

Note:

For more complex components, it makes sense to have several describe blocks that group things into categories like error scenarios, empty input, wired data, regular data, and so on. See the below screenshot for reference.

Run the Test:
  1. In Visual Studio Code, click on Terminal > New Terminal. This opens a terminal in Visual Studio Code. The terminal defaults to the current project top-level directory.
  2. Execute the following command in the terminal,
    • npm run test:unit
  3. The test Passes. (Refer below screenshot)
Run Tests Continuously During Development:

For a single component each time you save changes, to run all tests, change directories to the component directory and run the sfdx-lwc-jest command with the watch parameter.

  • npm run test:unit:watch
Run Tests in Jest Debug Mode:

To run the project’s Jest tests in debug mode, run the sfdx-lwc-jest command with the –debug parameter.

  • npm run test:unit:debug
Conclusion:

Jest test Framework focuses on the Black-Box testing and can be written without application knowledge. However, public methods, templates, and events must be tested first, and most importantly it doesn’t require a 100% code coverage. Write Jest tests to do the following,

  1. Test a component’s public API (@api properties and methods, events)
  2. Test basic user interaction (clicks)
  3. Verify the DOM output of a component
  4. Verify that events fire when expected
Reference links:

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.testing

https://jestjs.io/

https://trailhead.salesforce.com/en/content/learn/modules/test-lightning-web-components/set-up-jest-testing-framework

https://github.com/salesforce/sfdx-lwc-jest

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.