fix: switch ExpectStatic any types to AsymmetricMatcher<unknown>, with DeeplyAllowMatchers<T>#7016
Conversation
any types to unknownany types to unknown
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site configuration. |
|
How is this going to work? expect(obj).toEqual<{
id: string
name: string
}>({
id: expect.any(String),
name: 'Amelia',
})Error:
|
Aha! Updated to use a |
any types to unknownany types to AsymmetricMatcher<unknown>, with DeeplyAllowMatchers<T>
|
Hey @JoshuaKGoldberg! I'd love to include this in 3.2. Could you add some tests to |
|
That's great, thank you @sheremet-va! Yes please - I'm a bit swamped this month and don't think I'll have time to figure out the |
|
Hello Vite! I just bumped to 3.2.0 and I ran into the same problem as #7016 (comment) Here is my code (details omitted for clarity): With an error:
What is the correct way to type this? I tried to use |
Looks like we should expose it, I will create a PR |
|
I feel this change is breaking some legit types starting at Vitest 3.2: function expectMany<T>(value: { enabled: false } | { enable: true; data: T }) {
// In Vitest 3.2:
//
// Argument of type '{ enabled: false; } | { enable: true; data: T; }' is not assignable to parameter of type '{ enabled: false | AsymmetricMatcher<unknown, MatcherState>; } | { enable: true | AsymmetricMatcher<unknown, MatcherState>; data: AsymmetricMatcher<...> | DeeplyAllowMatchers<...>; }'.
// Type '{ enable: true; data: T; }' is not assignable to type '{ enabled: false | AsymmetricMatcher<unknown, MatcherState>; } | { enable: true | AsymmetricMatcher<unknown, MatcherState>; data: AsymmetricMatcher<...> | DeeplyAllowMatchers<...>; }'.
// Type '{ enable: true; data: T; }' is not assignable to type '{ enable: true | AsymmetricMatcher<unknown, MatcherState>; data: AsymmetricMatcher<unknown, MatcherState> | DeeplyAllowMatchers<T>; }'.
// Types of property 'data' are incompatible.
// Type 'T' is not assignable to type 'AsymmetricMatcher<unknown, MatcherState> | DeeplyAllowMatchers<T>'.
// Type 'T' is not assignable to type 'AsymmetricMatcher<unknown, MatcherState>'.ts(2345)
expect(value).toEqual(value);
} |
|
We heavily use the satisfies keyword in our tests. This change seems to make it impossible to use it. An abstract example, which demonstrates the problem: type Foo = {
bar: string;
foobar: {
foo: string;
bar: string;
}[]
}
const mockedFoo = vi.mocked(mock<Foo>()) // mock is from https://github.com/eratio08/vitest-mock-extended - but the important part is the expect statement
expect(mockedFoo).toHaveBeenCalledWith({
bar: "baz",
foobar: expect.any(Array),
} satisfies Foo)The error: |
|
We have some tests that pass
Were we using const avroReaderStub = new AvroReader(expect.anything());
vi.mocked(avroReaderStub.hasNext).mockReturnValue(true);
const chunk = new Chunk(avroReaderStub as any, 0, 0, "log/00/2020/07/30/2300/");
assert.equal(chunk.hasNext(), true);
vi.mocked(avroReaderStub.hasNext).mockReturnValue(false);
assert.equal(chunk.hasNext(), false); |
|
@jeremymeng Your usage doesn't look right. If it can just work by |
|
@hi-ogawa Thanks! Right, I don't think we care about the constructor argument for this test. I fixed the test by replacing |
Description
Switches the two
anytypes tounknownAsymmetricMatcher<unknown>, and updated APIs such astoEqual<T>to deeply allow those using a newDeeplyAllowMatchers<T>.Sending as a draft for reference.
Fixes #7015.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
I don't know how to best do this. Do you want a type test somewhere?I added atypes.test.tsfile but am 100% sure I missed a much better way to do thatpnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.