vitest-dev / vitest

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

Types are not actually Jest compatible #3146

Open TimothyJones opened 1 year ago

TimothyJones commented 1 year ago

Describe the bug

On the front page of the documentation, it says that it's "Jest Compatible".

However, the type for a vitest TestFunction is () => Awaitable<void>.

But, in Jest the corresponding part of their ProvidesCallback type is () => Promise<unknown>.

This matters in beforeEach and afterEach, where you might quite reasonably call a setup promise which happens to return a value that you don't care about.

I think there are two possible fixes:

(side note: I think the note on the front page suggests that it would be fully jest compatible with no changes).

Reproduction

describe("a test that doesn't compile", ()=> {
  it("won't compile",() => {
     return Promise.resolve(2);
  });

  beforeEach(() => {
    // neither will this
    return Promise.resolve("Some setup promise that happens to return a value")
  });
})

System Info

N/A

Used Package Manager

npm

Validations

sheremet-va commented 1 year ago

"Hooks" section in migration guide literally mentions what you need to do in this case.

sheremet-va commented 1 year ago

It was also changed quite some time ago not to throw an error if something either than a function is returned. I don't see the Vitest version in your issue template (which is actually required).

sheremet-va commented 1 year ago

Just now understood what the issue is talking about 🤦‍♂️

Changing the type to unknown should be fine.

TimothyJones commented 1 year ago

Apologies, perhaps I wasn't clear with the way I worded the description.

I'm reporting it on behalf of another user (see linked issue), but I checked the source and it seems current, so I guess 0.29.8. Apologies for not including this.

I don't see the issue I'm asking about mentioned in the hooks section here - but that might have been before you realised what I was asking.

I'd be happy to send a PR updating the type.

TimothyJones commented 1 year ago

Raised #3147