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
0answers
20 views

Write JEST case for LWC component with nested imperative calls

I am passing the data in .mockResolvedValue() method and it is working as expected by providing the correct result. Example : getDataFromApex({accountId : this.accountId}) .then((result) => ...
1
vote
1answer
77 views

I want to cover the onMouseover event in JavaScript by writing JEST test case in LWC

I want to cover the Jest test for onmouseover event for this .tried with few approrch ,seems like its not working. <template> <div class="consumer-container" if:true={showFlags}&...
0
votes
0answers
11 views

Jest Mock deleteRecord to throw error

Is it possible to mock uiRecordApi deleteRecord to throw error? it("should show error toast when delete record method throws error", () => { const element = createElement("c-...
4
votes
1answer
114 views

Jest Tests for KeyPress Event

I am trying to step up my Jest skills and working through Wes Boss classic 30 Vanilla JS projects in 30 Days. I thought I would do them all in LWCs with Jest for a challenge. I built my Drum Kit and ...
0
votes
1answer
48 views

LWC Jest testing toasts

I'm having an issue with testing toasts. I have read a lot about this thanks to google and followed the instructions but I can't seem to test my toast. When I step through the test, I have a ...
0
votes
1answer
72 views

LWC jest mocking '@salesforce/apex/refreshApex' problem

I'm trying to get my arms around testing LWC. I'm trying to test a button click that calls some Apex, then calls 'refreshApex' and then throws a success toast. It seems I'm mocking my own custom Apex ...
1
vote
1answer
61 views

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

I have a function that is declared within my default class (constructor) that I would like to export to a Jest test suite. Is this possible? testLWC.js import { LightningElement } from 'lwc'; export ...
0
votes
0answers
39 views

JEST Coverage for LWC

I have made an LWC component and all functionality seems to be working, however, I am struggling to get test coverage for some functions. I have built some tests for them but according to the code ...
0
votes
0answers
20 views

using query selector with variables

it was my understanding that you could use variables in a query selector (though i could be wrong, it seems), yet this code fails: let fields = [".cmwc__ref_purchdate","....
2
votes
1answer
63 views

Unable to override @salesforce/community/basePath mock within jest test

I'm trying to mock the @salesforce/community/basePath scoped module with different values for each jest test. I've mocked it globally : jest.mock( "@salesforce/i18n/lang", () => {...
0
votes
1answer
84 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
326 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
241 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
47 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
310 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 ...
3
votes
1answer
314 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
87 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
102 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
184 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
63 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
223 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
185 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
262 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
158 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',{ ...
2
votes
1answer
180 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
71 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....
5
votes
2answers
2k 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
73 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
705 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
792 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
611 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
1k 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
925 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
264 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
261 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
1k 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
577 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 ...
3
votes
1answer
137 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
2k 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
593 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
290 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
749 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
113 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
857 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
790 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
4k 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> ...
2
votes
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
671 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 ...