From a1aadd713c767b047ef46976bb690a57351322e6 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 19 Oct 2023 11:21:51 +0200 Subject: [PATCH] feat(vitest): run typecheck during tests (#4324) --- docs/advanced/api.md | 5 - docs/config/index.md | 20 ++- docs/guide/cli.md | 3 + docs/guide/testing-types.md | 10 +- packages/runner/src/utils/tasks.ts | 4 +- packages/ui/client/components/Suites.vue | 1 + packages/ui/client/components/TaskItem.vue | 1 + .../ui/client/components/views/ViewEditor.vue | 2 +- packages/ui/client/composables/summary.ts | 27 +++- packages/ui/package.json | 1 + packages/vitest/src/node/cli-api.ts | 9 ++ packages/vitest/src/node/cli.ts | 12 +- packages/vitest/src/node/config.ts | 9 +- packages/vitest/src/node/core.ts | 20 +-- packages/vitest/src/node/logger.ts | 19 ++- packages/vitest/src/node/pool.ts | 19 ++- packages/vitest/src/node/pools/typecheck.ts | 123 ++++++++++++++++++ packages/vitest/src/node/reporters/base.ts | 32 +++-- .../node/reporters/renderers/listRenderer.ts | 2 +- packages/vitest/src/node/stdin.ts | 4 - packages/vitest/src/node/workspace.ts | 75 ++--------- packages/vitest/src/runtime/execute.ts | 2 + packages/vitest/src/typecheck/collect.ts | 2 + packages/vitest/src/typecheck/typechecker.ts | 34 ++++- packages/vitest/src/types/config.ts | 14 +- packages/vitest/src/types/pool-options.ts | 2 +- pnpm-lock.yaml | 13 +- test/coverage-test/package.json | 2 +- test/typescript/package.json | 2 +- test/typescript/test/runner.test.ts | 7 +- test/typescript/test/vitest.custom.config.ts | 1 + test/typescript/test/vitest.empty.config.ts | 1 + 32 files changed, 330 insertions(+), 148 deletions(-) create mode 100644 packages/vitest/src/node/pools/typecheck.ts diff --git a/docs/advanced/api.md b/docs/advanced/api.md index fa4990699..e21fe2f0a 100644 --- a/docs/advanced/api.md +++ b/docs/advanced/api.md @@ -52,7 +52,6 @@ Vitest instance requires the current test mode. It can be either: - `test` when running runtime tests - `benchmark` when running benchmarks -- `typecheck` when running type tests ### mode @@ -64,10 +63,6 @@ Test mode will only call functions inside `test` or `it`, and throws an error wh Benchmark mode calls `bench` functions and throws an error, when it encounters `test` or `it`. This mode uses `benchmark.include` and `benchmark.exclude` options in the config to find benchmark files. -#### typecheck - -Typecheck mode doesn't _run_ tests. It only analyses types and gives a summary. This mode uses `typecheck.include` and `typecheck.exclude` options in the config to find files to analyze. - ### start You can start running tests or benchmarks with `start` method. You can pass an array of strings to filter test files. diff --git a/docs/config/index.md b/docs/config/index.md index 028b1d897..841e1e8b2 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -547,7 +547,7 @@ export default defineConfig({ ### poolMatchGlobs -- **Type:** `[string, 'threads' | 'forks' | 'vmThreads'][]` +- **Type:** `[string, 'threads' | 'forks' | 'vmThreads' | 'typescript'][]` - **Default:** `[]` - **Version:** Since Vitest 0.29.4 @@ -1637,6 +1637,24 @@ Changes the order in which setup files are executed. Options for configuring [typechecking](/guide/testing-types) test environment. +#### typecheck.enabled + +- **Type**: `boolean` +- **Default**: `false` +- **CLI**: `--typecheck`, `--typecheck.enabled` +- **Version**: Since Vitest 1.0.0-beta.3 + +Enable typechecking alongside your regular tests. + +#### typecheck.only + +- **Type**: `boolean` +- **Default**: `false` +- **CLI**: `--typecheck.only` +- **Version**: Since Vitest 1.0.0-beta.3 + +Run only typecheck tests, when typechecking is enabled. When using CLI, this option will automatically enable typechecking. + #### typecheck.checker - **Type**: `'tsc' | 'vue-tsc' | string` diff --git a/docs/guide/cli.md b/docs/guide/cli.md index 5d7b7317f..c4569ec8a 100644 --- a/docs/guide/cli.md +++ b/docs/guide/cli.md @@ -96,6 +96,9 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim | `--inspect-brk` | Enables Node.js inspector with break | | `--bail ` | Stop test execution when given number of tests have failed | | `--retry ` | Retry the test specific number of times if it fails | +| `--typecheck [options]` | Custom options for typecheck pool. If passed without options, enables typechecking | +| `--typecheck.enabled` | Enable typechecking alongside tests (default: `false`) | +| `--typecheck.only` | Run only typecheck tests. This automatically enables typecheck (default: `false`) | | `-h, --help` | Display available CLI options | ::: tip diff --git a/docs/guide/testing-types.md b/docs/guide/testing-types.md index 258e970ab..eb2f167db 100644 --- a/docs/guide/testing-types.md +++ b/docs/guide/testing-types.md @@ -66,12 +66,12 @@ assertType(answr) // ## Run typechecking -Add this command to your `scripts` section in `package.json`: +To enabled typechecking, just add `--typecheck` flag to your Vitest command in `package.json`: ```json { "scripts": { - "typecheck": "vitest typecheck" + "test": "vitest --typecheck" } } ``` @@ -80,13 +80,13 @@ Now you can run typecheck: ```sh # npm -npm run typecheck +npm run test # yarn -yarn typecheck +yarn test # pnpm -pnpm run typecheck +pnpm run test ``` Vitest uses `tsc --noEmit` or `vue-tsc --noEmit`, depending on your configuration, so you can remove these scripts from your pipeline. diff --git a/packages/runner/src/utils/tasks.ts b/packages/runner/src/utils/tasks.ts index 40244da59..d692a7995 100644 --- a/packages/runner/src/utils/tasks.ts +++ b/packages/runner/src/utils/tasks.ts @@ -7,8 +7,8 @@ function isAtomTest(s: Task): s is Test | Custom { export function getTests(suite: Arrayable): (Test | Custom)[] { const tests: (Test | Custom)[] = [] - const suite_arr = toArray(suite) - for (const s of suite_arr) { + const arraySuites = toArray(suite) + for (const s of arraySuites) { if (isAtomTest(s)) { tests.push(s) } diff --git a/packages/ui/client/components/Suites.vue b/packages/ui/client/components/Suites.vue index 492b10d3b..c9a1d8038 100644 --- a/packages/ui/client/components/Suites.vue +++ b/packages/ui/client/components/Suites.vue @@ -23,6 +23,7 @@ async function onRunCurrent() {