Skip to content

Commit

Permalink
Migrate test framework to vitest (#2769)
Browse files Browse the repository at this point in the history
Get rid of mocha and upgrade to vitest which is a more modern
alternative providing, watch, direct typescript compilation out of the
box, expect library and more.

Advantage over mocha:
- Much better cli
  -  watch mode
  - better diff
- Better extension:
  -  tree organization for files too (not everything flattened)
- update in real time the test(no more need to refresh manually to
discover where are the tests)
  - just a little buggy
- Compiles typescript directly
- provides more expectation apis(like jest)

Cons over mocha: 
- Slower(about 2x) but that means we don't need to build the test as
part of build which would speed up that part(not as much as is lost)

Todo: 
- typespec-azure migration
  • Loading branch information
timotheeguerin authored Jan 2, 2024
1 parent da99aa9 commit 9c7bf80
Show file tree
Hide file tree
Showing 317 changed files with 3,904 additions and 4,466 deletions.
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"streetsidesoftware.code-spell-checker"
"streetsidesoftware.code-spell-checker",
"ZixuanChen.vitest-explorer"
]
}
20 changes: 0 additions & 20 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,6 @@
"order": 999
}
},
{
"type": "node",
"request": "attach",
"name": "Tests",
"port": 9229,
"smartStep": true,
"sourceMaps": true,
"continueOnAttach": true,
"skipFiles": [
"<node_internals>/**/*.js",
"**/.vscode/extensions/hbenl.vscode-mocha-test-adapter-*/**.js"
],
"outFiles": [
"${workspaceFolder}/packages/*/dist/**/*.js",
"${workspaceFolder}/packages/*/dist-dev/**/*.js"
],
"presentation": {
"hidden": true
}
},
{
"name": "Attach to Language Server",
"type": "node",
Expand Down
19 changes: 2 additions & 17 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,8 @@
},
"typescript.tsdk": "./packages/compiler/node_modules/typescript/lib",
"git.ignoreLimitWarning": true,
"testExplorer.useNativeTesting": true,
"mochaExplorer.parallel": false,
"mochaExplorer.files": [
"./packages/*/dist/test/**/*.test.js",
"./packages/*/dist/test/**/*.e2e.js",
"./packages/*/dist-dev/test/**/*.test.js",
"./packages/*/test/**/*.test.js"
],
"mochaExplorer.ignore": "./packages/*/dist/test/manual/**/*.js",
"mochaExplorer.mochaPath": "./packages/compiler/node_modules/mocha",
"mochaExplorer.timeout": 500000,
"mochaExplorer.require": "source-map-support/register",
"mochaExplorer.debuggerConfig": "Tests",
"mochaExplorer.env": {
"TYPESPEC_VERBOSE_TEST_OUTPUT": "true",
"NODE_OPTIONS": "--stack-trace-limit=50"
},
"vitest.enable": true,
"vitest.commandLine": "node ./packages/compiler/node_modules/vitest/vitest.mjs",
"prettier.prettierPath": "./packages/compiler/node_modules/prettier/index.cjs",
"prettier.documentSelectors": ["**/*.tsp"],
"testExplorer.errorDecoration": false,
Expand Down
22 changes: 9 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ PR validation will ensure that reference docs are up to date.

## Recommended extensions

1. [Mocha Test Explorer](https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-mocha-test-adapter):
Run tests from the IDE.
1. [Vitest Test Explorer](https://marketplace.visualstudio.com/items?itemName=ZixuanChen.vitest-explorer):
Run tests from the IDE. (Version `0.2.43` is bugged on OSX, use `0.2.42` instead)
2. [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode):
Automatically keep code formatted correctly on save.
3. [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint):
Expand Down Expand Up @@ -191,17 +191,13 @@ Terminal pane will have three parallel watch tasks running:

## Testing

With [Mocha Test
Explorer](https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-mocha-test-adapter)
installed, click on its icon in the sidebar, then click on the play
button at the top or on any individual test or test group to run just
one test or just one group. You can also click on the bug icon next to
an individual test to debug it.

You can see additional information logged by each test using
`logVerboseTestOutput` by clicking on the test and looking at the
output pane. Unlike the command line, no environment variable is
needed.
```bash
# Run all the tests
rush test

# Run in a specific package tests in watch mode
npm run test:watch
```

## Debugging

Expand Down
10 changes: 10 additions & 0 deletions common/changes/@typespec/bundler/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/bundler",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/bundler"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/compiler/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/compiler",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/compiler"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/eslint-config-typespec",
"comment": "Migrate test rules to vitest",
"type": "none"
}
],
"packageName": "@typespec/eslint-config-typespec"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/eslint-plugin",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/eslint-plugin"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/html-program-viewer",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/html-program-viewer"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/http/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/http",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/http"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/internal-build-utils",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/internal-build-utils"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/json-schema/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/json-schema",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/json-schema"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/library-linter",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/library-linter"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/openapi/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/openapi",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/openapi"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/openapi3/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/openapi3",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/openapi3"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/playground/vitest_2023-12-27-18-29.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/playground",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/playground"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/prettier-plugin-typespec",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/prettier-plugin-typespec"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/protobuf/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/protobuf",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/protobuf"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/rest/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/rest",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/rest"
}
10 changes: 10 additions & 0 deletions common/changes/@typespec/versioning/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/versioning",
"comment": "",
"type": "none"
}
],
"packageName": "@typespec/versioning"
}
10 changes: 10 additions & 0 deletions common/changes/tmlanguage-generator/vitest_2024-01-02-16-19.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "tmlanguage-generator",
"comment": "",
"type": "none"
}
],
"packageName": "tmlanguage-generator"
}
10 changes: 10 additions & 0 deletions common/changes/typespec-vscode/vitest_2023-12-22-02-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "typespec-vscode",
"comment": "",
"type": "none"
}
],
"packageName": "typespec-vscode"
}
Loading

0 comments on commit 9c7bf80

Please sign in to comment.