vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Test Context lost the types in 0.28.1

Open meza opened this issue 2 years ago • 13 comments

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

meza avatar Jan 24 '23 13:01 meza

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

sheremet-va avatar Jan 24 '23 13:01 sheremet-va

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

meza avatar Jan 24 '23 17:01 meza

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.

bhvngt avatar Jan 26 '23 06:01 bhvngt

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

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

sheremet-va avatar Jan 26 '23 07:01 sheremet-va

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

meza avatar Jan 26 '23 14:01 meza

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.

sheremet-va avatar Jan 26 '23 14:01 sheremet-va

Thanks for the clarification, that sounds much better :)

meza avatar Jan 26 '23 14:01 meza

@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

lukaVarga avatar Jan 29 '23 21:01 lukaVarga

@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 avatar Jan 30 '23 07:01 sheremet-va

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.

sheremet-va avatar Jan 30 '23 07:01 sheremet-va

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

meza avatar Feb 25 '23 23:02 meza

@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.

maximilianfixl avatar Nov 21 '23 11:11 maximilianfixl

@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.

sheremet-va avatar Nov 21 '23 11:11 sheremet-va