Skip to content

Commit 36bc699

Browse files
authored
chore: add typecheck and workspace examples (#5006)
1 parent 9c7c0fc commit 36bc699

20 files changed

+324
-67
lines changed

docs/guide/testing-types.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ title: Testing Types | Guide
44

55
# Testing Types
66

7+
::: tip Sample Project
8+
9+
[GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/typecheck) - [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/typecheck?initialPath=__vitest__/)
10+
11+
:::
12+
713
Vitest allows you to write tests for your types, using `expectTypeOf` or `assertType` syntaxes. By default all tests inside `*.test-d.ts` files are considered type tests, but you can change it with [`typecheck.include`](/config/#typecheck-include) config option.
814

915
Under the hood Vitest calls `tsc` or `vue-tsc`, depending on your config, and parses results. Vitest will also print out type errors in your source code, if it finds any. You can disable it with [`typecheck.ignoreSourceErrors`](/config/#typecheck-ignoresourceerrors) config option.

docs/guide/workspace.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ title: Workspace | Guide
44

55
# Workspace
66

7+
::: tip Sample Project
8+
9+
[GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/workspace) - [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/workspace?initialPath=__vitest__/)
10+
11+
:::
12+
713
Vitest provides built-in support for monorepos through a workspace configuration file. You can create a workspace to define your project's setups.
814

915
## Defining a Workspace

examples/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| `graphql` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/graphql) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/graphql?initialPath=__vitest__/) |
66
| `image-snapshot` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/image-snapshot) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/image-snapshot?initialPath=__vitest__/) |
77
| `lit` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/lit) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/lit?initialPath=__vitest__/) |
8+
| `marko` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/marko) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/marko?initialPath=__vitest__/) |
89
| `mocks` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/mocks) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/mocks?initialPath=__vitest__/) |
910
| `nextjs` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/nextjs) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/nextjs?initialPath=__vitest__/) |
1011
| `playwright` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/playwright) | |
@@ -18,7 +19,8 @@
1819
| `solid` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/solid) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/solid?initialPath=__vitest__/) |
1920
| `svelte` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/svelte) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/svelte?initialPath=__vitest__/) |
2021
| `sveltekit` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/sveltekit) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/sveltekit?initialPath=__vitest__/) |
21-
| `marko` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/marko) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/marko?initialPath=__vitest__/) |
22+
| `typecheck` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/typecheck) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/typecheck?initialPath=__vitest__/) |
2223
| `vitesse` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/vitesse) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/vitesse?initialPath=__vitest__/) |
2324
| `vue-jsx` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/vue-jsx) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/vue-jsx?initialPath=__vitest__/) |
2425
| `vue` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/vue) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/vue?initialPath=__vitest__/) |
26+
| `workspace` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/workspace) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/workspace?initialPath=__vitest__/) |

examples/typecheck/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@vitest/example-typecheck",
3+
"type": "module",
4+
"private": true,
5+
"license": "MIT",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "vitest",
9+
"test:ui": "vitest --ui",
10+
"test:run": "vitest run"
11+
},
12+
"devDependencies": {
13+
"@types/node": "^20.11.5",
14+
"@vitest/ui": "latest",
15+
"typescript": "^5.2.2",
16+
"vite": "latest",
17+
"vitest": "latest"
18+
},
19+
"stackblitz": {
20+
"startCommand": "npm run test:ui"
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
3+
test('normal', () => {
4+
expect(1 + 1).toBe(2)
5+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, expectTypeOf, test } from 'vitest'
2+
3+
test('type', () => {
4+
expectTypeOf(1).toEqualTypeOf(2)
5+
expect(1).toBe(2) // not executed
6+
})

examples/typecheck/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2020",
4+
"module": "ESNext",
5+
"moduleResolution": "Bundler",
6+
"strict": true,
7+
"declaration": true,
8+
"declarationMap": true,
9+
"sourceMap": true,
10+
"verbatimModuleSyntax": true
11+
},
12+
"include": ["src", "test"],
13+
"exclude": ["node_modules"]
14+
}

examples/typecheck/vite.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference types="vitest" />
2+
3+
// Configure Vitest (https://vitest.dev/config/)
4+
5+
import { defineConfig } from 'vite'
6+
7+
export default defineConfig({
8+
test: {
9+
/* for example, use global to avoid globals imports (describe, test, expect): */
10+
// globals: true,
11+
typecheck: {
12+
enabled: true,
13+
},
14+
},
15+
})

examples/workspace/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@vitest/example-workspace",
3+
"type": "module",
4+
"private": true,
5+
"license": "MIT",
6+
"scripts": {
7+
"test": "vitest",
8+
"test:ui": "vitest --ui",
9+
"test:run": "vitest run"
10+
},
11+
"devDependencies": {
12+
"@vitest/ui": "latest",
13+
"vite": "latest",
14+
"vitest": "latest"
15+
},
16+
"stackblitz": {
17+
"startCommand": "npm run test:ui"
18+
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "@vitest/example-workspace-lib1",
3+
"type": "module",
4+
"private": true,
5+
"license": "MIT"
6+
}

0 commit comments

Comments
 (0)