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

Add jest-runner #59

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: node_js
os:
- osx
- linux
node_js: 8
node_js: 10

install:
- |
Expand All @@ -13,8 +13,8 @@ install:

script:
- |
echo ">>> Compile vscode-test"
yarn && yarn compile
echo ">>> Compile vscode-test and run tests"
yarn && yarn compile && yarn test --ci
echo ">>> Compiled vscode-test"
cd sample
echo ">>> Run sample integration test"
Expand Down
80 changes: 51 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,32 @@

This module helps you test VS Code extensions.

Supported:
## Table of contents

- Node >= 8.x
- [Installation](#installation)
- [Supported](#supported)
- [Usage](#usage)
- [`jest-runner` usage](https://github.com/microsoft/vscode-test/blob/master/jest-runner)
- [License](#license)
- [Contributing](#contributing)

## Installation

### npm

```sh
npm install vscode-test --save-dev
```

### yarn

```sh
yarn add vscode-test jest --dev
```

## Supported

- Node >= 10.x
- Windows >= Windows Server 2012+ / Win10+ (anything with Powershell >= 5.0)
- macOS
- Linux
Expand All @@ -27,28 +50,28 @@ See [./sample](./sample) for a runnable sample, with [Azure DevOps Pipelines](ht
```ts
async function go() {
try {
const extensionDevelopmentPath = path.resolve(__dirname, '../../../')
const extensionTestsPath = path.resolve(__dirname, './suite')
const extensionDevelopmentPath = path.resolve(__dirname, '../../../');
const extensionTestsPath = path.resolve(__dirname, './suite');

/**
* Basic usage
*/
await runTests({
extensionDevelopmentPath,
extensionTestsPath
})
extensionTestsPath,
});

const extensionTestsPath2 = path.resolve(__dirname, './suite2')
const testWorkspace = path.resolve(__dirname, '../../../test-fixtures/fixture1')
const extensionTestsPath2 = path.resolve(__dirname, './suite2');
const testWorkspace = path.resolve(__dirname, '../../../test-fixtures/fixture1');

/**
* Running another test suite on a specific workspace
*/
await runTests({
extensionDevelopmentPath,
extensionTestsPath: extensionTestsPath2,
launchArgs: [testWorkspace]
})
launchArgs: [testWorkspace],
});

/**
* Use 1.36.1 release for testing
Expand All @@ -57,8 +80,8 @@ async function go() {
version: '1.36.1',
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [testWorkspace]
})
launchArgs: [testWorkspace],
});

/**
* Use Insiders release for testing
Expand All @@ -67,33 +90,33 @@ async function go() {
version: 'insiders',
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [testWorkspace]
})
launchArgs: [testWorkspace],
});

/**
* Noop, since 1.36.1 already downloaded to .vscode-test/vscode-1.36.1
*/
await downloadAndUnzipVSCode('1.36.1')
await downloadAndUnzipVSCode('1.36.1');

/**
* Manually download VS Code 1.35.0 release for testing.
*/
const vscodeExecutablePath = await downloadAndUnzipVSCode('1.35.0')
const vscodeExecutablePath = await downloadAndUnzipVSCode('1.35.0');
await runTests({
vscodeExecutablePath,
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [testWorkspace]
})
launchArgs: [testWorkspace],
});

/**
* Install Python extension
*/
const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath)
const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath);
cp.spawnSync(cliPath, ['--install-extension', 'ms-python.python'], {
encoding: 'utf-8',
stdio: 'inherit'
})
stdio: 'inherit',
});

/**
* - Add additional launch flags for VS Code
Expand All @@ -106,11 +129,11 @@ async function go() {
launchArgs: [
testWorkspace,
// This disables all extensions except the one being testing
'--disable-extensions'
'--disable-extensions',
],
// Custom environment variables for extension test script
extensionTestsEnv: { foo: 'bar' }
})
extensionTestsEnv: { foo: 'bar' },
});

/**
* Use win64 instead of win32 for testing Windows
Expand All @@ -120,17 +143,16 @@ async function go() {
extensionDevelopmentPath,
extensionTestsPath,
version: '1.40.0',
platform: 'win32-x64-archive'
platform: 'win32-x64-archive',
});
}

} catch (err) {
console.error('Failed to run tests')
process.exit(1)
console.error('Failed to run tests');
process.exit(1);
}
}

go()
go();
```

## License
Expand Down
209 changes: 209 additions & 0 deletions jest-runner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<p>
<h1 align="center">vscode-test-jest-runner</h1>
</p>

Run VS Code tests using Jest Testing Framework

## Table of contents

- [Installation](#installation)
- [Setup](#setup)
- [Environment variables](#environment-variables)

## Installation

### npm

```sh
npm install vscode-test jest --save-dev
```

### yarn

```sh
yarn add vscode-test jest --dev
```

## Setup

### Example `launch.json`

```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Jest: Extension Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/node_modules/vscode-test/out/jest-runner"
],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: compile",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"JEST_RUNNER_TEST_REGEX": "",
"JEST_RUNNER_UPDATE_SNAPSHOTS": "false"
}
},
{
"name": "Jest: Current Test File",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/node_modules/vscode-test/out/jest-runner"
],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: compile",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"JEST_RUNNER_TEST_REGEX": "${file}",
"JEST_RUNNER_UPDATE_SNAPSHOTS": "false"
}
},
{
"name": "Jest: Update All Snapshots",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/node_modules/vscode-test/out/jest-runner"
],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: compile",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"JEST_RUNNER_TEST_REGEX": "",
"JEST_RUNNER_UPDATE_SNAPSHOTS": "true"
}
},
{
"name": "Jest: Update Snapshots in Current Test File",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/node_modules/vscode-test/out/jest-runner"
],
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: compile",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"JEST_RUNNER_TEST_REGEX": "${file}",
"JEST_RUNNER_UPDATE_SNAPSHOTS": "true"
}
}
]
}
```

## Environment variables

### `JEST_RUNNER_TEST_REGEX`

The pattern Jest uses to detect test files.

> **Example `env` settings:**
>
> ```json
> "env": {
> "JEST_RUNNER_TEST_REGEX": "${file}",
> }
> ```

### `JEST_RUNNER_UPDATE_SNAPSHOTS`

Use this to re-record every snapshot that fails during this test run. Can be used together with `JEST_RUNNER_TEST_REGEX` to re-record snapshots.

> **Example `env` settings:**
>
> ```json
> "env": {
> "JEST_RUNNER_UPDATE_SNAPSHOTS": "true",
> }
> ```

### `JEST_RUNNER_SETUP`

The path to a module that runs some code to configure or set up the testing framework before each test. You can use this to mock VS Code APIs, such as forcing the `getConfiguration` API to use an in-memory cache vs. interacting with the file system (see `jest-runner-setup.ts` example below).

> **Example `env` settings:**
>
> ```json
> "env": {
> "JEST_RUNNER_SETUP": "${workspaceFolder}/dist/test-utils/jest-runner-setup.js",
> }
> ```
>
> **`jest-runner-setup.ts`**
>
> ```ts
> process.env.NODE_ENV = 'test';
>
> jest.mock(
> 'vscode',
> () => {
> const { vscode } = global as any;
>
> vscode.workspace.getConfiguration = (section: string) => {
> let config: WorkspaceConfig = { ...workspaceConfig };
> for (const sectionKey of section.split('.')) {
> config = config[sectionKey];
> }
>
> function get<T>(section: string, defaultValue: T): T;
> function get<T>(section: string): T | undefined {
> return config[section];
> }
>
> return {
> ...config,
> get,
> update: update(section),
> };
> };
>
> return vscode;
> },
> { virtual: true }
> );
>
> interface WorkspaceConfig {
> [name: string]: any;
> }
>
> let workspaceConfig = defaultConfig();
> afterEach(() => {
> workspaceConfig = defaultConfig();
> });
>
> function update(rootSection: string): (section: string, value: any) => void {
> return (section: string, value: any) => {
> let config: any = workspaceConfig;
> for (const sectionKey of rootSection.split('.')) {
> config = config[sectionKey];
> }
>
> config[section] = value;
> };
> }
>
> function defaultConfig(): WorkspaceConfig {
> return {
> workbench: {
> colorTheme: 'Default Dark+',
> },
> };
> }
> ```
Loading