1 | # lwc-jest-preset
|
2 |
|
3 | Tools to assist with testing Lightning Web Components (LWC) with Jest. This project provides 2 services: preset Jest configuration for testing LWC components, and stubs for common external libraries used in LWC components.
|
4 |
|
5 | ## Requirements
|
6 |
|
7 | - Node 8.x
|
8 | - NPM 5.x
|
9 | - Yarn >= 1.0.0
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ### Jest Preset Configuration
|
14 |
|
15 | 1. Install `lwc-jest-transformer`. This package runs LWC components through the LWC compiler.
|
16 |
|
17 | `yarn add -D lwc-jest-transformer`
|
18 |
|
19 | 1. Install `@lwc/jest-resolver`. This package resolves all supported `lwc-*` imports.
|
20 |
|
21 | `yarn add -D lwc-jest-resolver`
|
22 |
|
23 | 1. Use this project's preset config. This maps to the settings in `jest-preset.json`. Any settings added to your project's own `jest` config will take precedence to entries in the preset.
|
24 |
|
25 | ```json
|
26 | {
|
27 | "jest": {
|
28 | "preset": "lwc-jest-preset"
|
29 | }
|
30 | }
|
31 | ```
|
32 |
|
33 | 1. 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 the `example` and `other` namespaces:
|
34 |
|
35 | ```json
|
36 | {
|
37 | "moduleNameMapper": {
|
38 | "^(example|other)-(.+)$": "<rootDir>/src/test/modules/$1/$2/$2"
|
39 | }
|
40 | }
|
41 | ```
|
42 |
|
43 | 1. Create a `__tests__` inside the bundle of the LWC component under test.
|
44 | 1. Create a new test file in `__tests__` that follows the naming convention `<js-file-under-test>.test.js`. See an example in this projects `src/test` directory.
|
45 | 1. Write and run the Jest tests!
|