Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 13, 2024
1 parent 6173593 commit db49551
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
8 changes: 7 additions & 1 deletion docs/guide/testing-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ Vitest allows you to write tests for your types, using `expectTypeOf` or `assert

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.

Keep in mind that Vitest doesn't run or compile these files, they are only statically analyzed by the compiler, and because of that you cannot use any dynamic statements. Meaning, you cannot use dynamic test names, and `test.each`, `test.runIf`, `test.skipIf`, `test.concurrent` APIs. But you can use other APIs, like `test`, `describe`, `.only`, `.skip` and `.todo`.
Keep in mind that Vitest doesn't run these files, they are only statically analyzed by the compiler. Meaning, that if you use a dynamic name or `test.each` or `test.for`, the test name will not be evaluated - it will be displayed as is.

::: warning
Before Vitest 2.1, your `typecheck.include` overrode the `include` pattern, so your runtime tests did not actually run; they were only type-checked.

Since Vitest 2.1, if your `include` and `typecheck.include` overlap, Vitest will report type tests and runtime tests as separate entries.
:::

Using CLI flags, like `--allowOnly` and `-t` are also supported for type checking.

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/typecheck/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function collectTests(
const property = callee?.property?.name
const mode = !property || property === name ? 'run' : property
// the test node for skipIf and runIf will be the next CallExpression
if (mode === 'each' || mode === 'skipIf' || mode === 'runIf') {
if (mode === 'each' || mode === 'skipIf' || mode === 'runIf' || mode === 'for') {
return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { expectTypeOf, test } from 'vitest'

test.each(['some-value'])('%s', () => {
test.each(['some-value'])('each: %s', () => {
expectTypeOf(1).toEqualTypeOf(2)
})

test.for(['some-value'])('for: %s', () => {
expectTypeOf(1).toEqualTypeOf(2)
})

Expand Down
5 changes: 3 additions & 2 deletions test/typescript/test/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ describe('ignoreSourceErrors', () => {
})

describe('when the title is dynamic', () => {
it('correctly works', async () => {
it('works correctly', async () => {
const vitest = await runVitest({
root: resolve(__dirname, '../fixtures/dynamic-title'),
})

expect(vitest.stdout).toContain('✓ %s')
expect(vitest.stdout).toContain('✓ for: %s')

Check failure on line 122 in test/typescript/test/runner.test.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

test/runner.test.ts > when the title is dynamic > works correctly

AssertionError: expected '\n RUN v2.1.0-beta.5 /home/runner/wo…' to contain '✓ for: %s' - Expected + Received - ✓ for: %s + + RUN v2.1.0-beta.5 /home/runner/work/vitest/vitest/test/typescript/fixtures/dynamic-title + + ✓ test/dynamic-title.test-d.ts > each: %s + ✓ test/dynamic-title.test-d.ts > for: %s + ✓ test/dynamic-title.test-d.ts > dynamic skip + ✓ test/dynamic-title.test-d.ts > template string + ✓ test/dynamic-title.test-d.ts > template ${"some value"} string + ✓ test/dynamic-title.test-d.ts > template ${`literal`} string + ✓ test/dynamic-title.test-d.ts > name + ✓ test/dynamic-title.test-d.ts > (() => "some name")() + + Test Files 1 passed (1) + Tests 8 passed (8) + Type Errors no errors + Start at 08:24:52 + Duration 2.47s (transform 0ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms, typecheck 2.46s) + + ❯ test/runner.test.ts:122:27

Check failure on line 122 in test/typescript/test/runner.test.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20.14)

test/runner.test.ts > when the title is dynamic > works correctly

AssertionError: expected '\n RUN v2.1.0-beta.5 /home/runner/wo…' to contain '✓ for: %s' - Expected + Received - ✓ for: %s + + RUN v2.1.0-beta.5 /home/runner/work/vitest/vitest/test/typescript/fixtures/dynamic-title + + ✓ test/dynamic-title.test-d.ts > each: %s + ✓ test/dynamic-title.test-d.ts > for: %s + ✓ test/dynamic-title.test-d.ts > dynamic skip + ✓ test/dynamic-title.test-d.ts > template string + ✓ test/dynamic-title.test-d.ts > template ${"some value"} string + ✓ test/dynamic-title.test-d.ts > template ${`literal`} string + ✓ test/dynamic-title.test-d.ts > name + ✓ test/dynamic-title.test-d.ts > (() => "some name")() + + Test Files 1 passed (1) + Tests 8 passed (8) + Type Errors no errors + Start at 08:24:00 + Duration 2.38s (transform 0ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms, typecheck 2.37s) + + ❯ test/runner.test.ts:122:27

Check failure on line 122 in test/typescript/test/runner.test.ts

View workflow job for this annotation

GitHub Actions / test (macos-14, 20.14)

test/runner.test.ts > when the title is dynamic > works correctly

AssertionError: expected '\n RUN v2.1.0-beta.5 /Users/runner/w…' to contain '✓ for: %s' - Expected + Received - ✓ for: %s + + RUN v2.1.0-beta.5 /Users/runner/work/vitest/vitest/test/typescript/fixtures/dynamic-title + + ✓ test/dynamic-title.test-d.ts > each: %s + ✓ test/dynamic-title.test-d.ts > for: %s + ✓ test/dynamic-title.test-d.ts > dynamic skip + ✓ test/dynamic-title.test-d.ts > template string + ✓ test/dynamic-title.test-d.ts > template ${"some value"} string + ✓ test/dynamic-title.test-d.ts > template ${`literal`} string + ✓ test/dynamic-title.test-d.ts > name + ✓ test/dynamic-title.test-d.ts > (() => "some name")() + + Test Files 1 passed (1) + Tests 8 passed (8) + Type Errors no errors + Start at 08:22:46 + Duration 1.35s (transform 0ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms, typecheck 1.34s) + + ❯ test/runner.test.ts:122:27

Check failure on line 122 in test/typescript/test/runner.test.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20.14)

test/runner.test.ts > when the title is dynamic > works correctly

AssertionError: expected '\n RUN v2.1.0-beta.5 D:/a/vitest/vit…' to contain '✓ for: %s' - Expected + Received - ✓ for: %s + + RUN v2.1.0-beta.5 D:/a/vitest/vitest/test/typescript/fixtures/dynamic-title + + ✓ test/dynamic-title.test-d.ts > each: %s + ✓ test/dynamic-title.test-d.ts > for: %s + ✓ test/dynamic-title.test-d.ts > dynamic skip + ✓ test/dynamic-title.test-d.ts > template string + ✓ test/dynamic-title.test-d.ts > template ${"some value"} string + ✓ test/dynamic-title.test-d.ts > template ${`literal`} string + ✓ test/dynamic-title.test-d.ts > name + ✓ test/dynamic-title.test-d.ts > (() => "some name")() + + Test Files 1 passed (1) + Tests 8 passed (8) + Type Errors no errors + Start at 08:27:46 + Duration 2.80s (transform 0ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms, typecheck 2.78s) + + ❯ test/runner.test.ts:122:27
expect(vitest.stdout).toContain('✓ each: %s')
expect(vitest.stdout).toContain('✓ dynamic skip')
expect(vitest.stdout).not.toContain('✓ false') // .skipIf is not reported as a separate test
expect(vitest.stdout).toContain('✓ template string')
Expand Down

0 comments on commit db49551

Please sign in to comment.