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

chore: add typecheck and workspace examples #5006

Merged
merged 9 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
12 changes: 12 additions & 0 deletions docs/guide/testing-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ 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__/)

:::

::: tip

You can find a sample project in [Examples](/guide/#examples).

:::

sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 12 additions & 0 deletions docs/guide/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ 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__/)

:::

::: tip

You can find a sample project in [Examples](/guide/#examples).

:::

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
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
| `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__/) |
| `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__/) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you use a generator for that from scripts folder? I think generator sorts it by name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I didn't know there is pnpm docs:examples script. It looks like the last "marko" one was also added manually e75dabb

| `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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineProject } from 'vitest/config'

export default defineProject({
test: {},
})
13 changes: 13 additions & 0 deletions examples/workspace/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineWorkspace } from 'vitest/config'

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