Skip to main content

Code reviews rule: Untested Lightning Web Components

Written by David Martin
Updated this week

Untested Lightning Web Components

Why is this an issue?

Lightning Web Components without test coverage are more likely to contain bugs and regressions. Untested components are harder to refactor safely and may not behave correctly across different scenarios.

This rule identifies LWC controllers (JavaScript files) that do not have corresponding test files that import and test them.

Examples

Example of incorrect configuration:

force-app/main/default/lwc/myComponent/
myComponent.js # No tests import this component
myComponent.html

Example of correct configuration:

force-app/main/default/lwc/myComponent/
myComponent.js
myComponent.html
__tests__/
myComponent.test.js # Imports and tests 'c/myComponent'

The test file must import the component using the c/ prefix:

import MyComponent from 'c/myComponent';

How can I fix violations?

  1. Create a __tests__ directory inside your LWC bundle

  2. Create a test file (e.g., myComponent.test.js)

  3. Import your component using the standard c/componentName import syntax

  4. Write meaningful tests that cover your component's functionality

When should I disable this rule?

You may want to dismiss this issue if:

  • The component is a trivial wrapper component with no logic or state

  • The component is being deprecated and will be removed soon

  • You have integration tests that cover the component's functionality at a higher level

Resources

Did this answer your question?