Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 1.5 KB

README.md

File metadata and controls

45 lines (31 loc) · 1.5 KB

@lwc/jest-preset

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.

Requirements

  • Node 8.x
  • NPM 5.x
  • Yarn >= 1.0.0

Usage

Jest Preset Configuration

  1. Install @lwc/jest-transformer. This package runs LWC components through the LWC compiler.

    yarn add -D @lwc/jest-transformer

  2. Install @lwc/jest-resolver. This package resolves all supported lwc-* imports.

    yarn add -D @lwc/jest-resolver

  3. 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.

    {
        "jest": {
            "preset": "@lwc/jest-preset"
        }
    }
  4. 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:

    {
        "moduleNameMapper": {
            "^(example|other)/(.+)$": "<rootDir>/src/test/modules/$1/$2/$2"
        }
    }
  5. Create a __tests__ inside the bundle of the LWC component under test.

  6. 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.

  7. Write and run the Jest tests!