Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change autoRun default #906

Merged
merged 3 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ Content
- [Jest failed to run](#jest-failed-to-run)
- [I don't see "Jest" in the bottom status bar](#i-dont-see-jest-in-the-bottom-status-bar)
- [What to do with "Long Running Tests Warning"](#what-to-do-with-long-running-tests-warning)
- [The extension seems to consume high CPU](#the-extension-seems-to-consume-high-cpu)
- [The tests and status do not match or some tests showing question marks unexpectedly?](#the-tests-and-status-do-not-match-or-some-tests-showing-question-marks-unexpectedly)
- [Want to Contribute?](#want-to-contribute)
- [License](#license)
Expand Down Expand Up @@ -232,8 +231,8 @@ By default, users need not do anything, the extension will automatically trigger
<summary>fully automated</summary>

No need to manually trigger any test run, all changes will be monitored and related tests will be run accordingly. It is basically running jest with `--watch` or `--watchAll`. This is the default mode prior to v4. Example:
- `"jest.autoRun": {"watch": true}` => will start the jest with the watch flag and leave all tests at "unknown" state until changes are detected.
- `"jest.autoRun": {"watch": true, "onStartup": ["all-tests"]}` => will start running all tests upon project launch to update overall project test stats, followed by the jest watch for changes.
- `"jest.autoRun": "watch"` => will start the jest with the watch flag and leave all tests at "unknown" state until changes are detected. This is also the default after v4.7.
- `"jest.autoRun": "legacy"` => will start running all tests upon project launch to update overall project test stats, followed by the jest watch for changes. This was the default prior to v4.7.

</details>

Expand All @@ -245,17 +244,13 @@ Allow users to control test run completely either through commands/menu/TestExpl
- fully manual
- there will be no automatic test run, users will trigger test run by either command or context-menu.
- Example: `"jest.autoRun": "off"`
- automatically run tests when test file changed
- the extension will trigger test run for the given test file upon save.
- Example: "jest.autoRun": `{"watch": false, "onSave": "test-file"}`
- automatically run tests when either test or source file changed:
- automatically run tests when test or source file changed
- the extension will trigger test run for the given test or source file upon save.
- Example: "jest.autoRun": `{"watch": false, "onSave": "test-src-file"}`

- Example: `"jest.autoRun": "on-save"`
</details>

Note: other than the "off" mode, users can specify the "onStartup" option for any "jest.autoRun" config, for example: `{"watch": false, "onSave": "test-file", "onStartup": ["all-tests"]}`

Note: see [jest.autoRun](#autorun) for full control options.
### How to debug tests?

A test can be debugged via the debug codeLens appeared above the [debuggable](#debugcodelensshowwhenteststatein) tests. Simply clicking on the codeLens will launch vscode debugger for the specific test. The extension also supports parameterized tests and allows users to pick the specific parameter set to debug.
Expand Down Expand Up @@ -365,16 +360,16 @@ Users can use the following settings to tailor the extension for their environme
|setting|description|default|example/notes|
|---|---|---|---|
|**Process**|
|autoEnable :x:|Automatically start Jest for this project|true|Please use `autoRun` instead|
|<strike>autoEnable</strike> :x:|Automatically start Jest for this project|true|Please use [autoRun](#autorun) instead|
|[jestCommandLine](#jestCommandLine)|The command line to start jest tests|undefined|`"jest.jestCommandLine": "npm test -"` or `"jest.jestCommandLine": "yarn test"` or `"jest.jestCommandLine": "node_modules/.bin/jest --config custom-config.js"`|
|nodeEnv|Add additional env variables to spawned jest process|null|`"jest.nodeEnv": {"PORT": "9800", "BAR":"true"}` |
|shell|Custom shell (path or LoginShell) for executing jest|null|`"jest.shell": "/bin/bash"` or `"jest.shell": "powershell"` or `"jest.shell": {"path": "/bin/bash"; args: ["--login"]}` |
|[autoRun](#autorun)|Controls when and what tests should be run|undefined|`"jest.autoRun": "off"` or `"jest.autoRun": {"watch": true, "onStartup": ["all-tests"]}` or `"jest.autoRun": false, onSave:"test-only"}`|
|[autoRun](#autorun)|Controls when and what tests should be run|undefined|`"jest.autoRun": "off"` or `"jest.autoRun": "watch"` or `"jest.autoRun": {"watch": false, "onSave":"test-only"}`|
|[rootPath](#rootPath)|The path to your frontend src folder|""|`"jest.rootPath":"packages/app"` or `"jest.rootPath":"/apps/my-app"`|
|[monitorLongRun](#monitorlongrun)| monitor long running tests based on given threshold in ms|60000|`"jest.monitorLongRun": 120000`|
|pathToJest :x:|The path to the Jest binary, or an npm/yarn command to run tests|undefined|Please use `jestCommandLine` instead|
|pathToConfig :x:|The path to your Jest configuration file"|""|Please use `jestCommandLine` instead|
|runAllTestsFirst :x:| Run all tests before starting Jest in watch mode|true|Please use `autoRun` instead|
|runAllTestsFirst :x:| Run all tests before starting Jest in watch mode|undefined|Please use `autoRun` instead|
|**Editor**|
|<strike>enableInlineErrorMessages</strike> :x:| Whether errors should be reported inline on a file|--|This is now deprecated in favor of `jest.testExplorer` |
|[testExplorer](#testexplorer) |Configure jest test explorer|`{"enabled": true}`| `{"enabled": false}`, `{"enabled": true, showClassicStatus: true, showInlineError: true}`|
Expand Down Expand Up @@ -453,14 +448,26 @@ for example:
##### autoRun
```ts
AutoRun =
| 'off'
| "watch" | "off" | "legacy" | "on-save"
| { watch: true, onStartup?: ["all-tests"] }
| {
watch: false,
onStartup?: ["all-tests"],
onSave?: 'test-file' | 'test-src-file',
onSave?: "test-file" | "test-src-file",
}
```

The string type are short-hand for the most common configurations:

| Short Hand | description | actual config | note |
|:-:|---|---|---|---|
|**"watch"** |run jest in watch mode| {"watch": true} | the default mode|
|**"off"**|turn off jest autoRun| {"watch": false} | this is the manual mode |
|**"legacy"**|starting a full test-run followed by jest watch| {"watch": true, "onStartup": ["all-tests"]} | he default mode prior to v4.7 |
|**"on-save"**|run jest upon source or test file changes| {"watch": false, "onSave": "test-src-file"} | |

User can also pass the actual config in the `.vscode/settings.json`, see more example below.

<details>
<summary>example</summary>

Expand All @@ -469,14 +476,17 @@ for example:
"jest.autoRun": "off"
```
- Run all the tests in the workspace upon extension startup, followed by jest watch run for subsequent test/src file changes.
```json
"jest.autoRun": "legacy"
```
or
```json
"jest.autoRun": {
"watch": true,
"onStartup": ["all-tests"]
}
```


- Only run tests in the test file when the test file itself changes. It will neither run all tests for the workspace upon startup nor trigger any test run when the source file changes.
``` json
"jest.autoRun": {
Expand All @@ -485,25 +495,23 @@ for example:
}
```
- Like the one above but does run all tests upon extension start up

``` json
"jest.autoRun": {
"watch": false,
"onSave": "test-file",
"onStartup": ["all-tests"]
}
```
- migration rule from settings prior to v4:
- if `"jest.autoEnabled" = false` => manual mode: `"jest.autoRun": "off"`
- if `"jest.runAllTestsFirst" = false` => `"jest.autoRun": {"watch": true }`
- if no customization of the 2 settings and no `"jest.autoRun"` found =>
``` json
"jest.autoRun": {
"watch": true,
"onStartup": ["all-tests"]
}
```

</details>

Note: migration rule for default autoRun:

- if `"jest.autoEnabled" = false` => manual mode: `"jest.autoRun": "off"`
- if `"jest.runAllTestsFirst" = true` => `"jest.autoRun": {"watch": true, "onStartup": ["all-tests"] }`
- if no customization of the 2 settings and no `"jest.autoRun"` found => `"jest.autoRun": "watch"`

##### testExplorer
```ts
testExplorer =
Expand Down Expand Up @@ -694,8 +702,6 @@ The extension monitor excessive test run with ["jest.monitorLongRun"](#monitorlo
- If the run appeared to hang, i.e. the TestExplorer or statusBar showed test running when it is not. It might be related to this [jest issue](https://github.com/facebook/jest/issues/13187), which should be fixed after release `29.0.2`. If you believe your issue is different, please [file a new issue](https://github.com/jest-community/vscode-jest/issues) so we can take a look.

You can also turn off the monitor or change the threshold with ["jest.monitorLongRun"](#monitorlongrun) to meet your needs.
### The extension seems to consume high CPU
By default the extension will run all tests when it is launched followed by a jest watch process. If you have many resource intensive tests or source files that can trigger many tests when changed, this could be the reason. Check out [jest.autoRun](#autorun) to see how you can change and control when and what tests should be run.

### The tests and status do not match or some tests showing question marks unexpectedly?

Expand Down
13 changes: 4 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,15 @@
},
"jest.nodeEnv": {
"markdownDescription": "The env passed to runner process in addtion to `process.env`",
"type": [
"object",
"null"
],
"type": "object",
"default": null,
"scope": "resource"
},
"jest.shell": {
"markdownDescription": "The shell path or a login-shell to override jest runner process default shell (see Node [child_process.spawn()](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)) for more detail)",
"type": [
"string",
"object",
"null"
"object"
],
"default": null,
"scope": "resource"
Expand All @@ -136,7 +132,7 @@
"jest.runAllTestsFirst": {
"description": "Run all tests before starting Jest in watch mode",
"type": "boolean",
"default": true,
"default": null,
"scope": "resource",
"markdownDeprecationMessage": "**Deprecated**: in favor of `#jest.autoRun#`"
},
Expand Down Expand Up @@ -215,8 +211,7 @@
"markdownDescription": "Control when jest should run (changed) tests. It supports multiple models, such as fully automated, fully manual and onSave... See [AutoRun](https://github.com/jest-community/vscode-jest/blob/master/README.md#how-to-trigger-the-test-run) for details and examples",
"type": [
"object",
"string",
"null"
"string"
],
"default": null,
"scope": "resource"
Expand Down
87 changes: 62 additions & 25 deletions src/JestExt/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { ProjectWorkspace, LoginShell } from 'jest-editor-support';
import { JestProcessRequest } from '../JestProcessManagement';
import {
PluginResourceSettings,
JestExtAutoRunConfig,
JestExtAutoRunSetting,
TestExplorerConfig,
NodeEnv,
MonitorLongRun,
JestExtAutoRunConfig,
JestExtAutoRunShortHand,
} from '../Settings';
import { AutoRunMode } from '../StatusBar';
import { pathToJest, pathToConfig, toFilePath } from '../helpers';
Expand All @@ -23,7 +25,7 @@ export const isWatchRequest = (request: JestProcessRequest): boolean =>
request.type === 'watch-tests' || request.type === 'watch-all-tests';

const autoRunMode = (autoRun: JestExtAutoRunConfig): AutoRunMode => {
if (autoRun === 'off') {
if (autoRun.watch === false && !autoRun.onSave && !autoRun.onStartup) {
return 'auto-run-off';
}
if (autoRun.watch === true) {
Expand All @@ -37,32 +39,34 @@ const autoRunMode = (autoRun: JestExtAutoRunConfig): AutoRunMode => {
}
return 'auto-run-off';
};
/**
* create a backward compatible runMode from the the legacy settings
*/
const getAutoRun = (pluginSettings: PluginResourceSettings): JestExtAutoRunConfig => {
if (pluginSettings.autoRun) {
return pluginSettings.autoRun;
}

if (!pluginSettings.autoEnable) {
return 'off';
}
if (pluginSettings.runAllTestsFirst) {
return { watch: true, onStartup: ['all-tests'] };
export const toAutoRun = (shortHand: JestExtAutoRunShortHand): JestExtAutoRunConfig => {
switch (shortHand) {
case 'legacy':
return { watch: true, onStartup: ['all-tests'] };
case 'default':
case 'watch':
return { watch: true };
case 'off':
return { watch: false };
case 'on-save':
return { watch: false, onSave: 'test-src-file' };
default: {
const message = `invalid autoRun setting "${shortHand}". Will use default setting instead`;
console.error(message);
vscode.window.showErrorMessage(message);
return toAutoRun('default');
}
}

return { watch: true };
};

export const AutoRun = (pluginSettings: PluginResourceSettings): AutoRunAccessor => {
const config = getAutoRun(pluginSettings);
const config = pluginSettings.autoRun;
return {
config,
isOff: config === 'off',
isWatch: config !== 'off' && config.watch,
onSave: config !== 'off' && config.watch === false ? config.onSave : undefined,
onStartup: config !== 'off' ? config.onStartup : undefined,
isOff: config.watch === false && config.onSave == null && config.onStartup == null,
isWatch: config.watch === true,
onSave: config.watch === false ? config.onSave : undefined,
onStartup: config.onStartup,
mode: autoRunMode(config),
};
};
Expand Down Expand Up @@ -137,31 +141,64 @@ const getShell = (config: vscode.WorkspaceConfiguration): string | LoginShell |
}
};

/**
* create a backward compatible runMode from the the legacy settings
*/
const autoRunFromLegacySettings = (
autoEnable?: boolean,
runAllTestsFirst?: boolean
): JestExtAutoRunConfig | undefined => {
if (autoEnable === false) {
return toAutoRun('off');
}
if (runAllTestsFirst === true) {
return toAutoRun('legacy');
}
};

const getAutoRunSetting = (
config: vscode.WorkspaceConfiguration,
autoEnable?: boolean,
runAllTestsFirst?: boolean
): JestExtAutoRunConfig => {
const setting = config.get<JestExtAutoRunSetting | null>('autoRun');

if (!setting) {
return autoRunFromLegacySettings(autoEnable, runAllTestsFirst) ?? toAutoRun('default');
}
if (typeof setting === 'string') {
return toAutoRun(setting);
}
return setting;
};
export const getExtensionResourceSettings = (uri: vscode.Uri): PluginResourceSettings => {
const config = vscode.workspace.getConfiguration('jest', uri);

const autoEnable = config.get<boolean>('autoEnable');
const runAllTestsFirst = config.get<boolean>('runAllTestsFirst') ?? undefined;

return {
showTerminalOnLaunch: config.get<boolean>('showTerminalOnLaunch') ?? true,
autoEnable: config.get<boolean>('autoEnable'),
autoEnable,
enableSnapshotUpdateMessages: config.get<boolean>('enableSnapshotUpdateMessages'),
pathToConfig: config.get<string>('pathToConfig'),
jestCommandLine: config.get<string>('jestCommandLine'),
pathToJest: config.get<string>('pathToJest'),
restartJestOnSnapshotUpdate: config.get<boolean>('restartJestOnSnapshotUpdate'),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
rootPath: path.join(uri.fsPath, config.get<string>('rootPath')!),
runAllTestsFirst: config.get<boolean>('runAllTestsFirst'),
runAllTestsFirst,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
showCoverageOnLoad: config.get<boolean>('showCoverageOnLoad')!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
coverageFormatter: config.get<string>('coverageFormatter')!,
debugMode: config.get<boolean>('debugMode'),
coverageColors: config.get<CoverageColors>('coverageColors'),
autoRun: config.get<JestExtAutoRunConfig>('autoRun'),
testExplorer: config.get<TestExplorerConfig>('testExplorer') ?? { enabled: true },
nodeEnv: config.get<NodeEnv | null>('nodeEnv') ?? undefined,
shell: getShell(config) ?? undefined,
monitorLongRun: config.get<MonitorLongRun>('monitorLongRun') ?? undefined,
autoRun: getAutoRunSetting(config, autoEnable, runAllTestsFirst),
};
};

Expand Down
4 changes: 2 additions & 2 deletions src/JestExt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JestTotalResults, ProjectWorkspace } from 'jest-editor-support';
import * as vscode from 'vscode';
import { LoggingFactory } from '../logging';
import {
JestExtAutoRunConfig,
JestExtAutoRunSetting,
OnSaveFileType,
OnStartupType,
PluginResourceSettings,
Expand All @@ -19,7 +19,7 @@ export enum WatchMode {
WatchAll = 'watchAll',
}
export interface AutoRunAccessor {
config: JestExtAutoRunConfig;
config: JestExtAutoRunSetting;
isOff: boolean;
isWatch: boolean;
onSave: OnSaveFileType | undefined;
Expand Down
6 changes: 4 additions & 2 deletions src/Settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ export type JestTestProcessType =

export type OnStartupType = Extract<JestTestProcessType, 'all-tests'>[];
export type OnSaveFileType = 'test-file' | 'test-src-file';
export type JestExtAutoRunShortHand = 'default' | 'watch' | 'on-save' | 'legacy' | 'off';

export type JestExtAutoRunConfig =
| 'off'
| { watch: true; onStartup?: OnStartupType }
| {
watch: false;
onStartup?: OnStartupType;
onSave?: OnSaveFileType;
};
export type JestExtAutoRunSetting = JestExtAutoRunShortHand | JestExtAutoRunConfig;

export type TestExplorerConfig =
| { enabled: false }
Expand All @@ -42,7 +44,7 @@ export interface PluginResourceSettings {
coverageFormatter: string;
debugMode?: boolean;
coverageColors?: CoverageColors;
autoRun?: JestExtAutoRunConfig;
autoRun: JestExtAutoRunConfig;
testExplorer: TestExplorerConfig;
nodeEnv?: NodeEnv;
shell?: string | LoginShell;
Expand Down
Loading