Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
Something went wrong. Try again.
2.4 kB · 75 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576/* oxlint-disable typescript/ban-ts-comment */
import { describe, expectTypeOf, test, vi } from 'vitest'
describe('test', () => { test('some-test', () => { expectTypeOf(Date).toBeConstructibleWith(new Date()) expectTypeOf(Date).toBeConstructibleWith('01-01-2000')
type ResponsiveProp<T> = T | T[] | { xs?: T; sm?: T; md?: T } const getResponsiveProp = <T>(_props: T): ResponsiveProp<T> => ({}) interface CSSProperties { margin?: string padding?: string } const cssProperties: CSSProperties = { margin: '1px', padding: '2px' } expectTypeOf(getResponsiveProp(cssProperties)) .exclude<unknown[] | { xs?: unknown }>() // .exclude<{ xs?: unknown }>() .toEqualTypeOf<CSSProperties>() })
describe('test2', () => { test('some-test 2', () => { expectTypeOf(Promise.resolve('string')).resolves.toEqualTypeOf<string>() expectTypeOf(45).toEqualTypeOf(45) }) })
test('ignored error', () => { // @ts-ignore 45 is not a string expectTypeOf(45).toEqualTypeOf<string>() })
test('expected error', () => { // @ts-expect-error 45 is not a string expectTypeOf(45).toEqualTypeOf<string>() })
test('spyOn googleapis compiles', () => { // googleapis-like typing to reproduce https://github.com/vitest-dev/vitest/issues/3141 // oxlint-disable-next-line no-unassigned-vars let google!: { [key: string]: unknown sheets: () => { foo: string } } vi.spyOn(google, 'sheets').mockReturnValue({ foo: 'bar' }) // @ts-expect-error vi.spyOn(google, 'sheets').mockReturnValue({ foo: 1234 }) })
test('spyOn private and protected methods compiles', () => { class TestClass { private privateMethod(): number { return 42 }
protected protectedMethod(): string { return '42' } }
const instance = new TestClass() vi.spyOn(instance, 'privateMethod').mockReturnValue(1) vi.spyOn(instance, 'protectedMethod').mockReturnValue('1') // @ts-expect-error private method returns number vi.spyOn(instance, 'privateMethod').mockReturnValue('1') // @ts-expect-error protected method returns string vi.spyOn(instance, 'protectedMethod').mockReturnValue(1) // @ts-expect-error unknown methods are rejected vi.spyOn(instance, 'unknownMethod') })})
expectTypeOf({ wolk: 'true' }).toHaveProperty('wolk')