Skip to content

Commit

Permalink
setup wizard beta (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz authored Apr 4, 2021
1 parent d50d127 commit adb9048
Show file tree
Hide file tree
Showing 28 changed files with 2,831 additions and 42 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Please add your own contribution below inside the Master section
Bug-fixes within the same version aren't needed
## Master
* added setup extension wizard - @connectdotz (#655)
* update doc to give warning on potential incorrect coverage in watch mode - @connectdotz(#655)
* Introducing a new interactive mode, in addition to the current "watch" mode, to run jest tests. Use the new `jest.autoRun` settings to control when and what tests should be run automatically, or not. - @connectdotz (#674)
* refactor jest process management to adopt queue-based model with promise-based process execution. - @connectdotz (#674)
* enhance statusBar to display autoRun and workspace stats - @connectdotz (#674)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ It is recommended not to add the following options as they are managed by the ex
<details>
<summary><a id="rootPath"></a>rootPath</summary>

If your project doesn't live in the root of your repository, you may want to customise the `jest.rootPath` setting to enlighten the extension as to where to look. For instance: `"jest.rootPath": "src/client-app"` will direct the extension to use the `src/client-app` folder as the root for Jest.
If your project doesn't live in the root of your repository, you may want to customize the `jest.rootPath` setting to enlighten the extension as to where to look. For instance: `"jest.rootPath": "src/client-app"` will direct the extension to use the `src/client-app` folder as the root for Jest.
</details>

<details>
Expand Down Expand Up @@ -369,7 +369,7 @@ Please see [vscode Key Bindings](https://code.visualstudio.com/docs/getstarted/k


## Troubleshooting
Sorry you are having trouble with the extension. If your issue did not get resolved after checking out the [how-to](#how-to-) section and the tips below, feel free to [ask](https://github.com/jest-community/vscode-jest/issues) the community, chances are some one else had a similar experience and could help resolving it.
Sorry you are having trouble with the extension. If your issue did not get resolved after checking out the [how-to](#how-to-use-the-extension) section and the tips below, feel free to [ask](https://github.com/jest-community/vscode-jest/issues) the community, chances are some one else had a similar experience and could help resolving it.

<details>
<summary>I don't see "Jest" in the bottom status bar</summary>
Expand All @@ -379,7 +379,7 @@ Sorry you are having trouble with the extension. If your issue did not get resol
</details>

<details>
<summary>I got "Jest failed too many times..." error message</summary>
<summary>I got "Jest Process xxx failed unexpectedly..." or "Jest failed too many times..." error message</summary>

This usually mean the extension is not able to start jest process for you. First check the Jest OUTPUT channel or developer console to see what is the actual error (see [self-diagnosis](#self-diagnosis)).

Expand All @@ -389,7 +389,7 @@ Sorry you are having trouble with the extension. If your issue did not get resol
```
The issue is probably not related to this extension. If this only happened occasionally after launch or you saw vscode warning about shell start up slow, try to simplified the env files, restart vscode or reload windows. See [Resolving Shell Environment is Slow](https://code.visualstudio.com/docs/supporting/faq#_resolving-shell-environment-is-slow-error-warning).
If you see error about not able to find `jest` or some other jest related runtime error: if you can run jest from terminal then you can use the **"Run Setup Wizard"** button in the error panel to help resolving the configuration issue. There could be other causes, such as jest test root path is different from the project's, which can be fixed by setting `jest.rootPath`. Feel free to check out the [customization](#customization) section to manually adjust the extension if needed.
If you see error about not able to find `jest` or some other jest related runtime error: if you can run jest from terminal then you can use the **"Run Setup Wizard"** button in the error panel to help resolving the configuration issue, see [Setup Wizard](setup-wizard.md) for more details. There could be other causes, such as jest test root path is different from the project's, which can be fixed by setting [jest.rootPath](#rootPath). Feel free to check out the [customization](#customization) section to manually adjust the extension if needed.
</details>
<details>
Expand Down
13 changes: 13 additions & 0 deletions __mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ const OverviewRulerLane = {
const Uri = {
file: (f) => f,
parse: jest.fn(),
joinPath: jest.fn(),
};
const Range = jest.fn();
const Position = jest.fn();
const Diagnostic = jest.fn();
const ThemeIcon = jest.fn();
const DiagnosticSeverity = { Error: 0, Warning: 1, Information: 2, Hint: 3 };
const ConfigurationTarget = { Global: 1, Workspace: 2, WorkspaceFolder: 3 };

const debug = {
onDidTerminateDebugSession: jest.fn(),
Expand All @@ -56,8 +60,13 @@ const commands = {
registerTextEditorCommand: jest.fn(),
};

// eslint-disable-next-line @typescript-eslint/no-empty-function
const CodeLens = function CodeLens() {};

const QuickInputButtons = {
Back: {},
};

export {
CodeLens,
languages,
Expand All @@ -67,8 +76,12 @@ export {
OverviewRulerLane,
Uri,
Range,
Position,
Diagnostic,
ThemeIcon,
DiagnosticSeverity,
ConfigurationTarget,
debug,
commands,
QuickInputButtons,
};
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "4.0.0-alpha.4",
"publisher": "Orta",
"engines": {
"vscode": "^1.23.0"
"vscode": "^1.45.0"
},
"author": {
"name": "Orta Therox, ConnectDotz & Sean Poulter",
Expand Down Expand Up @@ -233,13 +233,17 @@
"command": "io.orta.jest.editor.workspace.run-all-tests",
"title": "Jest: Run All Tests in Current Workspace"
},
{
"command": "io.orta.jest.editor.workspace.toggle-coverage",
"title": "Jest: Toggle Coverage for Current Workspace"
},
{
"command": "io.orta.jest.editor.run-all-tests",
"title": "Jest: Run Related Tests"
},
{
"command": "io.orta.jest.editor.workspace.toggle-coverage",
"title": "Jest: Toggle Coverage for Current Workspace"
"command": "io.orta.jest.setup-extension",
"title": "Jest: Setup Extension (Beta)"
}
],
"menus": {
Expand Down Expand Up @@ -390,7 +394,7 @@
"@types/istanbul-lib-source-maps": "^4.0.1",
"@types/jest": "^26.0.15",
"@types/node": "^8.0.31",
"@types/vscode": "^1.23.0",
"@types/vscode": "^1.45.0",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"copy-webpack-plugin": "^6.0.2",
Expand Down
134 changes: 134 additions & 0 deletions setup-wizard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Extension Setup Wizard (Beta) <!-- omit in toc -->


This is an interactive tool to help users set up vscode-jest extension if the default configuration is not sufficient.

_(The setup wizard is released as a beta product in v4. Thank you for trying it out! 👍 Please don't hesitate to make suggestion or file issues so we can quickly improve and make it more useful for the community.)_


---

- [Overview](#overview)
- [How to run it](#how-to-run-it)
- [How does it work](#how-does-it-work)
- [Jest command line](#jest-command-line)
- [Debug Config](#debug-config)
- [FAQ](#faq)

---
## Overview

It helps users to set up the essential configurations of the extension via a simple UI. While the extension provides default configurations that work for the common standard environments, such as CRA and plain jest, the more sophisticated projects might need to customize the extension via a few simple settings that the wizard will help walking through this process.

The wizard asks questions and collects answers to update `settings.json` and `launch.json` in user's workspace ( [How does it work ?](#how-does-it-work)). It works for both single and multi-root workspaces. An OUTPUT channel `"vscode-jest Setup"` will be presented to show progress and tips through out the process.

Users can run the wizard any time ([How to run it ?](how-to-run-it)) and safely abort if desired.

The wizard tries its best to create accurate configurations but it will not be able to cover all the use cases out there. However, it always strikes for transparency, and shows where and what the configuration will be updated so users can easily modify it later manually if needed.

_(Note: the wizard is not to set up [jest](https://jestjs.io) itself. Actually, a working jest environment (such that you can run jest tests in terminal) is a prerequisite of running `vscode-jest` extension.)_
## How to run it
in v4.0, there are multiple ways you can activate the setup wizard

1. via [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette): one can launch the wizard on-demand via vscode command palette `"Jest: Setup Extension"`
2. via the setup button in various message panels, such as when jest process failed to start or when detecting missing debug config.

## How does it work
The wizard provides 2 main setup tasks:
1. [jest command line](#jest-command-line): used by the extension to kick off jest test run on behave of users.
2. [vscode debug config](#debug-config): to enable debugging individual jest test via the debug codeLens.

The wizard setup is workspace bound. For multi-root projects, user will be prompt to select a workspace folder to work with.

### Jest command line
The extension start jest tests process on behave of the users by issuing the same test command they otherwise run in terminal. Therefore it needs to know the command to start jest process. The wizard will examine the existing settings, looking for `"jest.jestCommandLine"` or the deprecated `"jest.pathToJest"` and `"jest.pathToConfig"`. If found, it will let users to [migrate](#note-1)/edit it; if not found, it will ask user to manually input the command line.

Please be aware that all relative paths in the settings are resolved against the `rootPath`, which by default is the current workspace folder unless you customize it with `"jest.rootPath"`.


While users can pass any jest CLI options in the `"jest.jestCommandLine"`, it is recommended NOT to pass the following:
- the watch options (--watch, --watchAll): the extension will append watch flag when appropriate.
- the coverage option (--coverage): user can easily toggle on/off coverage via command so no need to add it on the commandLine.

Because the extension appends additional options to this commandLine at run time, please make sure these additional options can be passed through, for example, if your command-line is `"npm test"`, make sure you add `"--"` at the end to pass through the additional options: `"npm test --"`

**Please note, `"jest.jestCommandLine"` setting is required to configure the debug config below.**

### Debug Config
When clicked on the debug codeLens, the extension will look for a debug config named `"vscode-jest-tests"`, and append the additional arguments (such as `--testNamePattern`) when launching the debugger.

If there is no existing `"jest.jestCommandLine"`, it will suggest to set one up before proceed, after all, a working jest environment is the prerequisite of this extension.

The wizard will examine the `launch.json` for existing config. If found, users can choose to use it as it is, replace (rename the old one and generate a new one) or manually editing it; if not found, wizard will try to [generate](#note-2) a new one.

The debug config is saved in `launch.json` in workspace folder and shown at the end of the setup for review or adjustment. If the user chooses to "replace" the existing config, the old configure will be renamed to `vscode-jest-tests-xxxxx` for reference purpose only, which can be safely deleted if not needed.

The generated config probably work fine for most projects, but could require further adjustment for projects including but not limited to the following:
- projects use different jest config between debug and regular test run
- projects with [platform specific properties](#note-3).

Check out [here](#note-4) if you are having problem running vscode-jest debug codeLens.
## FAQ

- <a id="note-1">**What does jestCommandLine migration process do?**</a>

The wizard will combine `jest.PathToJest` and `jest.pathToConfig` into a single command line `jest.jestCommandLine` , e.g.
```
//example 1
jest.PathToJest = 'yarn test'
jest.PathToConfig = '../../shared-jest.json'
==> jest.jestCommandLine = 'yarn test --config ../../shared-jest.json'
// example 2
jest.PathToJest = '../../node_modules/.bin/jest '
jest.PathToConfig = './jest-config.json'
==> jest.jestCommandLine = '../../node_modules/.bin/jest --config ./jest-config.json'
```

The `jest.jestCommandLine` setting will be saved in the workspace folder's local "settings.json" file.

- <a id="note-2">**How is debug config generated?**</a>
The process goes like this:
- parse `jest.jestCommandLine` into command and arguments. Resolve relative command path based on `jest.rootPath` if defined otherwise uses vscode variable `"${workspaceFolder}"`.
- obtain a debug template from `DebugConfigurationProvider`
- merge the jest command and arguments with the template.
- if the command is `npm` or `yarn` it updates `runtimeExecutable` property; otherwise updates `program`
- the arguments will be added to the `args` property, plus jest debug specific flags such as `--runInBand`.
- update `cwd` property with either `jest.rootPath` if defined, otherwise the vscode variable `"${workspaceFolder}"`


- <a id="note-3">**How is platform specific properties handled in DebugConfig generation?**</a>

Currently the wizard does not generate platform specific properties. Users will need to manually maintain them in `lauch.json` file.

For example, for a different jest command under windows, add the `windows.program` section in `launch.json`:

```
// launch.json
"configurations": [
...
{
type: "node",
name: "vscode-jest-tests",
...
program = "${workspaceFolder}/node_modules/.bin/jest",
windows = {
program: '${workspaceFolder}/node_modules/jest/bin/jest',
},
},
]
```
See more details and examples in [vscode: platform specific properties](https://code.visualstudio.com/docs/editor/debugging#_platformspecific-properties).


- <a id="note-4">**vscode-jest debug codeLens failed, now what?**</a>

If your regular jest run was fine but you can't debug the test with debug codeLens after running wizard. There should be some error message in your terminal that might help you pinpoint the culprit.

There are many information online about how to setup vscode debug config for specific environments/frameworks, you might find the following helpful:
- [vscode debug config properties](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-properties)
- [Launch configurations for common scenarios](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configurations-for-common-scenarios)
- [vscode-recipes for debug jest tests](https://github.com/microsoft/vscode-recipes/tree/master/debugging-jest-tests)

While you can manually correct the debug config and move on, if you think your use case is actually quite common, feel free to create a discussion/issue so we might be able to enhance the wizard or include your example config to help others.
2 changes: 1 addition & 1 deletion src/DebugCodeLens/DebugCodeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class DebugCodeLens extends vscode.CodeLens {
* @param fileName
* @param testIds test name/pattern.
* Because a test block can have multiple test results, such as for paramertized tests (i.e. test.each/describe.each), there could be multiple debuggable candidates, thus it takes multiple test identifiers.
* Noite: If a test id is a string array, it should represent the hierarchiical relationship of a test structure, such as [describe-id, test-id].
* Note: If a test id is a string array, it should represent the hierarchical relationship of a test structure, such as [describe-id, test-id].
*/
constructor(
document: vscode.TextDocument,
Expand Down
Loading

0 comments on commit adb9048

Please sign in to comment.