vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.08k stars 1.18k forks source link

Test Context lost the types in 0.28.1 #2741

Open meza opened 1 year ago

meza commented 1 year ago

Describe the bug

Since v0.28.1, typescript now considers all test context variables to be implicit any types.

Reproduction

The error popped up in this automated dependency PR: https://github.com/meza/minecraft-mod-manager/pull/208

You can reproduce it in the branch it refers to.

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
    Memory: 25.23 GB / 31.31 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 19.4.0 - ~/.node-installations/bin/node
    Yarn: 1.22.17 - /mnt/c/Users/meza/AppData/Roaming/npm/yarn
    npm: 9.2.0 - ~/.node-installations/bin/npm
  npmPackages:
    @vitest/coverage-c8: 0.28.1 => 0.28.1
    @vitest/ui: ^0.28.0 => 0.28.1
    vitest: 0.28.1 => 0.28.1

Used Package Manager

pnpm

Validations

sheremet-va commented 1 year ago

It was a breaking change. To extend context, you need to augment @vitest/runner module now. Documentation should be updated

meza commented 1 year ago

Do you have an example of this somewhere? Just trying to have simple, local test contexts.

bhvngt commented 1 year ago

I am facing similar issue.

To fix my issue, I had to make following changes

  1. refactored TestContext declaration so that instead of module vitest, module @vitest/runner is augmented.
  2. I had to change import of TestContext and it from vitest to @vitest/runner
  3. I had to annotate each of it blocks with TestContext. @sheremet-va This one was quiet tedious. This was not required earlier.

Here's change in code

- declare module "vitest" {
+ declare module "@vitest/runner" {  
    export interface TestContext {
        app: App
    }
}
- import { beforeEach, describe, expect, it, TestContext } from "vitest";
+ import { beforeEach, describe, expect } from "vitest";
+ import { it, TestContext } from "@vitest/runner";

...
- it("should initialize screening state with empty object", function({ app }) {
+ it<TestContext>("test description", function({ app, expect }) {

Because of point no. 3 I have put upgrade to 0.28.2 on hold. Would like to wait for the official clarification on this issue.

sheremet-va commented 1 year ago

Would like to wait for the official clarification on this issue.

Points 2 and 3 are bugs. Point 1 is expected.

meza commented 1 year ago

Feels like a big step backwards in usability if this augmentation has to be done even for local test contexts

sheremet-va commented 1 year ago

Feels like a big step backwards in usability if this augmentation has to be done even for local test contexts

Yes, it is a bug. You shouldn't need to augment global context to use local contexts.

meza commented 1 year ago

Thanks for the clarification, that sounds much better :)

lukaVarga commented 1 year ago

@sheremet-va might this also cause type tests to not work correctly? Eg. this test is passing even though it clearly shouldn't pass (and the IDE also shows the types not matching)

test('type', () => {
  expectTypeOf(5).toEqualTypeOf<string>();
});
image
sheremet-va commented 1 year ago

@sheremet-va might this also cause type tests to not work correctly? Eg. this test is passing even though it clearly shouldn't pass (and the IDE also shows the types not matching)

I don't think it's related. Please, open a separate issue.

sheremet-va commented 1 year ago

For some reason, reexporting types in pnpm doesn't work. Typescript doesn't pick up types from @vitest/runner and all test/describe/... functions are marked as any.

When using other package managers local context types work fine.

meza commented 1 year ago

I found it! It's the "preserveSymlinks": true in the tsconfig!

maximilianfixl commented 12 months ago

@sheremet-va might this also cause type tests to not work correctly? Eg. this test is passing even though it clearly shouldn't pass (and the IDE also shows the types not matching)

test('type', () => {
  expectTypeOf(5).toEqualTypeOf<string>();
});
image

Is there any news on this? In version 1.0.0-beta.5 as well as in the versions below, the tests are still passed. This makes expectTypeOf currently unusable.

sheremet-va commented 12 months ago

@sheremet-va might this also cause type tests to not work correctly? Eg. this test is passing even though it clearly shouldn't pass (and the IDE also shows the types not matching)

test('type', () => {
  expectTypeOf(5).toEqualTypeOf<string>();
});
image

Is there any news on this? In version 1.0.0-beta.5 as well as in the versions below, the tests are still passed. This makes expectTypeOf currently unusable.

This issue is not even about TypeScript typechecker. If you have any problems, open a new issue with a reproduction.