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.
181
questions
0
votes
0
answers
20
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
21
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
105
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
124
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 } ...
0
votes
1
answer
69
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 ...
0
votes
1
answer
5k
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('...
0
votes
0
answers
83
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
1
answer
79
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
682
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
26
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
1
answer
1k
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-...
0
votes
0
answers
87
views
LWC jest tests mock child component or use existing tests
I have multiple custom components. Code just for example. childLwc is standalone component and has its own tests. So my question is how can i use childLwc without mocking everything I need again. Can ...
0
votes
1
answer
310
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 ...
0
votes
1
answer
285
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
671
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
0
answers
312
views
Issue when test connectedCallback method on LWC component that use more that one module mock
I have the following files to define a LWC.
customComponent.js
import {api, LightningElement} from "lwc";
import getResult01 from "@salesforce/apex/Controller01.getResult01";
...
0
votes
1
answer
207
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
254
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 ...
2
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
97
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-...
0
votes
0
answers
151
views
jest lwc code coverage overlay not working
I can't get lwc jest code coverage overlay to work in vs code. I have installed the jest extension. jest.config.js is there with the following code:
const { jestConfig } = require('@salesforce/...
0
votes
0
answers
83
views
LWC JEST Debug Test issue- Unbound breakpoint
I am trying to debug lwc JEST for hello world for lwc recipe.
https://github.com/trailheadapps/lwc-recipes
For any test when I am adding breakpoint and selecting debug test
option seeing following ...
2
votes
1
answer
227
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 ...
1
vote
1
answer
697
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&...
3
votes
1
answer
129
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) {
...
0
votes
1
answer
454
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
8k
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
419
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 ...
2
votes
1
answer
416
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
};...
0
votes
0
answers
848
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 ...
12
votes
4
answers
2k
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 ...
1
vote
0
answers
223
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 {...
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
455
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-...
2
votes
1
answer
1k
views
Any way in LWC jest testing to make sure a child component received the right data from the parent?
I understand the philosophy that Jest is about "black box" testing, where you just observe the effects of the code. So you have limited access to things, and have to rely on observing ...
0
votes
0
answers
1k
views
How to Jest file upload
I am stuck with Jest testing of file resize/upload logic.
This is a simple job to resize uploaded image and add it to the Notes & Attachments based on recordId.
It is not a production code - it ...
2
votes
2
answers
1k
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
115
views
JEST with Light DOM components
I'm trying to write tests for my LWC components. There also "Light DOM" components present but it seems like there is no support for it:
This is the error I'm getting:
Error: Assert ...
0
votes
1
answer
246
views
Unable to create Jest tests for generation 2 package
I have created an LWC app, and am now trying to add code test coverage. But unfortunately, Jest (@salesforce/[email protected]) is failing in two different ways:
I use the getRecords wire adapter, ...
2
votes
1
answer
719
views
Testing LWC with Jest, datatable data array length not updating after promises are resolved
I've been writing lwcs since they came out, but never wrote unit tests for them. Just getting my feet wet here :)
I have an lwc with three datatables. The first one has row actions that process the ...
1
vote
1
answer
381
views
Jest error: Cannot find module 'lightning/alert' from '.../newAccount.test.js'
When I run the test for my LWC I have this error:
I read about other issues like that where the solution is to mock the missing function but I cant find something related to action in the repo
my ...
-2
votes
1
answer
353
views
Make Jest for LWC [closed]
I have controller.js . It returns the ID of the case and displays duplicates of this case on the case page. The code works without problems, but it doesn't work with tests in any way
import { ...
1
vote
1
answer
1k
views
Getting a null error in Jest test
i am new to LWC and jest test.So while doing the testing i have got an error.
TypeError: Cannot read properties of null (reading 'style')
js file
@wire(getItemProperty, { iId: '$contactId' })
...
0
votes
1
answer
2k
views
How to select an element inside a template if true in LWC Jest test?
I have an LWC app that has a lightning-input and a lighning-button. It fetches data from the server for a record whose ID is specified. I have successfully mocked the server calls.
I am trying to ...
0
votes
1
answer
2k
views
How do you click on checkbox group in jest?
I am trying to JEST test a checkbox group that has 1 option and when I do .click() it says can't click null property. Is there a different way I should be hitting it with JEST?
For clarification, the ...
1
vote
1
answer
318
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
1
answer
2k
views
How to use Cobertura and JUnit reporters with LWC Jest?
When creating a new SFDX project, the default Jest configuration for LWC unit tests looks like this:
jest.config.js
const { jestConfig } = require('@salesforce/sfdx-lwc-jest/config');
module.exports =...
0
votes
0
answers
624
views
jest dom is not updating after element.innerText is set imperatively
I am testing a component that renders a table. Each row of the table body has a column that contains a button, when the button is pressed a javascript handler modifies the cell by setting the ...
0
votes
1
answer
158
views
Error in executing JEST Unit Test cases in react-native project with Salesforce Mobile SDK created by forcereact
We are creating React-Native mobile App Using Salesforce Mobile SDK.
In this project we setup JEST for unit test cases writing. But whenever it goes into Salesforce Mobile SDK API it failed and sends ...