8

When running the Jest test I'm getting this error: Invalid sourceApiVersion found in sfdx-project.json. Expected 47.0, found 48.0 error

My sfdx-project.json looks like this

{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true
    }
  ],
  "namespace": "",
  "sfdcLoginUrl": "https://login.salesforce.com",
  "sourceApiVersion": "48.0"
}

HTML for component under test

<template>
    <div class="message">Hello World!</div>
</template>

Js for component under test

    import { LightningElement } from 'lwc';

    export default class Modal extends LightningElement {}

Simplified Jest test

import { createElement } from "lwc";

import Modal from "c/modal";


describe("c-test-jest initialize", () => {
    it("message should be initialization", () => {
        // Setup
        const modal = initElement();


        // Exercise
        return Promise.resolve().then(() => {


            // Verify
            expect(modal.shadowRoot.querySelector(".message")).not.toBeNull();
        });
    });

    // HELPER

    function initElement() {
        let element = createElement("c-modal", {
            is: Modal
        });

        document.body.appendChild(element);
        return element;
    }
});

Adding Package JSON Screen shot enter image description here

3 Answers 3

7

You need to update your sfdx-lwc-jest node dependency to a more recent version:

  1. Edit package.json
  2. Update the version of sfdx-lwc-jest under devDependencies with the proper version of 'sfdx-lwc-jest' according to your current Salesforce version from here in versions tab
  3. Run npm install or yarn install depending on your Node package manager.
4
  • Thanks for your help and sorry for the delayed response, That was the first thing I checked. I even tried deleting node_modules folder and ran npm install but still getting the same error. I have added a screenshot for your reference. Commented Mar 3, 2020 at 4:06
  • looks like it was a cache issue, I cleared cache using npm cache clean --force and now it works Commented Mar 3, 2020 at 4:13
  • 1
    By the way you might get this error since your project is set-up on the latest none production version of Salesforce; If you do want to use the latest Salesforce version with your Jest tests, you can install the prerelease version: npm install @salesforce/sfdx-lwc-jest@prerelease --save-dev
    – GoldenAxe
    Commented Nov 7, 2020 at 19:51
  • npm view @salesforce/sfdx-lwc-jest version to get the latest version. If your version is explicitly specified in your package.json, upgrade probably won't work by itself. Commented Aug 30 at 16:36
0

If you are running into this error in the Trailhead module:

Salesforce trailhead LWC Testing

Running npm install @salesforce/sfdx-lwc-jest@prerelease --save-dev in the VS code terminal worked for me. I believe there is a lag time between when Salesforce releases their versions and when the project.json file gets updated for npm. Updating to the prelease version should resolve this issue.

0

If you receive an “Invalid sourceApiVersion” error, it is due to an VS Code extension updated with the latest version of Salesforce. "error Invalid sourceApiVersion found in sfdx-project.json. Expected 49.0, found 50.0"

In Visual Studio Code, in the top level directory, open sfdx-project.json. Update the line of code with "sourceApiVersion" to the expected version of the error message you received. "sourceApiVersion": "49.0" Save the file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .