Tools to assist with testing Lightning Web Components (LWC) with Jest. This project provides 2 services: preset Jest configuration for testing Lightning web components, and stubs for common external libraries used in Lightning web components.
- Node 8.x
- NPM 5.x
- Yarn >= 1.0.0
-
Install
@lwc/jest-transformer
. This package runs LWC components through the LWC compiler.yarn add -D @lwc/jest-transformer
-
Install
@lwc/jest-resolver
. This package resolves all supportedlwc-*
imports.yarn add -D @lwc/jest-resolver
-
Use this project's preset config. This maps to the settings in
jest-preset.json
. Any settings added to your project's ownjest
config will take precedence to entries in the preset.{ "jest": { "preset": "@lwc/jest-preset" } }
-
Update the
moduleNameMapper
entry in your Jest config to point to where your LWC components live. For example, use the following to map all components in theexample
andother
namespaces:{ "moduleNameMapper": { "^(example|other)/(.+)$": "<rootDir>/src/test/modules/$1/$2/$2" } }
-
Create a
__tests__
inside the bundle of the LWC component under test. -
Create a new test file in
__tests__
that follows the naming convention<js-file-under-test>.test.js
. See an example in this projectssrc/test
directory. -
Write and run the Jest tests!