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

feat: Implement standalone mocha runner and loader #1

Merged
merged 28 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
af988dd
refactor: Rebrand project for Mocha
Danielku15 Mar 24, 2024
0733d11
feat: Implement standalone mocha runner and loader
Danielku15 Mar 24, 2024
93fd534
chore: Extend tests
Danielku15 Mar 24, 2024
738e0b6
Merge branch 'main' into feature/mocha-standalone
Danielku15 Mar 31, 2024
088cfdc
Merge branch 'main' into feature/mocha-standalone
Danielku15 Mar 31, 2024
619f562
build: add test reports
Danielku15 Mar 31, 2024
1e2d0b4
build: Fix vscode DTS
Danielku15 Mar 31, 2024
372eb17
build: don't fail fast
Danielku15 Mar 31, 2024
6eeefd1
build: increase timeout of tests
Danielku15 Mar 31, 2024
105289c
build: Add test config printing
Danielku15 Mar 31, 2024
33f0174
build: explicit compile call
Danielku15 Mar 31, 2024
5ecc515
build: test output to check for files
Danielku15 Mar 31, 2024
c32cb75
build: fix improt
Danielku15 Mar 31, 2024
39d8693
build: Remove artifact name
Danielku15 Mar 31, 2024
0d7ac25
build: Permissions and matrix handling
Danielku15 Mar 31, 2024
a36d8e2
build: Align formatting of test results
Danielku15 Mar 31, 2024
5390451
chore: Cleanup and todo resolving
Danielku15 Mar 31, 2024
8d265a1
chore: some more logs
Danielku15 Mar 31, 2024
c3d6e1d
fix: Line numbers on typescript
Danielku15 Mar 31, 2024
8753898
chore: Use mocharc as root note text for clarity
Danielku15 Mar 31, 2024
f4cb19f
fix: zero-based columns
Danielku15 Mar 31, 2024
0aa66d7
fix: handle inconsistencies in source-map lib
Danielku15 Mar 31, 2024
9c25be6
fix: clear out test inconsistencies
Danielku15 Mar 31, 2024
c063976
fix: handle source mapped javascript
Danielku15 Mar 31, 2024
602308d
feat: add support for ignore patterns
Danielku15 Mar 31, 2024
93d84ec
Remove OpenJS foundation for now.
Danielku15 Apr 2, 2024
3f1ee6a
chore: Add note about extension to readme
Danielku15 Apr 2, 2024
5828c93
chore: add link to project.
Danielku15 Apr 2, 2024
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: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*]
insert_final_newline = true
indent_style = space
indent_size = 2
5 changes: 3 additions & 2 deletions .esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ const watch = process.argv.includes("--watch");
const minify = watch ? process.argv.includes("--minify") : !process.argv.includes("--no-minify");

const ctx = esbuild.context({
entryPoints: ["src/extension.ts"],
entryPoints: ["src/extension.ts", "src/reporter/fullJsonStreamReporter.ts"],
tsconfig: "./tsconfig.json",
bundle: true,
external: ["vscode", "pnpapi"],
external: ["vscode", "pnpapi", "mocha", "esbuild"],
sourcemap: !minify,
minify,
platform: "node",
outdir: "out",
keepNames: !minify
});

ctx
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ on:
pull_request:
workflow_dispatch:

permissions:
contents: read
actions: read
checks: write

jobs:
build:
name: Build and Test
strategy:
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
vscode-version: ['stable', 'insiders']
vscode-platform: ['desktop']
runs-on: ${{ matrix.os }}
env:
VSCODE_TEST_VERSION: ${{matrix.vscode-version}}
VSCODE_TEST_PLATFORM: ${{matrix.vscode-platform}}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -22,7 +31,18 @@ jobs:
with:
node-version: lts/*
- run: npm install
- run: npm run compile:test
- name: Print Test Config
run: node ./.vscode-test.mjs
env:
VSCODE_CONFIG_LOG: true
- run: xvfb-run -a npm test
if: runner.os == 'Linux'
- run: npm test
if: runner.os != 'Linux'
- uses: dorny/test-reporter@v1
if: always()
with:
name: VS Code Test Results (${{matrix.os}}, ${{matrix.vscode-version}}, ${{matrix.vscode-platform}})
path: 'test-results/*.json'
reporter: mocha-json
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
test-results/*.json

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
Expand Down Expand Up @@ -121,6 +122,7 @@ dist

# Stores VSCode versions used for testing VSCode extensions
.vscode-test
.working-copy

# yarn v2
.yarn/cache
Expand Down
19 changes: 19 additions & 0 deletions .vscode-ci-test-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const BaseReporter = require('mocha/lib/reporters/base');
const SpecReporter = require('mocha/lib/reporters/spec');
const JsonReporter = require('mocha/lib/reporters/json');

module.exports = class MultiReporter extends BaseReporter {
reporters;

constructor(runner, options) {
super(runner, options);
this.reporters = [
new SpecReporter(runner, {
reporterOption: options.reporterOption.specReporterOption,
}),
new JsonReporter(runner, {
reporterOption: options.reporterOption.jsonReporterOption,
}),
];
}
};
68 changes: 55 additions & 13 deletions .vscode-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,68 @@ import * as path from 'path';
import { fileURLToPath } from 'url';

const dirname = fileURLToPath(new URL('.', import.meta.url));
const testCaseRunnerDir = path.join(dirname, 'out/test/testCases');
const integrationTestDir = path.join(dirname, 'out/test/integration');
const workspaceBaseDir = path.join(dirname, 'test-workspaces');

// @ts-check
const vsCodeVersion = process.env.VSCODE_TEST_VERSION ?? 'stable';
const vsCodePlatform = process.env.VSCODE_TEST_PLATFORM ?? 'desktop';

export default defineConfig([
let createCommonOptions = (label) => {
if (process.env.GITHUB_ACTIONS) {
return {
platform: vsCodePlatform,
version: vsCodeVersion,
env: {
MOCHA_COLORS: 'true',
},
mocha: {
ui: 'bdd',

reporter: path.join(dirname, '.vscode-ci-test-reporter.js'),
reporterOption: {
jsonReporterOption: {
output: path.join(dirname, 'test-results', `${label}.json`),
},
},
timeout: 60_000,
},
};
} else {
return {
platform: vsCodePlatform,
version: vsCodeVersion,

mocha: {
ui: 'bdd',
timeout: 60_000,
},
};
}
};

const config = [
{
label: 'core',
files: 'out/**/*.test.js',
mocha: { ui: 'bdd' },
label: 'unit',
files: 'out/test/unit/**/*.test.js',
...createCommonOptions('unit'),
},
...fs
.readdirSync(testCaseRunnerDir)
.filter((f) => f.endsWith('.js'))
.readdirSync(integrationTestDir)
.filter((f) => f.endsWith('.test.js'))
.map((file) => {
const label = path.basename(file, '.js');
const label = path.basename(file, '.test.js');
return {
label,
files: path.join(testCaseRunnerDir, file),
workspaceFolder: path.join(dirname, `testCases/${label}`),
mocha: { ui: 'bdd', timeout: 60_000 },
files: path.join(integrationTestDir, file),
workspaceFolder: path.join(workspaceBaseDir, label),
...createCommonOptions(label),
};
}),
]);
];

if (process.env.VSCODE_CONFIG_LOG) {
const util = await import('util');
console.log(util.inspect(config, { showHidden: false, depth: null, colors: true }));
}

export default defineConfig(config);
12 changes: 10 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"typescript.tsc.autoDetect": "off",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
"source.organizeImports": "never",
"source.sortMembers": "explicit"
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[github-actions-workflow]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
}
5 changes: 5 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ out/test/**
testCases/**
.esbuild.js
.nvmrc


node_modules/**
!node_modules/esbuild/**/*
!package.json
9 changes: 0 additions & 9 deletions CODE_OF_CONDUCT.md

This file was deleted.

27 changes: 27 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Contributing Guide

This project was scaffolded with the Yeoman and VS Code Extension generator.
https://code.visualstudio.com/api/get-started/your-first-extension


## Get up and running straight away

* Press `F5` to open a new window with the extension loaded.
* Set breakpoints in the code inside `src/extension.ts` to debug the extension.
* Find output from the extension in the debug console.


## Make changes

* You can relaunch the extension from the debug toolbar after changing code in `src/extension.ts`.
* You can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with the extension to load your changes.

## Run tests (of this extension)

* Install the [Extension Test Runner](https://marketplace.visualstudio.com/items?itemName=ms-vscode.extension-test-runner)
* Run the "watch" task via the **Tasks: Run Task** command. Make sure this is running, or tests might not be discovered.
* Open the Testing view from the activity bar and click the Run Test" button, or use the hotkey `Ctrl/Cmd + ; A`
* See the output of the test result in the Test Results view.
* Make changes to `src/test/extension.test.ts` or create new test files inside the `test` folder.
* The provided test runner will only consider files matching the name pattern `**.test.ts`.
* You can create folders inside the `test` folder to structure your tests any way you want.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
MIT License

Copyright (c) Daniel Kuschny (Danielku15) and contributors.
Copyright (c) Microsoft Corporation.

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
53 changes: 24 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
# VS Code Extension Test Runner
# Mocha VS Code Extension

This is a VS Code extension for other extension authors, that runs tests as you develop extensions. It requires use of the new extension test API and configuration file. For more information, see our [testing guide for extension authors](https://code.visualstudio.com/api/working-with-extensions/testing-extension).
This is the Mocha extension for VS Code enabling developers to run and debug tests right within VS Code using the built-in test explorer.

> [!NOTE]
> This extension is in a fairly early development stage but mostly functional. We soon
> will start to publish some pre-release versions to the VS Code Extension gallery.
>
> Follow our progress at https://github.com/orgs/CoderLine/projects/15/views/1
>
> Please provide feedback and discuss improvements over at https://github.com/CoderLine/mocha-vscode/discussions

## Credits

This project started as a fork of the `Extension Test Runner` and `Command-line runner for VS Code tests` developed by Microsoft and then was adapted to work with Mocha directly.
The main credits of this extension go over to the folks at Microsoft (and their contributors) and without them it would have been a lot more effort to ship a Mocha test runner for VS Code.

- https://marketplace.visualstudio.com/items?itemName=ms-vscode.extension-test-runner
- https://github.com/microsoft/vscode-extension-test-runner
- https://github.com/microsoft/vscode-test-cl

## Getting Started

Please follow the [testing guide for extension authors](https://code.visualstudio.com/api/working-with-extensions/testing-extension) to initially set up tests using the command line. Then, [install this extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.extension-test-runner).
Please follow the [general Mocha documentation](https://mochajs.org/) to initially set up tests using the command line. Then, [install this extension](https://marketplace.visualstudio.com/items?itemName=mocha.mocha-vscode).

This extension automatically discovers and works with the `.vscode-test.js/mjs/cjs` files found in your workspace. It requires minimal to no extra configuration. It works by looking at test files in your JavaScript code. If you write tests in TypeScript, you will want to:
This extension automatically discovers and works with the `.mocharc.js/cjs/yaml/yml/json/jsonc` files found in your workspace. It requires minimal to no extra configuration. It works by looking at test files in your JavaScript code. If you write tests in TypeScript, you will want to:

1. Modify your tsconfig.json and add `"sourceMap": true`
1. Add `**/*.js.map` to your `.vscodeignore` file to avoid bloating the published extension.

## Configuration

- `extension-test-runner.extractSettings`: configures how tests get extracted. You can configure:
- `mocha-vscode.extractSettings`: configures how tests get extracted. You can configure:

- The `extractWith` mode, that specifies if tests are extracted via evaluation or syntax-tree parsing. Evaluation is likely to lead to better results, but may have side-effects. Defaults to `evaluation`.
- The `extractTimeout` limiting how long the extraction of tests for a single file is allowed to take.
- The `test` and `suite` identifiers the process extracts. Defaults to `["it", "test"]` and `["describe", "suite"]` respectively, covering Mocha's common interfaces.

- `extension-test-runner.debugOptions`: options, normally found in the launch.json, to pass when debugging the extension. See [the docs](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes) for a complete list of options.

## Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

## Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
trademarks or logos is subject to and must follow
[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
Any use of third-party trademarks or logos are subject to those third-party's policies.
- `mocha-vscode.debugOptions`: options, normally found in the launch.json, to pass when debugging the extension. See [the docs](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes) for a complete list of options.
41 changes: 0 additions & 41 deletions SECURITY.md

This file was deleted.

25 changes: 0 additions & 25 deletions SUPPORT.md

This file was deleted.

Binary file modified icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading