Questions tagged [lwc-jest]
jest library is used to write unit tests for the front-end JavaScript. This is used in conjunction with LWC to check our front-end modules.
182
questions
1
vote
0
answers
23
views
How can I Jest Test an LWC without relying on implementation details and assuming extended HTML does its job?
I have this test:
it('handles errors from getDummyApiUsers', async () => {
// Arrange
const mockError = new Error('Simulated error from getDummyApiUsers');
...
0
votes
1
answer
32
views
How can I Jest test an Lightning Web Componet which leverages `connectedCallback`?
I have a Lightning Web Component "dapUserList".
This is the HTML:
<template>
<lightning-card title="User List" icon-name="utility:user">
<c-...
0
votes
1
answer
77
views
Jest - How to test that element exists in my component
I'm trying to do a simple test that confirms that a field (from an object) exists on a javascript lwc component. The field I'm trying to check is Sweep_Day__c (from the account object). This field ...
0
votes
1
answer
45
views
JEST Testing for wire methods
import { createElement } from 'lwc';
import ConversationWebComponent from 'c/conversationWebComponent';
import {setImmediate} from 'timers';
import getConversationData from '@salesforce/apex/...
1
vote
0
answers
47
views
Jest framework createElement import error
I've done a bit of looking around and the general answer for this import error is make sure the __test__ directory is added to the .forceignore. I have been following the LWC Tests module but I am ...
13
votes
4
answers
3k
views
How can I test that my component, which implements the standard modal components, is working properly?
Since the update to API 56.0 (Winter '23) Salesforce introduced a new set of components to help developers create modals with the Lightning Experience format in an easier way. The components added ...
0
votes
0
answers
50
views
LWC test async call from button click
I have a custom lightning web component that I want to pass tests but I'm not being able to accomplish with an async call fired from a button click.
html
<lightning-button label="Validate"...
0
votes
1
answer
123
views
LWC Jest: test a component that uses lightning/toast
The lightning/toast utility is GA as of Winter '24, but I haven't seen anything on StackExchange or in the docs about how to write tests for components that use it. One you import it into a component ...
0
votes
0
answers
262
views
Access this.refs / lwc:refs="" when testing in jest?
I am attempting to use this.refs from with a jest test like this:
<template>
<input lwc:ref="inputEl" />
</template>
// input.test.js
import { createElement } from 'lwc'
...
2
votes
1
answer
186
views
How to write jest unit test to verify Nebula logger methods are called
I am using Nebula logger in my lwc to log errors but lost on how to write jest unit test that verifies nebula logger methods are actually called.
Here is sample code:
myLwc.js
import { createLogger } ...
1
vote
1
answer
2k
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 ...
12
votes
3
answers
7k
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 ...
0
votes
1
answer
115
views
Calling child component dynamically until a condition is met
I have recently started coding in LWC and I have a use case that -> I need to call this c-component2 repeatedly from `mycomponent1.html until a condition is met. Is there something I can do to ...
3
votes
1
answer
3k
views
ShowToastEvent is not working for jest test class
MyLighingwebcomponent
<template>
<lightning-card title="Custom Search Functionality in LWC" icon-name="standard:account">
<div class="slds-m-around_medium">
&...
1
vote
2
answers
618
views
getPicklistValues jest test
I have two wire calls to getPicklistValues method in my LWC component.
In my jest test I am calling getPicklistValues twice to emit the test data based on different configs. However the mocking is not ...
5
votes
2
answers
2k
views
My LWC test is not displaying code coverage
I have configured jest for Lightning web component test. My tests are running and passing but code coverage not displaying into output. Following is package.json file: -
{
"name": "my-...
0
votes
1
answer
7k
views
How to use Fetch in Jest Javascript Test
Setting aside that making HTTP requests during tests isn't the best practice.
I am trying to use fetch, but I am getting this error:
ReferenceError: fetch is not defined
Here is the code:
describe('...
9
votes
1
answer
7k
views
How to test LWC onsuccess event handler in Jest
I'm looking for help writing jest test for my onsuccess event handler for my lightning-record-edit form.
I'm new to jest so forgive me if this is covered somewhere else, I just haven't been able to ...
0
votes
0
answers
121
views
How to cover 'Import communityPath from '@salesforce/community/basePath'; in Jest (for LWC)
cmp.js File
Import communityPath from '@salesforce/community/basePath';
test.js File
jest.mock(
'@salesforce/community/basePath',
()=>({ default : '/mocked/community/basePath'}),
);
But ...
1
vote
0
answers
266
views
LWC Jest test can't set @api array/object
I have and array @api property and can't set it in test. It also doesnt work if I'm trying to pass object data and works if i pass just string. When I pass array or object it displays in lwc as [] or {...
1
vote
1
answer
124
views
Testing LWC component using jest-cucumber but createElement not working as expected
I am writing a tests for my LWC component using jest-cucumber. This is my test:
defineFeature(feature, test => {
beforeEach(() => {
element = createElement('c-myLwcElement', {
...
3
votes
0
answers
989
views
Error in LWC test: LWC1127: Invalid directive "$lwc:if" on element <template>
I have the following in my html template using lwc:if:
<template lwc:if={showSpinner}>
<lightning-spinner alternative-text="Loading" size="large"></lightning-...
0
votes
1
answer
33
views
Jest Test Case Reporting
Is there a way to publish Jest test case results?
i.e., instead of only having test cases run locally on a single developers machine, is there a way to upload them to your org or github to be run in ...
2
votes
2
answers
1k
views
LWC jest issue with getRecordNotifyChange - TypeError: (0 , _uiRecordApi.getRecordNotifyChange) is not a function
I am using 'getRecordNotifyChange'https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_get_record_notify in a "Modify" LWC component, to flag a record as ...
4
votes
1
answer
5k
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 ...
3
votes
1
answer
2k
views
How do I mock a child LWC in a parent's Jest Unit Test?
I have two components that are in my app:
parentComponent
childComponent.
'parentComponent' includes 'childComponent' as a component.
E.g: parentComponent.html
<template>
<c-child-...
2
votes
1
answer
487
views
LWC jest mock custom permission: jest.mock( "@salesforce/customPermission/my-custom-permission",
I am mocking custom permission successfully like this:
jest.mock(
"@salesforce/customPermission/my-custom-permission",
() => {
return {
default: true
};...
7
votes
1
answer
778
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 ...
0
votes
1
answer
457
views
Salesforce LWC - State management in checkbox input control
I am new to salesforce LWC. Developing a page with a checkbox inside a component. So my task is to maintain the state of the checkbox even after the page refresh. For this, I used session management ...
3
votes
1
answer
250
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)
{
...
0
votes
1
answer
358
views
How to use objectContaining() or another method to assert that key/value pairs are in a request body in Jest
I am trying to assert that currencyIsoCode: 'USD' is in an HTTP request body. I am able to assert that method: 'POST' is in the request body, so I am assuming the problem has something to do with the ...
0
votes
0
answers
749
views
Jest Error - TypeError: this._environment.runScript is not a function
Test suite failed to run
TypeError: this._environment.runScript is not a function
at Runtime._execModule (node_modules/@salesforce/lwc-jest/node_modules/jest-runtime/build/index.js:856:41)
Hey, I ...
0
votes
1
answer
263
views
Can't test LWC component with getRelatedListRecords LDS?
I create LWC component that contains wire service with importing getRelatedListRecords function. and its work normal.
but the problem is when I create the test using jest-test
I don't know why I ...
0
votes
1
answer
323
views
LWC datatable jest testing: row action menu items
I would appreciate guidance on how to jest test row action menu items.
Are there any reference docs, clear examples or advice, please?
I am unable to find anything directly helpful.
SF general type ...
0
votes
1
answer
3k
views
Error: Cannot find module '@salesforce/sfdx-lwc-jest/config' while testing my LWC from vs code
I have started my practice on LWC testing While i'm run this command:"npm run test:unit"
i am getting this error:Cannot find module '@salesforce/sfdx-lwc-jest/config'.
Error body is:
Error: Cannot ...
3
votes
1
answer
1k
views
LWC JEST: TypeError: expect(...).toBeAccessible is not a function
I am facing following error while running lwc test in newly created Salesforce DX project. I have installed JEST using sfdx force:lightning:lwc:test:setup command.
This error I am facing only for ...
0
votes
1
answer
119
views
lwc testing TypeError: element.wireRecord is not a function
I am trying to do a test when no records are returned. Error is here: element.wireRecord(wireRecord);
test code is:
it('Check no quote found error', () => {
const element = createElement('c-bid-...
2
votes
1
answer
297
views
Mocked imperative Apex method does not return mocked value
My lwc has an input and a button. When I press the button an apex call with the input's value is made. Even though the method is called with the right params, the return value is some sort of empty ...
3
votes
1
answer
169
views
Two Wires to same service confusing emits in Jest
I have this in an LWC:
import {getObjectInfo} from 'lightning/uiObjectInfoApi';
@wire(getObjectInfo, { objectApiName: 'My_First_Object__c' })
receiveMyFirstInfo({error, data}) {
if (data) {
...
1
vote
1
answer
1k
views
Cannot find module 'lightning/actions' import { CloseActionScreenEvent } from "lightning/actions";
Is there a valid solution for this issue in latest versions of @salesforce/sfdx-lwc-jest? I implemented the below solution and still get the error:
"@salesforce/sfdx-lwc-jest": "1.1.0&...
0
votes
1
answer
540
views
Cannot mock an lwc getter with Jest
I created a lwc in which I can't mock a getter in the class for unit testing. The class is as follows:
export default class CreateSubscription extends LightningElement {
isSubAvailable;
...
// ...
0
votes
1
answer
9k
views
Jest error:- TypeError: Cannot read properties of null (reading 'dispatchEvent')
I am failing to understand the error source in my first attempt at 'jesting'.
import { createElement } from 'lwc';
import Lwcsearchcomponent from 'c/lwcsearchcomponent'
describe('lwcsearchcomponent ...
0
votes
0
answers
2k
views
Jest exceed timeout when using fake timers
I am writing jest test for search input and it give me an error that timeout is exceeded when i am using fake timers. I was trying to increase timeout but received the same error
I have looked into ...
0
votes
0
answers
485
views
LWC/Jest/PapaParse: How to ensure mocked function can call private function?
thanks in advance for any help!
I'm currently trying to write a test for the following behavior: the User uploads a .csv file to a lightning-input element of my LWC, and an HTML element of the ...
3
votes
1
answer
2k
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 ...
0
votes
0
answers
937
views
Writing a JEST Test for a Function that Contains a Promise and setTimeout
I'm trying to write a test for a Lightning Web Component that fires a custom event, which causes a function to execute in the component.
The code for the test is below:
it('make recurring ...
1
vote
1
answer
383
views
Wire Service Jest Test - Config Object Empty so filterFn not working properly
So, I followed:
https://github.com/salesforce/wire-service-jest-util
Testing LWC with multiple @wire getRecord calls with Jest
to help get my test configuration setup for multiple @wire getRecord ...
2
votes
2
answers
2k
views
Mocking Apex Wire Adapter - "emit is not a function"
I am trying to set up a very simple test of a LWC apex wire adapter. This includes mocking the apex response with the contents of a JSON file as shown in the LWC docs.
...
import MyComponent from 'c/...
0
votes
1
answer
1k
views
LWC jest test on conditional element
How can I test if:true element in my lwc? showLoadingSpinner is @api. I am new to jest, sorry if stupid question. Console log shows null.
<template if:true={showLoadingSpinner}>
<div id=&...
0
votes
1
answer
577
views
Lightning button not rendered in jest test
I'm trying to write a jest test for lwc component, and facing an issue with rendering . For example, if we add the condition like this
<template if:true={booleanFlagValue}>
<lightning-...