vitest
vitest copied to clipboard
Test Context lost the types in 0.28.1
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
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
It was a breaking change. To extend context, you need to augment @vitest/runner
module now. Documentation should be updated
Do you have an example of this somewhere? Just trying to have simple, local test contexts.
I am facing similar issue.
To fix my issue, I had to make following changes
- refactored
TestContext
declaration so that instead of modulevitest
, module@vitest/runner
is augmented. - I had to change import of
TestContext
andit
fromvitest
to@vitest/runner
- I had to annotate each of
it
blocks withTestContext
. @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.
Would like to wait for the official clarification on this issue.
Points 2 and 3 are bugs. Point 1 is expected.
Feels like a big step backwards in usability if this augmentation has to be done even for local test contexts
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.
Thanks for the clarification, that sounds much better :)
@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>();
});

@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.
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.
I found it! It's the "preserveSymlinks": true
in the tsconfig!
@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>(); });
![]()
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 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>(); });
![]()
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 makesexpectTypeOf
currently unusable.
This issue is not even about TypeScript typechecker. If you have any problems, open a new issue with a reproduction.