Skip to content

Broken types for extended test context #6660

@dosolkowski-work

Description

@dosolkowski-work

Describe the bug

When upgrading from vitest 2.0.5 to 2.1.2, something has changed with the types for custom test contexts (export const myTest = test.extend(...)) that results in tsc reporting all test function parameters as having any type, when it previously used to be able to identify their types. An example error:

src/utils/useFrozenState.test.ts:41:63 - error TS7006: Parameter 'initialState' implicitly has an 'any' type.

41     it.each([null, undefined])("refuses to freeze %o", async (initialState) => {
                                                                 ~~~~~~~~~~~~

In this case, it should be able to tell that initialState has a type of null | undefined. Vitest was the only library changed, and reverting back to 2.0.5 restores the prior behavior where types were identified correctly.

Reproduction

Our repository is set up similar to this arrangement: https://github.com/qidydl/vite-workspace-experiment where we use paths and references in tsconfig.json to refer to shared code. The test context in the shared code is not complex:

export interface CustomTestContext {
    user: UserEvent;
}

export const customTest = test.extend<CustomTestContext>({
    user: async ({}, use) => await use(userEvent.setup()),
});

The usage is also not complex:

import { customTest as it } from "@shared/_test/testUtils";

it.each([null, undefined])("refuses to freeze %o", async (initialState) => {
    ...test using initialState
});

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13950HX
    Memory: 13.43 GB / 31.61 GB
  Binaries:
    Node: 20.18.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.12.1 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @vitest/coverage-istanbul: ^2.1.2 => 2.1.2
    @vitest/eslint-plugin: ^1.1.7 => 1.1.7
    @vitest/ui: ^2.1.2 => 2.1.2
    vite: ^5.4.8 => 5.4.8
    vitest: ^2.1.2 => 2.1.2

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions