From 6f7052b891335c602bebd8c15249fdbb2c253916 Mon Sep 17 00:00:00 2001
From: Pierre-Marie Dartus
Date: Wed, 13 Jan 2021 10:45:29 +0100
Subject: [PATCH] feat: Add new --skipApiVersionCheck flag (#188)
---
README.md | 28 ++++++++++++++-------------
src/options/options.js | 6 ++++++
src/utils/test-runner.js | 18 ++++++++++++++---
tests/__snapshots__/help.test.js.snap | 24 ++++++++++++-----------
4 files changed, 49 insertions(+), 27 deletions(-)
diff --git a/README.md b/README.md
index d2f8eb4e..285a5354 100644
--- a/README.md
+++ b/README.md
@@ -42,26 +42,28 @@ Alternatively, you can globally install the package and run directly from the co
## Usage
```
-`sfdx-lwc-jest [options]` runs Jest unit tests
+`sfdx-lwc-jest [options]` runs Jest unit tests in SFDX workspace
Options:
- --version Show version number [boolean]
- --coverage Collect coverage and display in output
+ --version Show version number [boolean]
+ --coverage Collect coverage and display in output
[boolean] [default: false]
- --updateSnapshot, -u Re-record every snapshot that fails during a test run
+ -u, --updateSnapshot Re-record every snapshot that fails during a test
+ run [boolean] [default: false]
+ --verbose Display individual test results with the test suite
+ hierarchy [boolean] [default: false]
+ --watch Watch files for changes and rerun tests related to
+ changed files [boolean] [default: false]
+ --debug Run tests in debug mode
+ (https://jestjs.io/docs/en/troubleshooting)
[boolean] [default: false]
- --verbose Display individual test results with the test suite
- hierarchy [boolean] [default: false]
- --watch Watch files for changes and rerun tests related to
- changed files [boolean] [default: false]
- --debug Run tests in debug mode
- (https://jestjs.io/docs/en/troubleshooting)
- [boolean] [default: false]
- --help Show help [boolean]
+ --skipApiVersionCheck Disable the "sourceApiVersion" field check before
+ running tests. [boolean] [default: false]
+ --help Show help [boolean]
Examples:
sfdx-lwc-jest --coverage Collect coverage and display in output
- sfdx-lwc-jest -- --json All params after `--` will be directly passed to Jest
+ sfdx-lwc-jest -- --json All params after `--` are directly passed to Jest
```
## Passing Additional Jest CLI Options
diff --git a/src/options/options.js b/src/options/options.js
index dd75ec88..f085f438 100644
--- a/src/options/options.js
+++ b/src/options/options.js
@@ -37,6 +37,12 @@ const testOptions = {
type: 'boolean',
default: false,
},
+
+ skipApiVersionCheck: {
+ description: 'Disable the "sourceApiVersion" field check before running tests',
+ type: 'boolean',
+ default: false,
+ },
};
module.exports = testOptions;
diff --git a/src/utils/test-runner.js b/src/utils/test-runner.js
index 52217a4b..744d5d22 100644
--- a/src/utils/test-runner.js
+++ b/src/utils/test-runner.js
@@ -18,20 +18,26 @@ const { PROJECT_ROOT, getSfdxProjectJson } = require('./project');
const { jestConfig, expectedApiVersion, jestPath } = require('../config');
// CLI options we do not want to pass along to Jest
-const OPTIONS_BLACKLIST = ['_', '$0', 'debug', 'd'];
+// prettier-ignore
+const OPTIONS_DISALLOW_LIST = [
+ '_',
+ '$0',
+ 'debug', 'd',
+ 'skipApiVersionCheck', 'skip-api-version-check'
+];
function getOptions(argv) {
let options = [];
Object.keys(argv).forEach((arg) => {
- if (argv[arg] && !OPTIONS_BLACKLIST.includes(arg)) {
+ if (argv[arg] && !OPTIONS_DISALLOW_LIST.includes(arg)) {
options.push(`--${arg}`);
}
});
return options.concat(argv._);
}
-async function testRunner(argv) {
+function validSourceApiVersion() {
const sfdxProjectJson = getSfdxProjectJson();
const apiVersion = sfdxProjectJson.sourceApiVersion;
if (apiVersion !== expectedApiVersion) {
@@ -39,6 +45,12 @@ async function testRunner(argv) {
`Invalid sourceApiVersion found in sfdx-project.json. Expected ${expectedApiVersion}, found ${apiVersion}`,
);
}
+}
+
+async function testRunner(argv) {
+ if (!argv.skipApiVersionCheck) {
+ validSourceApiVersion();
+ }
const hasCustomConfig = fs.existsSync(path.resolve(PROJECT_ROOT, 'jest.config.js'));
const config = hasCustomConfig ? [] : ['--config', JSON.stringify(jestConfig)];
diff --git a/tests/__snapshots__/help.test.js.snap b/tests/__snapshots__/help.test.js.snap
index dc759412..d44337ed 100644
--- a/tests/__snapshots__/help.test.js.snap
+++ b/tests/__snapshots__/help.test.js.snap
@@ -4,19 +4,21 @@ exports[`--help attribute shows help 1`] = `
"\`sfdx-lwc-jest [options]\` runs Jest unit tests in SFDX workspace
Options:
- --version Show version number [boolean]
- --coverage Collect coverage and display in output
+ --version Show version number [boolean]
+ --coverage Collect coverage and display in output
[boolean] [default: false]
- -u, --updateSnapshot Re-record every snapshot that fails during a test run
+ -u, --updateSnapshot Re-record every snapshot that fails during a test
+ run [boolean] [default: false]
+ --verbose Display individual test results with the test suite
+ hierarchy [boolean] [default: false]
+ --watch Watch files for changes and rerun tests related to
+ changed files [boolean] [default: false]
+ --debug Run tests in debug mode
+ (https://jestjs.io/docs/en/troubleshooting)
[boolean] [default: false]
- --verbose Display individual test results with the test suite
- hierarchy [boolean] [default: false]
- --watch Watch files for changes and rerun tests related to
- changed files [boolean] [default: false]
- --debug Run tests in debug mode
- (https://jestjs.io/docs/en/troubleshooting)
- [boolean] [default: false]
- --help Show help [boolean]
+ --skipApiVersionCheck Disable the \\"sourceApiVersion\\" field check before
+ running tests [boolean] [default: false]
+ --help Show help [boolean]
Examples:
sfdx-lwc-jest --coverage Collect coverage and display in output