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.
Jest Preset Configuration
@lwc/synthetic-shadow. This is a polyfill for ShadowRoot that was tailor-made for LWC.
yarn add --dev @lwc/synthetic-shadow
Use this project's preset config. This maps to the settings in
jest-preset.json. Any settings added to your project's own
jestconfig will take precedence to entries in the preset.
moduleNameMapperentry in your Jest config to point to where your LWC components live. For example, use the following to map all components in the
__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 projects
Write and run the Jest tests!