vitest-dev / vitest-dev/vitest

Missing type inference for expect(...).toEqual<T>(...)

Open
#8,326 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

p2-nice-to-have types
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

Type inference for expect(x).toEqual(y) is lacking.

That is, you can pass x and y of incompatible types without getting a type inspection.

Compared with type inference for assert.deepStrictEqual(x,y), which works as expected, and correctly gives a type inspection if the type of y is not assignable to the type of x.

You can of course manually type-hint as e.g. expect(x).toEqual<Y>(y), and this might be more appropriate and explicit in some cases - however, it is inconsistent with assert.deepStrictEqual, which does support inference.

(It also slows you down when writing tests, since you won't get auto-completions for y, e.g. when y is an object literal type, unless you manually provide the type yourself.)

Reproduction

Here's an example:

import { test, expect, assert } from "vitest"

type Foo = {
    hello: 'world'
}

function makeFoo(): Foo {
    return { hello: 'world' }
}

test(`can parse PTN place notations`, () => {
  expect(makeFoo()).toEqual({ nope: 'wrong' }) // 👎

  expect(makeFoo()).toEqual<Foo>({ nope: 'wrong' }) // 👍

  assert.deepStrictEqual(makeFoo(), { nope: 'wrong' }) // 👍
})

You can see it failing in this TS playground.

System Info
System:
    OS: Linux 6.6 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (24) x64 AMD Ryzen 9 7900 12-Core Processor
    Memory: 12.68 GB / 15.49 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.20.5 - ~/.nvm/versions/node/v18.20.5/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v18.20.5/bin/npm
    pnpm: 9.15.2 - ~/.nvm/versions/node/v18.20.5/bin/pnpm
    bun: 1.0.0 - ~/.bun/bin/bun
  npmPackages:
    vitest: ^3.2.4 => 3.2.4
Used Package Manager

npm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript playground reproduction and compare the inferred types for expect(...).toEqual(...) with assert.deepStrictEqual(...). Trace the type declarations behind expect and toEqual; done when incompatible argument types are rejected and object-literal completions work without an explicit generic type.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.