12 votes
Accepted

How to test LWC onsuccess event handler in Jest

The comment from @muenzpraeger was correct. The event handler won't fire on its own, so it needs to be fired directly from the test. So the test.js would look something like this: var element = ...
aaron's user avatar
  • 855
11 votes
Accepted

Run Single Test for Lightning Web Component

You can just pass e. g. a file name after your script name E. g: npm run test:unit mycomponent.test.js Would all tests matching a file named mycomponent.test.js
Renji-xD's user avatar
  • 2,791
10 votes
Accepted

Testing LWC with multiple @wire getRecord calls with Jest

This is a known gap in the wire service test utility. It's been around for a while, but we haven't forgotten about it. There's currently some non-trivial changes being made to the internals of how the ...
TrevorBliss's user avatar
  • 3,833
10 votes
Accepted

Automated testing of Lightning Web Components?

Testing for LWC is implemented in Jest. These can be run locally to your org, or even enabled to run in "watch" mode to give you instant feedback while you code. From the docs, LWC testing enables ...
pchittum's user avatar
  • 19.6k
10 votes
Accepted

How can I test that my component, which implements the standard modal components, is working properly?

Figured out the solution to this by following the concepts in the mocking trailhead. Essentially we just need to make mocks for these components and tell jest to use them. 1. Update sfdx-lwc-jest Be ...
TemporaryFix's user avatar
  • 2,937
9 votes
Accepted

In an unit test for an LWC, how can I prove an async chain is setting a value in a lightning-formatted-number?

Figured out a way to flush the promises by applying answer from https://stackoverflow.com/questions/44741102/how-to-make-jest-wait-for-all-asynchronous-code-to-finish-execution-before-expec ...
Brian Kessler's user avatar
9 votes
Accepted

LWC + Jest: Mocking imperative Apex and returning a value

For easier readability, you can store as many .json files as needed for all your mocked data needs and have "less" in your jest test. Within your __tests__ directory, you can create a data ...
Kris Goncalves's user avatar
8 votes
Accepted

How to mock custom LWC components in JEST

Figured it out by myself, turns out to be pretty straight forward: // childComponent.js @api getModifiedRows() { let arr = []; this.modifiedRowRecords.forEach( (value) => { arr.push(value);...
J. Schreiber's user avatar
  • 1,540
7 votes
Accepted

How to unit test a LWC component with @api fields

You would provide the value yourself or else it'll be undefined. test('initialization', () => { const element = createElement('c-unit-test-component', { is: UnitTestComponent ...
Kris Goncalves's user avatar
7 votes
Accepted

How to get code coverage report for LWC unit tests with JEST?

After running the command mentioned in the question and checking the project structure again, I saw that a /coverage folder was automatically created that contains a lcov-report. So similar to what ...
Sebastiano Schwarz's user avatar
6 votes
Accepted

LWC + Jest: Make assertions about an element's rendered style

Stumbled across the answer as I was writing up the question. You can access an element's style properties ( ...drum roll... ) via its style property. return Promise.resolve() .then(() => { ...
Matthew Souther's user avatar
6 votes
Accepted

Getting error when running Jest tests with sfdx project at version 48

You need to update your sfdx-lwc-jest node dependency to a more recent version: Edit package.json Update the version of sfdx-lwc-jest under devDependencies with the proper version of 'sfdx-lwc-jest' ...
POZ's user avatar
  • 4,354
6 votes
Accepted

How to check the CustomEvent detail in a Jest test for an LWC?

This Jest Test Patterns and Mock Dependencies includes the answer: return Promise.resolve().then(() => { expect(handler).toHaveBeenCalledTimes(2); expect(handler.mock.calls[0][...
Keith C's user avatar
  • 136k
6 votes

sfdx force:lightning:lwc:test:run not working

npm i @salesforce/sfdx-lwc-jest This should do the trick
Haider Naseem's user avatar
6 votes
Accepted

LWC Jest: is the subscription part code executed in jest function?

The callback function in your code is not getting called because the standard stubs for lightning messaging service that ship with the sfdx-lwc-jest for the project do not implement it. Since the ...
Mohith Shrivastava's user avatar
6 votes

How to Mock loadScript in JEST

Here an example of custom mock of loadScript for loading moment.js lib, taken from lwc-recipes jest.mock( 'lightning/platformResourceLoader', () => { return { loadScript(...
Oleh Berehovskyi's user avatar
6 votes

Is it alright to change properties to @api so they can be tested in unit test in lwc jest

In my opinion, as a long-time developer, I believe you should consider the component as a black box, only testing its inputs and outputs. If you have behaviours you want to test that are not easily ...
Phil W's user avatar
  • 35.4k
6 votes
Accepted

LWC JEST: TypeError: expect(...).toBeAccessible is not a function

Jest by default does not support testing Accessibility. You will need to make sure you use the Salesforce Accessibility Automation Libraries We have a blog post detailing how to setup and get started ...
Mohith Shrivastava's user avatar
5 votes
Accepted

What's the difference between sfdx-lwc-jest & lwc/jest-preset?

As per my understanding after reading the docs, sfdx-lwc-jest sfdx-lwc-jest works in Salesforce DX projects only. sfdx-lwc-jest, sets up all the necessary Jest configurations for a Salesforce DX ...
sanket kumar's user avatar
  • 15.2k
5 votes

Is Jest mandatory in LWC For appExchange app

Jest is necessary for unit testing your lightning web components. There is no hard requirement today to have unit tests for lightning components unlike apex unit tests. Also jest tests for lwc has ...
Mohith Shrivastava's user avatar
5 votes
Accepted

How do you run all the Jest LWC tests at once in VSCode?

Found the answer in this Test Lightning Web Components documentation: To run all test cases in a Jest test file, click the play icon on the menu bar in the top right corner of the editor window. ...
Keith C's user avatar
  • 136k
5 votes
Accepted

Error: Cannot find module '@salesforce/client/formFactor' while testing my LWC

Out of box sfdx-lwc-jest does not provide a mock stub for @salesforce/client/formFactor and hence you are getting the error. The easiest and simplest way is to implement a jest mock. If you are ...
Mohith Shrivastava's user avatar
5 votes

LWC + Jest: Mocking imperative Apex and returning a value

This is how I did it: jest.mock( "@salesforce/apex/MyController.insertRecords", () => { return { default: jest.fn(payload => { const ...
Matthew Souther's user avatar
5 votes
Accepted

How can I export an LWC function that's declared within a constructor?

In order to use a method, you must expose it as a public method. I wrote a unit test using your code, and it ended up looking like this: import { api, LightningElement } from 'lwc'; export default ...
sfdcfox's user avatar
  • 487k
5 votes
Accepted

registerApexTestWireAdapter is deprecated when working through trailhead Lightning Web Components Tests

createApexTestWireAdapter was added to replace registerApexTestWireAdapter. From the documentation you linked Notice that when testing an apex method used with @wire, you should change your apex ...
Kris Goncalves's user avatar
5 votes
Accepted

How do I mock a child LWC in a parent's Jest Unit Test?

In order to mock childComponent only in the test for parentComponent we need to: Build a skeleton stub for childComponent as a JS file. Instruct the parentComponent test to use this file instead of ...
Rob Baillie's user avatar
4 votes
Accepted

LWC Jest tests failing when adding variant to lightning-radio-group

Different Context but the same answer ;) As you can see here the stub was not updated since two months. You can: Open an Issue in the repo (that makes sense anyways i guess) and wait for an official ...
Renji-xD's user avatar
  • 2,791
4 votes

LWC: unit testing component using validity object

After analysis was able to resolve this with the hep of this link https://books.google.co.in/books?id=wlu8DwAAQBAJ&pg=PA512&lpg=PA512&dq=how+to+mock+this.template.querySelector(%...
Barsha Patro's user avatar
4 votes
Accepted

Mocking CurrentPageReference wire in Jest unit tests - LWC

CurrentPageReference isn't Apex Wire. Use registerTestWireAdapter(CurrentPageReference) instead of Apex Wire :)
KanekiSenapi's user avatar
4 votes

Testing LWC with multiple @wire getRecord calls with Jest

Further to @TrevorBliss's answer - this has now been patched in the latest version (3.x or greater) of the @salesforce/wire-service-jest-util. Paraphrasing from the changelog; to enable mocking data ...
grbeazley's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible