Skip to content

Commit 3b8c6b3

Browse files
committed
fix: make lwc a peerDependency
1 parent c3ace89 commit 3b8c6b3

File tree

10 files changed

+52
-175
lines changed

10 files changed

+52
-175
lines changed

README.md

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,34 @@
22

33
Run Jest against Lightning web components in a Salesforce DX workspace environment.
44

5-
## Versions
6-
7-
To test against the latest Salesforce production instances, use the npm tag appropriate for the current release, e.g.:
5+
## Install
86

7+
```shell
8+
# npm
9+
npm install --save-dev @salesforce-sfdx-lwc-jest lwc
910
```
10-
yarn add -D @salesforce/sfdx-lwc-jest@winter22
11-
yarn add -D @salesforce/sfdx-lwc-jest@spring22
12-
```
13-
14-
The npm `latest` tag corresponds to the latest version of this repo, not necessarily Salesforce production versions.
15-
16-
To see a full list of available versions, and the tag that maps to the corresponding Salesforce release, see [the list of `npm` package versions](https://www.npmjs.com/package/@salesforce/sfdx-lwc-jest?activeTab=versions).
17-
18-
## Invalid sourceApiVersion found in sfdx-project.json
19-
20-
If you see this error while running tests in your Salesforce DX project, it most likely means you are using the incorrect version of this project.
2111

22-
For example, the error message `Invalid sourceApiVersion found in sfdx-project.json. Expected 45.0, found 46.0` means this project is targeted to API version 45.0, which maps to Spring '19, but the Salesforce DX project the tests are run in is using API version 46.0, which maps to Summer '19. The version check is done against the projects `sourceApiVersion` field in the `sfdx-project.json` file at the top level of the project.
12+
```shell
13+
# yarn
14+
npm install --save-dev @salesforce-sfdx-lwc-jest lwc
15+
```
2316

24-
To fix this issue, make sure the most recent version of this project is being used, or switch to the `prerelease` version, depending on what release your target org is on.
17+
As of version 6.0.0 of `@salesforce/sfdx-lwc-jest`, you must install `lwc` alongside the package.
2518

26-
## Disabling the sourceApiVersion check
19+
## Versions
2720

28-
The `sourceApiVersion` field check is not a perfect check. Projects may be targeting orgs that are on the current release, but still have an older `sourceApiVersion` value set in their `sfdx-project.json` file. To disable this check, run tests with the `--skipApiVersionCheck` flag set.
21+
To test against the latest Salesforce production instances, ensure that your `lwc` dependency corresponds to the Salesforce release you want to test against. For example:
2922

30-
**Note that by doing this, you risk running with an old version of the test runner and your tests may be using an out of date version of the LWC framework. To ensure tests are always run with the proper framework version and configuration, make sure to be on the most recent `latest` or `prerelease` tagged version of this package.**
23+
```shell
24+
npm install --save-dev lwc@winter22
25+
npm install --save-dev lwc@spring22
26+
```
3127

32-
## Installation
28+
The npm `latest` tag corresponds to the latest version of the [LWC open-source framework](https://github.com/salesforce/lwc), not necessarily Salesforce production versions.
3329

34-
Add this project as a devDependency:
30+
To see a full list of available versions, and the tag that maps to the corresponding Salesforce release, see [the list of `npm` package versions](https://www.npmjs.com/package/lwc?activeTab=versions).
3531

36-
```bash
37-
yarn add -D @salesforce/sfdx-lwc-jest
38-
```
32+
## Running tests
3933

4034
Update your project's unit testing script in `package.json` to execute `sfdx-lwc-jest`:
4135

@@ -72,13 +66,6 @@ Options:
7266
--debug Run tests in debug mode
7367
(https://jestjs.io/docs/en/troubleshooting)
7468
[boolean] [default: false]
75-
--skipApiVersionCheck Disable the "sourceApiVersion" field check before
76-
running tests. **Warning** By disabling this check
77-
you risk running tests against stale versions of
78-
the framework. See details here:
79-
https://github.com/salesforce/sfdx-lwc-jest#disabli
80-
ng-the-sourceApiVersion-check
81-
[boolean] [default: false]
8269
--help Show help [boolean]
8370
8471
Examples:

package.json

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,15 @@
3333
"test": "jest"
3434
},
3535
"dependencies": {
36-
"@lwc/engine-dom": "6.5.1",
37-
"@lwc/engine-server": "6.5.1",
38-
"@lwc/jest-preset": "16.0.0",
39-
"@lwc/jest-resolver": "16.0.0",
40-
"@lwc/jest-serializer": "16.0.0",
41-
"@lwc/jest-transformer": "16.0.0",
42-
"@lwc/module-resolver": "6.5.1",
43-
"@lwc/synthetic-shadow": "6.5.1",
44-
"@lwc/wire-service": "6.5.1",
45-
"@salesforce/wire-service-jest-util": "4.1.4",
36+
"@lwc/jest-preset": "^16.0.0",
37+
"@lwc/jest-resolver": "^16.0.0",
38+
"@lwc/jest-serializer": "^16.0.0",
39+
"@lwc/jest-transformer": "^16.0.0",
40+
"@salesforce/wire-service-jest-util": "^4.1.4",
4641
"fast-glob": "^3.3.2",
47-
"jest": "29.7.0",
48-
"jest-environment-jsdom": "29.7.0",
49-
"yargs": "~17.7.2"
42+
"jest": "^29.7.0",
43+
"jest-environment-jsdom": "^29.7.0",
44+
"yargs": "^17.7.2"
5045
},
5146
"devDependencies": {
5247
"@babel/core": "^7.24.4",
@@ -58,6 +53,9 @@
5853
"lint-staged": "^15.2.2",
5954
"prettier": "^3.2.5"
6055
},
56+
"peerDependencies": {
57+
"lwc": ">=6"
58+
},
6159
"volta": {
6260
"node": "20.12.1",
6361
"yarn": "1.22.22"

scripts/upgrade-lwc.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,4 @@ const jestConfig = {
4444
snapshotSerializers: [require.resolve('@lwc/jest-serializer')],
4545
};
4646

47-
const expectedApiVersion = '61.0';
48-
49-
module.exports = { jestConfig, expectedApiVersion };
47+
module.exports = { jestConfig };

src/options/options.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ const testOptions = {
3737
type: 'boolean',
3838
default: false,
3939
},
40-
41-
skipApiVersionCheck: {
42-
description:
43-
'Disable the "sourceApiVersion" field check before running tests. **Warning** By disabling this check you risk running tests against stale versions of the framework. See details here: https://github.com/salesforce/sfdx-lwc-jest#disabling-the-sourceApiVersion-check',
44-
type: 'boolean',
45-
default: false,
46-
},
4740
};
4841

4942
module.exports = testOptions;

src/utils/__mocks__/project.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
*/
77
'use strict';
88

9-
const { expectedApiVersion } = require('../../config');
10-
119
module.exports = {
1210
PROJECT_ROOT: 'projectRoot',
1311
getSfdxProjectJson: () => {
14-
return { mock: true, sourceApiVersion: expectedApiVersion };
12+
return { mock: true };
1513
},
1614
getModulePaths: () => ['force-app/main/unix/lwc', 'force-app\\main\\windows\\lwc'],
1715
};

src/utils/project.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ function getModulePaths() {
3838

3939
module.exports = {
4040
PROJECT_ROOT,
41-
getSfdxProjectJson,
4241
getModulePaths,
4342
};

src/utils/test-runner.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,14 @@ const fs = require('fs');
1010
const path = require('path');
1111
const { spawn } = require('child_process');
1212

13-
const { PROJECT_ROOT, getSfdxProjectJson } = require('./project');
13+
const { PROJECT_ROOT } = require('./project');
1414

15-
const { error, info } = require('../log');
16-
const { jestConfig, expectedApiVersion } = require('../config');
15+
const { info } = require('../log');
16+
const { jestConfig } = require('../config');
1717

1818
// List of CLI options that should be passthrough to jest.
1919
const JEST_PASSTHROUGH_OPTIONS = new Set(['coverage', 'updateSnapshot', 'verbose', 'watch']);
2020

21-
function validSourceApiVersion() {
22-
const sfdxProjectJson = getSfdxProjectJson();
23-
const apiVersion = sfdxProjectJson.sourceApiVersion;
24-
if (apiVersion !== expectedApiVersion) {
25-
error(
26-
`Invalid sourceApiVersion found in sfdx-project.json. Expected ${expectedApiVersion}, found ${apiVersion}`,
27-
);
28-
}
29-
}
30-
3121
function getJestPath() {
3222
const packageJsonPath = require.resolve('jest/package.json');
3323

@@ -71,10 +61,6 @@ function getJestArgs(argv) {
7161
}
7262

7363
async function testRunner(argv) {
74-
if (!argv.skipApiVersionCheck) {
75-
validSourceApiVersion();
76-
}
77-
7864
const spawnCommand = 'node';
7965
const spawnArgs = [getJestPath(), ...getJestArgs(argv)];
8066

tests/__snapshots__/help.test.js.snap

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,19 @@ exports[`--help attribute shows help 1`] = `
44
"\`sfdx-lwc-jest [options]\` runs Jest unit tests in SFDX workspace
55
66
Options:
7-
--version Show version number [boolean]
8-
--coverage Collect coverage and display in output
7+
--version Show version number [boolean]
8+
--coverage Collect coverage and display in output
99
[boolean] [default: false]
10-
-u, --updateSnapshot Re-record every snapshot that fails during a test
11-
run [boolean] [default: false]
12-
--verbose Display individual test results with the test suite
13-
hierarchy [boolean] [default: false]
14-
--watch Watch files for changes and rerun tests related to
15-
changed files [boolean] [default: false]
16-
--debug Run tests in debug mode
17-
(https://jestjs.io/docs/en/troubleshooting)
10+
-u, --updateSnapshot Re-record every snapshot that fails during a test run
1811
[boolean] [default: false]
19-
--skipApiVersionCheck Disable the "sourceApiVersion" field check before
20-
running tests. **Warning** By disabling this check
21-
you risk running tests against stale versions of
22-
the framework. See details here:
23-
https://github.com/salesforce/sfdx-lwc-jest#disabli
24-
ng-the-sourceApiVersion-check
12+
--verbose Display individual test results with the test suite
13+
hierarchy [boolean] [default: false]
14+
--watch Watch files for changes and rerun tests related to
15+
changed files [boolean] [default: false]
16+
--debug Run tests in debug mode
17+
(https://jestjs.io/docs/en/troubleshooting)
2518
[boolean] [default: false]
26-
--help Show help [boolean]
19+
--help Show help [boolean]
2720
2821
Examples:
2922
sfdx-lwc-jest --coverage Collect coverage and display in output

0 commit comments

Comments
 (0)