Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.57 KB

File metadata and controls

49 lines (36 loc) · 1.57 KB

vitest/prefer-expect-type-of

📝 Enforce using expect(...).toBeTypeOf(...) instead of expect(typeof ...).toBe(...).

⚠️ This rule warns in the 🌐 all config.

🔧 This rule is automatically fixable by the --fix CLI option.

This rule enforces using Vitest's toBeTypeOf matcher instead of expect(typeof value).toBe(type) for runtime type checking.

Rule Details

expect(typeof value).toBe(type) works but is awkward and produces poor failure messages. Vitest's built-in toBeTypeOf matcher does the same typeof comparison with a clearer API and better error output. This rule rewrites the typeof-based pattern to use toBeTypeOf automatically.

Incorrect

import { test, expect } from 'vitest'

test('type checking', () => {
  expect(typeof 'hello').toBe('string')
  expect(typeof 42).toBe('number')
  expect(typeof true).toBe('boolean')
  expect(typeof {}).toBe('object')
  expect(typeof (() => {})).toBe('function')
  expect(typeof Symbol()).toBe('symbol')
  expect(typeof 123n).toBe('bigint')
  expect(typeof undefined).toBe('undefined')
})

Correct

import { test, expect } from 'vitest'

test('type checking', () => {
  expect('hello').toBeTypeOf('string')
  expect(42).toBeTypeOf('number')
  expect(true).toBeTypeOf('boolean')
  expect({}).toBeTypeOf('object')
  expect(() => {}).toBeTypeOf('function')
  expect(Symbol()).toBeTypeOf('symbol')
  expect(123n).toBeTypeOf('bigint')
  expect(undefined).toBeTypeOf('undefined')
})