Skip to content

Commit

Permalink
chore: add typecheck and workspace examples (#5006)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jan 20, 2024
1 parent 9c7c0fc commit 36bc699
Show file tree
Hide file tree
Showing 20 changed files with 324 additions and 67 deletions.
6 changes: 6 additions & 0 deletions docs/guide/testing-types.md
Expand Up @@ -4,6 +4,12 @@ title: Testing Types | Guide

# Testing Types

::: tip Sample Project

[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__/)

:::

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.

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.
Expand Down
6 changes: 6 additions & 0 deletions docs/guide/workspace.md
Expand Up @@ -4,6 +4,12 @@ title: Workspace | Guide

# Workspace

::: tip Sample Project

[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__/)

:::

Vitest provides built-in support for monorepos through a workspace configuration file. You can create a workspace to define your project's setups.

## Defining a Workspace
Expand Down
4 changes: 3 additions & 1 deletion examples/README.md
Expand Up @@ -5,6 +5,7 @@
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
| `playwright` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/playwright) | |
Expand All @@ -18,7 +19,8 @@
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
| `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__/) |
22 changes: 22 additions & 0 deletions examples/typecheck/package.json
@@ -0,0 +1,22 @@
{
"name": "@vitest/example-typecheck",
"type": "module",
"private": true,
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "vitest",
"test:ui": "vitest --ui",
"test:run": "vitest run"
},
"devDependencies": {
"@types/node": "^20.11.5",
"@vitest/ui": "latest",
"typescript": "^5.2.2",
"vite": "latest",
"vitest": "latest"
},
"stackblitz": {
"startCommand": "npm run test:ui"
}
}
5 changes: 5 additions & 0 deletions examples/typecheck/test/normal.test.ts
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest'

test('normal', () => {
expect(1 + 1).toBe(2)
})
6 changes: 6 additions & 0 deletions examples/typecheck/test/type.test-d.ts
@@ -0,0 +1,6 @@
import { expect, expectTypeOf, test } from 'vitest'

test('type', () => {
expectTypeOf(1).toEqualTypeOf(2)
expect(1).toBe(2) // not executed
})
14 changes: 14 additions & 0 deletions examples/typecheck/tsconfig.json
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2020",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"verbatimModuleSyntax": true
},
"include": ["src", "test"],
"exclude": ["node_modules"]
}
15 changes: 15 additions & 0 deletions examples/typecheck/vite.config.ts
@@ -0,0 +1,15 @@
/// <reference types="vitest" />

// Configure Vitest (https://vitest.dev/config/)

import { defineConfig } from 'vite'

export default defineConfig({
test: {
/* for example, use global to avoid globals imports (describe, test, expect): */
// globals: true,
typecheck: {
enabled: true,
},
},
})
19 changes: 19 additions & 0 deletions examples/workspace/package.json
@@ -0,0 +1,19 @@
{
"name": "@vitest/example-workspace",
"type": "module",
"private": true,
"license": "MIT",
"scripts": {
"test": "vitest",
"test:ui": "vitest --ui",
"test:run": "vitest run"
},
"devDependencies": {
"@vitest/ui": "latest",
"vite": "latest",
"vitest": "latest"
},
"stackblitz": {
"startCommand": "npm run test:ui"
}
}
6 changes: 6 additions & 0 deletions examples/workspace/packages/lib1/package.json
@@ -0,0 +1,6 @@
{
"name": "@vitest/example-workspace-lib1",
"type": "module",
"private": true,
"license": "MIT"
}
5 changes: 5 additions & 0 deletions examples/workspace/packages/lib1/test/double.test.ts
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest'

test('double', () => {
expect(2 * 2).toBe(4)
})
14 changes: 14 additions & 0 deletions examples/workspace/packages/lib1/tsconfig.json
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2020",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"verbatimModuleSyntax": true
},
"include": ["src", "test"],
"exclude": ["node_modules"]
}
5 changes: 5 additions & 0 deletions examples/workspace/packages/lib1/vite.config.ts
@@ -0,0 +1,5 @@
import { defineProject } from 'vitest/config'

export default defineProject({
test: {},
})
6 changes: 6 additions & 0 deletions examples/workspace/packages/lib2/package.json
@@ -0,0 +1,6 @@
{
"name": "@vitest/example-workspace-lib2",
"type": "module",
"private": true,
"license": "MIT"
}
5 changes: 5 additions & 0 deletions examples/workspace/packages/lib2/test/square.test.ts
@@ -0,0 +1,5 @@
import { expect, test } from 'vitest'

test('square', () => {
expect(2 ** 2).toBe(4)
})
14 changes: 14 additions & 0 deletions examples/workspace/packages/lib2/tsconfig.json
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2020",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"verbatimModuleSyntax": true
},
"include": ["src", "test"],
"exclude": ["node_modules"]
}
5 changes: 5 additions & 0 deletions examples/workspace/packages/lib2/vite.config.ts
@@ -0,0 +1,5 @@
import { defineProject } from 'vitest/config'

export default defineProject({
test: {},
})
13 changes: 13 additions & 0 deletions examples/workspace/tsconfig.json
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es2020",
"module": "node16",
"strict": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"verbatimModuleSyntax": true
},
"include": ["src", "test"],
"exclude": ["node_modules"]
}
5 changes: 5 additions & 0 deletions examples/workspace/vitest.workspace.ts
@@ -0,0 +1,5 @@
import { defineWorkspace } from 'vitest/config'

export default defineWorkspace([
'packages/*',
])

0 comments on commit 36bc699

Please sign in to comment.