Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toMatchInlineSnapshot produces invalid code when called inside a loop #6327

Closed
6 tasks done
silverwind opened this issue Aug 12, 2024 · 5 comments · Fixed by #6332
Closed
6 tasks done

toMatchInlineSnapshot produces invalid code when called inside a loop #6327

silverwind opened this issue Aug 12, 2024 · 5 comments · Fixed by #6332
Labels
p1-chore Doesn't change code behavior (priority)

Comments

@silverwind
Copy link
Contributor

silverwind commented Aug 12, 2024

Describe the bug

Calling toMatchInlineSnapshot within a loop produces invalid code:

import {test, expect} from "vitest";

test("snap", () => {
  for (const str of ["foo", "bar"]) {
    expect(str).toMatchInlineSnapshot();
    // transforms to expect(str).toMatchInlineSnapshot(`"foo"``"bar"`);
  }
});

The transformed code produces a runtime error:

TypeError: "\"foo\"" is not a function

Typescript also does not like it:

src/basic.test.ts:5:39 - error TS2349: This expression is not callable.
  Type 'String' has no call signatures.

8     expect(str).toMatchInlineSnapshot(`"foo"``"bar"`);

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-r8dfgp?file=src%2Fbasic.test.ts

Validations

@silverwind silverwind changed the title toMatchInlineSnapshot produces invalid typescript when called inside a loop toMatchInlineSnapshot produces invalid code when called inside a loop Aug 12, 2024
@sheremet-va
Copy link
Member

toMatchInlineSnapshot is not supported in a loop. We even throw an error when it's called in test.each.

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Aug 12, 2024
@sheremet-va
Copy link
Member

sheremet-va commented Aug 12, 2024

I guess we can try to find a way to throw an error here for a better DX, but I don't have ideas how. I will keep the issue open for now.

@sheremet-va sheremet-va reopened this Aug 12, 2024
@sheremet-va sheremet-va added p1-chore Doesn't change code behavior (priority) and removed pending triage labels Aug 12, 2024
@hi-ogawa
Copy link
Contributor

I remembered Jest throws a better error for this case. I haven't checked how they do, but made a quick repro for a reference https://stackblitz.com/edit/github-tj5mf5?file=basic.test.js

 FAIL  ./basic.test.js
  ● Test suite failed to run

    Jest: Multiple inline snapshots for the same call are not supported.

@silverwind
Copy link
Contributor Author

silverwind commented Aug 13, 2024

Could it be supported, though? It should be possible to encode multiple values in one string, but likely it will need some creative escaping to support all possible values.

@sheremet-va
Copy link
Member

Could it be supported, though? It should be possible to encode multiple values in one string, but likely it will need some creative escaping to support all possible values.

I don't think it could be supported or should be supported. It is much harder to support internally and it makes the result code look even less readable. Use toMatchSnapshot if you want to generate snapshots in a loop.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 28, 2024
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p1-chore Doesn't change code behavior (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants