Questions tagged [jest]

Questions related to using the `sfdx-lwc-jest` Jest test framework extension for the purpose of creating unit tests for Lightning Web Components.

Filter by
Sorted by
Tagged with
0
votes
1answer
20 views

How do you grab a title off of a button for Jest testing?

I am trying to grab a title off of a LWC button that is a basic component button with a title as title='Enabled button'. Right now I am checking if it is disabled by doing expect(selectBtn.disabled)....
3
votes
1answer
76 views

LWC Jest test fails when testing data returned after apex imperative call

I believe it is a simple fix, but I am new to everything and Jest being the latest one I am exposed to. I am trying to test if the lightning-combobox received the data to fill its options after the ...
1
vote
1answer
95 views

Unable to run a LWC Jest test for a custom LWC that uses lightning-quick-action-panel or CloseActionScreenEvent

I get this error: Cannot find module 'lightning/quickActionPanel' when running this Jest test: import { createElement } from 'lwc'; import DuplicateScreenAction from 'c/duplicateScreenAction'; let ...
1
vote
0answers
31 views

Why isn't jest being installed with @salesforce/sfdx-lwc-jest?

I'm working with a new project and tried to commit my first Lightning Web Component. I got this error: ESLint: 7.30.0 Error: Error while loading rule 'jest/no-deprecated-functions': Unable to detect ...
0
votes
1answer
111 views

How to get Jest test suite to cover Leaflet Map initialization code?

I'am using a leaflet map in my LWC and it works perfectly fine. However, when I run the test suite for this component using this command test:unit:coverage, I can see that the test script is stuck at ...
1
vote
1answer
112 views

How to unit test a LWC component with @api fields

How can I use Jest to test a LWC component where data is provided via @api annotation? For example, I have a component unitTestComponent.js import { LightningElement, api } from 'lwc'; export ...
1
vote
0answers
34 views

LWC JEST test for API callouts

I want to write JEST test for the following component: // JS file: HTTPCalloutInLWC.js import { LightningElement, track} from 'lwc'; // importing apex class to make callout import getCurrencyData ...
3
votes
0answers
69 views

What is a typical JavaScript code coverage for LWCs (Lightning Web Components) that are adequately tested via JEST?

I know this might get closed as being a matter of opinion, but as long as those opinions are based on actual experience, such answers are valuable to me, particularly if they are somewhat consistent. ...
1
vote
0answers
94 views

With LWC Jest, how to mock custom components

I'm trying to unit test my LWC which has another custom component as child. I tried to use the steps describe in this post : import { createElement } from 'lwc'; import EfxProjectReport from 'c/...
3
votes
2answers
53 views

Jest unit test fails for a property when it is not tagged by @api

That is a chunk of the JS code export default class CurrencyUI extends LightningElement { @api dropdownValue = 'USD'; That is the code for Jest it('Currency default value', () => { const ...
1
vote
0answers
138 views

Proper way to mock svg html template in Jest?

I want to create lwc component which selectively renders svg from html template files. I wonder how to mock that templates for the needs of JEST test. Import the same templates for component and test ...
5
votes
0answers
137 views

Declaratively mocking a custom LWC in a single unit test

The approaches suggested of using jestconfig module mapper or the mocks folder work well if you have a single mock per component you are testing, but not if you have to build how your parent component ...
1
vote
1answer
164 views

LWC + Jest: Assert about what happens before a wire adapter returns data

I have a LWC with a wired function hooked up to the getFiles method in an Apex class, like this: myLwc.html <template> <template if:true={showSpinner}> <lightning-spinner ...
0
votes
1answer
82 views

Not able to access LWC decorator in Jest test JS file?

import {createElement} from 'lwc'; import NewCmp from 'c/newCmp'; describe('c-new-cmp',() => { it('Test New cmp page', () => { const comp = createElement('c-new-cmp',{ ...
1
vote
1answer
141 views

Can we overwrite mocked values returned in LWC Jest?

I am mocking the values in a custom label using this from https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.unit_testing_using_jest_patterns import myImport from '@...
1
vote
1answer
2k views

LWC Error: Cannot set property 'textContent' of null

I have an lwc class with the following HTML: <template if:true={showModalButton}> <lightning-button class="right-col-button" variant="base" label={modalButtonLabel} ...
1
vote
0answers
58 views

issue in jest test

I am writing a jest for the component and and getting below error in the event: ABS.dispatchEvent(new CustomEvent('click')); error i am getting : TypeError: function xyz() {return mockConstructor....
3
votes
2answers
864 views

LWC + Jest: Mocking imperative Apex and returning a value

I'm going to do a Q&A-style entry here, since I couldn't find anything on the web describing this use case. I have seen several threads here describing how to do an empty mock on an Apex call in ...
1
vote
0answers
57 views

Expect() assertion for modal pop up in lwc test

I have an lwc component which is used for file uploading purposes. Below is the code related to modal pop up from html mark up of the component <template if:true={isModalOpen}> <c-...
1
vote
1answer
470 views

LWC + Jest: Tracking calls to a child component's @api-decorated functions

I struggled with this and couldn't find anything on Salesforce StackExchange or Google covering this kind of scenario in an LWC-specific context. So I'm going to post something Q&A-style. I have ...
3
votes
0answers
561 views

Test Class for LWC Component

I have a LWC Component which is used for record upload process. Below is my LWC Javascript code: import { LightningElement, api } from 'lwc'; import file1 from '@salesforce/apex/customLwcController....
1
vote
1answer
434 views

Unable to test variable value with Wire in jest

I am trying to read simple users list with wire method. However, I'm unable to test the variable value in jest promise, is there anything wrong with my code/understanding? LWC - @wire(getUsers) ...
3
votes
1answer
797 views

event.dataTransfer is getting undefined in JEST for dragstart

I'm trying to implement Jest tests for a module(to reorder data table rows). However, I'm unable to mimic the dragstart event. Can anyone help me to achieve the same behavior - Here is the function ...
4
votes
1answer
1k views

Jest LWC, How to test the callback function of a registerListener?

I have a LWC component which performs multiple operations in connectedCallback(), one of them is a registerListener which calls an apex method which updates a record in the DB. Here is my question: ...
3
votes
1answer
641 views

sfdx-lwc-jest' is not recognized as an internal or external command

I am trying to run LWC Jest test, While i'm run this command:"sfdx force:lightning:lwc:test:run" I am getting this error: 'sfdx-lwc-jest' is not recognized as an internal or external command,...
1
vote
1answer
196 views

DOM is not cleared between tests in lwc-jest

For some reason DOM is not cleared between lwc jest tests (I think). What I want to test that page renders with lightning-input disabled by default and after button click it's enabled. Test pass ...
1
vote
1answer
211 views

Cannot assert flowattributechangeevent in lwc test

I've created a lwc component that shows radio button. On Selecting the button, I call handleChange method as shown below. <template> <lightning-radio-group name="radioGroup" ...
2
votes
0answers
894 views

How to write jest test for lightning-datatable LWC

I'm populating the data table in the jest test successfully but when I try to select any row it comes up null which leads to this error TypeError: dataTableRow.click is not a function this is from my ...
1
vote
0answers
444 views

createElement error in jest test case (Lightning Web Component)

Error says the component is not a valid component, or does not extends LightningElement from "lwc". You probably forgot to add the extend clause on the class declaration. Imported ...
2
votes
1answer
121 views

LWC test class failing

I have this apex wire method in an LWC Component @wire(getCountriesData, { proposalId: '$proposalId' }) countriesData(result) { this.wiredCountriesData = result; if (result.data) { ...
2
votes
1answer
1k views

where to see test code coverage for lwc jest Test

I have this below test class which is testing a getPicklistValues method on a LWC component named manageCountries. import { createElement } from 'lwc'; import ManageCountries from 'c/ManageCountries'; ...
0
votes
1answer
447 views

LWC Toast Event JEST Test Issue

I have a JEST test that tests for the presence of a toast event when an exception occurs from an imperative apex call: test('test exception path', () => { getStatusList.mockResolvedValue(...
0
votes
1answer
212 views

How to access static resource in JEST for lighting web component

We are importing JavaScript library in lwc using static resource, I want to test component using JEst. But static resource is not rendering while running the test class ? How can I render ?
1
vote
2answers
657 views

TypeError: Cannot read property 'getElement' of null

It seems like when I have the querySelector('') set to "lightning-card" I get an error. I'm not understanding why. TypeError: Cannot read property 'getElement' of null 19 | ...
0
votes
1answer
82 views

JEST issue - Wired Get Rec

I am writing a Jest to test Wire adapter import { createElement } from 'lwc'; import NtnxMWDRequest from 'c/ntnxMWDRequest'; import { registerLdsTestWireAdapter } from '@salesforce/sfdx-lwc-jest'; ...
2
votes
1answer
718 views

mocking getSelectedRows() method from standard lightning-datatable component's JS class in jest test not working

my LWC component under test have lightning-datatable with onrowselection event, that I want to test in jest. Here is code snippet for component: HTML: <lightning-datatable key-field=...
4
votes
1answer
662 views

LWC: Assertion error when testing @wire chain in Jest

I am trying to write Jest test for a component with two @wired calls (both bound to functions), but I get assertion error about property of second @wire call not being annotated with wire annotation. ...
1
vote
1answer
3k views

Get computed style in Jest Test

I have the following code. Html template <template> <tr class="table__row" aria-hidden={isHidden} data-test-class="table-row"> <td>Cell</td> ...
1
vote
2answers
1k views

npm command not found when working with jest in LWC

I am doing this trailhead module to get started with testing in LWC. The first step is to run below command sfdx force:lightning:lwc:test:setup But when I run this in terminal, I am getting a below ...
2
votes
1answer
576 views

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

I am getting this error: Cannot find module '@salesforce/client/formFactor' when I run jest test, But it works in a scratch org. import { LightningElement, api } from "lwc"; import ...
2
votes
1answer
195 views

In an unit test for an LWC, how can I prove a method executes periodically according to metadata configuration?

I have the following HTML: <template> <h1>Current price of a bitcoin in Euro</h1> <lightning-formatted-number currency-code="EUR" ...
2
votes
1answer
688 views

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

I am trying to prove that the price is being set on the component. I have the following HTML: <template> <h1>Current price of a bitcoin in Euro</h1> <lightning-formatted-...
1
vote
1answer
811 views

In an unit test for an LWC, how can I mock out a fetch to a third party API?

I have a Lightning Web Component with the following HTML: <template> <h1>Current price of a bitcoin in Euro</h1> <lightning-formatted-number currency-code="EUR"...
2
votes
1answer
123 views

LWC Testing framework

We have a really nasty problem with the LWC Jest tests. Every single test takes too much time to execute on Windows. For example, the very same tests take 5 secs to execute on Mac and over 1 minute on ...
6
votes
1answer
565 views

How to mock out custom labels in LWC for a DX org?

I was trying to write unit tests for some of my LWC components, however the ones that use custom labels throw the following exception [LWC error]: Invalid operation on Element [object:vm undefined (2)...
4
votes
3answers
2k views

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

I have written a bunch of tests for LWC. I'm trying to run it using sfdx force:lightning:lwc:test:run but I run into: ERROR running force:lightning:lwc:test:run: No lwc-jest executable found. ...
1
vote
1answer
302 views

Lightning Web Component Jest testing

Hi I am trying to test my child component from the Jest test for that I am trying to fire an event but I am getting an error. Code In Jest: let aBCL = element.shadowRoot.querySelector(a-b-list-k'); ...
1
vote
1answer
1k views

Cannot find module '@jest/globals' from 'abc.test.js'

Has anyone encountered this kind of an error? We had a bunch of jest js files that were working a few months but now are failing. Can any JEST experts help?
0
votes
1answer
1k views

Manipulating track variables in jest tests?

I haven't been able to find any examples or any super helpful info on this. I did find someone posting that track variables are not publicly available to element when you do this: const element = ...
3
votes
0answers
891 views

LWC - Jest test for Apex calls in wired function

We have a class that calls the getRecord uiAPI and then using the result makes an APEX call to get more details from another custom object. import { LightningElement, api, wire, track } from 'lwc'; ...