Skip to content

toMatchInlineSnapshot produces invalid code when called inside a loop #6327

@silverwind

Description

@silverwind

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    p1-choreDoesn't change code behavior (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions