Skip to main content

Test Lightning Web ComponentsCall APIs from Apex

Test Lightning Web Components with Jest Framework


Prerequisites:


1. Before installing sfdx-lwc-jest install Node.js and npm.
2. Node.js : This page lists two releases of Node.js. We recommend using the “LTS” (Long Term Support) version, rather than the “Current” version.

Install Jest and Its Dependencies into Your Project
npm install @salesforce/sfdx-lwc-jest --save-dev
To Run test:
npm run test:unit

Component Folder Structure
Create a folder named __tests__ at the top level of your component’s bundle directory.
Update .forceignore with **/__tests__/**

Test File Naming Conventions:
Test files must have names that end in “.js”, and Salesforce recommend that tests end in “.test.js.”
Jest runs JavaScript files in the __tests__ directory

Example of package.json:
{
  ...
  "scripts": {
    ...
    "test:unit": "sfdx-lwc-jest",
    "test:unit:watch": "sfdx-lwc-jest --watch",
    "test:unit:debug": "sfdx-lwc-jest --debug",
    ...
  },
  ...
}

For More Details and Examples: Click Here

Sample code to check "Hello World!" output:


import { createElement } from 'lwc';
import Hello from 'c/lwcHelloWorld';

describe('c-lwcHelloWorld', () => {
    afterEach(() => {
        // The jsdom instance is shared across test cases in a single file so reset the DOM
        while (document.body.firstChild) {
            document.body.removeChild(document.body.firstChild);
        }
    });

    it('displays greeting', () => {
        // Create element
        const element = createElement('c-lwcHelloWorld', {
            is: Hello
        });
        document.body.appendChild(element);

        // Verify displayed greeting
        const div = element.shadowRoot.querySelector('div');
        expect(div.textContent).toBe('Hello, World!');
    });
});

Comments

Post a Comment

Popular posts from this blog

Maintain Your Administrator Certification for Spring ’25

  Manage Included Permission Sets in Permission Set Groups via Summaries Update user access more efficiently by specifying which permission set groups include a permission set directly from the permission set’s summary. Previously, to manage included permission sets, you were required to navigate to each permission set group’s page. From Setup, in the Quick Find box, enter  Permission Sets , and select Permission Sets. Select a permission set, and then click  View Summary . In the Related Permission Set Groups tab, click  Add  or  Remove . This change applies to Lightning Experience and Salesforce Classic (not available in all orgs) in Contact Manager, Group, Essentials, Professional, Enterprise, Performance, Unlimited, Developer, and Database.com editions. Sort List Views by Multiple Columns To see your data in a more intuitive way and make your list views more actionable, you can now sort list views on object home pages by up to five columns. Select the c...

Maintain Your Platform Developer I Certification for Winter ’25

  Make Invocable Actions Easier to Configure with New InvocableVariable Modifiers Simplify the configuration of invocable actions using new modifiers from Salesforce. Both the defaultValue and placeholderText modifiers will appear in Flow Builder for the Action elements that correspond to an invocable method. Here’s how to use them. defaultValue Modifier : Set a default value for an input parameter. When the action is used, the input parameter will have a predefined value unless changed by the user. placeholderText Modifier : Set custom placeholder text for an input parameter. Text can provide examples or additional guidance to help users understand what to enter in the input field. Accessing these modifiers in Flow Builder makes it easier to configure and use the actions within your flows. This change applies to Lightning Experience and Salesforce Classic in Performance, Unlimited, Developer, Enterprise, and Database.com editions.

Platform App Builder Certification Maintenance (Winter '26)

 Maintain Your Platform App Builder Certification for Winter ‘26 Trigger a Flow When a File Is Attached Managing files just got a lot smarter. You can now automatically launch a flow whenever a file is attached to a record—no Apex code or complex workarounds required. This enhancement lets you set filters so that your flow triggers only when it matters. You can filter by: File type or extension  (for example, trigger only when a PDF is attached) File name  (for example, when the file name contains “Contract”) Created by  (for example, only for files uploaded by a specific user group) When your conditions are met, the File Attach event automatically kicks off the flow. This opens up powerful new automation use cases, like: Starting a contract approval process when a signed document is uploaded. Sending notifications when a compliance file is attached. Automatically tagging or categorizing records based on file type. To set it up, start by creating an Automation Event–...

Translate