Skip to content

5) How to run your jest tests

Coding With The Force edited this page Feb 19, 2023 · 5 revisions

How to Run your Jest Tests

Once you've setup your project and written your jest tests, you likely want to actually run them! However, jest tests cannot run from inside your salesforce org, nor can they be deployed to it! So you have to run them outside your org in an IDE or via a CI/CD tool you have at your disposal. Below we'll go over the best ways to run our jest tests.


Running your Jest Tests

The way to run your jest tests without writing custom scripts is to just run one of the two commands below in your Terminal:

Run All Jest Tests in your Project:

node ./node_modules/@salesforce/sfdx-lwc-jest
/bin/sfdx-lwc-jest

Run a Single Jest Test in your Project:

There are two ways to run a single jest file in your project, you can either use the exact path of the LWC component or just use the name of the component using the -t parameter in the command. We'll take a look at both below:

Optional -t Parameter Example (Suggested Approach):

node ./node_modules/@salesforce/sfdx-lwc-jest
/bin/sfdx-lwc-jest -t nameOfLWCComponent

Exact Path Example:

node ./node_modules/@salesforce/sfdx-lwc-jest
/bin/sfdx-lwc-jest "exact path to your single test"

And that's all there is to it! You can run your Jest Tests now! However, it's kinda ugly right? I mean it's simple (and it works), but typing that out every time is like not spectacular. No worries, we'll figure out how to simplify that in a section below, but first let's go over some optional jest parameters.


Optional Jest Command Parameters

There are three very useful optional Jest Command Parameters that can be appended to the end of your command to run your jest test, they are outlined below:

  1. --watch - This command adds a listener after it is run. After running it, anytime you go into a jest test, update it and then save it, it will automatically run that jest test for you. Pretty kewl! The command would look like this node ./node_modules/@salesforce/sfdx-lwc-jest /bin/sfdx-lwc-jest --watch.
  2. --debug - This command runs a debugger that will assist you with your jest tests if they are being problematic. It's a super useful tool you should keep at the ready. The command would look like this node ./node_modules/@salesforce/sfdx-lwc-jest /bin/sfdx-lwc-jest --debug.
  3. --coverage - This command runs a coverage check for all lwc's in your project and then outputs a table with the results. Use this to check on your orgs lwc coverage! The command would look like this node ./node_modules/@salesforce/sfdx-lwc-jest /bin/sfdx-lwc-jest.

Using Custom Scripts to simplify your Jest Test Runs

If you wanna get a little fancy and make your commands to run Jest a litttttttle bit easier, we can update our package.json file to simplify things a bit. If you open up your package.json, you should see this inside of it (or something similar):

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  }

We need to update that bad boi. Instead of that auto-generated scripts, let's replace it with this:

"scripts": {
    "test": "npm run test:unit",
    "test:unit": "sfdx-lwc-jest",
    "test:unit:watch": "sfdx-lwc-jest --watch",
    "test:unit:debug": "sfdx-lwc-jest --debug",
    "test:unit:debug:watch": "sfdx-lwc-jest --debug --watch",
    "test:unit:coverage": "sfdx-lwc-jest --coverage"
  }

What this allows you to do is, instead of running this gigantic command to run your jest tests node ./node_modules/@salesforce/sfdx-lwc-jest /bin/sfdx-lwc-jest, now all you need to do is run the command npm run test. And just like that, your life becomes a little bit easier.

You can probably see however that we have 5 different scripts up there, so let's go over them:

  1. test and test:unit are effectively the same thing. test is just an even simpler structure to test:unit. You can run either and they will do the same thing. npm run test or npm run test:unit will have the same result.
  2. test:unit:watch runs your tests but it also adds the optional jest watch parameter. What this does is, every time you update and save a jest test from now on, it will auto run the test when it saves. The command is npm run test:unit:watch.
  3. test:unit:debug runs your tests but it also attaches a debugger to them. This can come in handy if you are having issues troubleshooting problems with your jest tests. The command is npm run test:unit:debug.
  4. test:unit:debug:watch is just a combination of test:unit:watch and test:unit:debug. If you want them both running at the same time, use this command. The command is npm run test:unit:debug:watch.
  5. test:unit:coverage adds the optional jest coverage parameter to your command. This parameter outputs a chart that shows you the coverage of each lwc js file in your project. It's super useful! The command is npm run test:unit:coverage

To test just a single test instead of all tests for any one of these commands, just append the path to the single file you'd like to test to the end of the command.