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

Error: toMatchInlineSnapshot cannot be called multiple times at the same location. #7450

Closed
6 tasks done
noam-honig opened this issue Feb 9, 2025 · 3 comments · May be fixed by #7464
Closed
6 tasks done

Error: toMatchInlineSnapshot cannot be called multiple times at the same location. #7450

noam-honig opened this issue Feb 9, 2025 · 3 comments · May be fixed by #7464

Comments

@noam-honig
Copy link

Describe the bug

I have a shared test.ts file that is used by multiple test files that are calling it and it uses toMatchInlineSnapshot.
It's a set of standard tests for a database that are executed for each database adapter implementation that I have.

All was great and worked in version 2.0.5. As soon as I updated to version 2.1.9 I started getting the error:
Error: toMatchInlineSnapshot cannot be called multiple times at the same location.

It is the same line of code of test that is executed several times wih multiple test files.

How can I fix rhat to work again?

Reproduction

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

test('a', () => {
  myTest();
});
test('b', () => {
  myTest();
});

function myTest() {
  expect(1 + 1).toMatchInlineSnapshot(`2`);
}

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (16) x64 Intel(R) Core(TM) i7-7820X CPU @ 3.60GHz
    Memory: 6.43 GB / 31.68 GB
  Binaries:
    Node: 20.14.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.6 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (132.0.2957.127)
    Internet Explorer: 11.0.26100.1882

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Feb 9, 2025

Technically it wasn't "all was good" since it's broken if you run from a clean state, so that's why it's gone as par #6327, #6332

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

function myTest() {
  expect(1 + 1).toMatchInlineSnapshot(`2``2`);
}

For this reproduction, expect(1 + 1).toEqual(2) is probably the only choice.

I have a shared test.ts file that is used by multiple test files that are calling it and it uses toMatchInlineSnapshot.

Actually different test files calling toMatchInlineSnapshot might be a workaround though https://stackblitz.com/edit/vitest-dev-vitest-cukpaczk?file=test%2Fshared-test.ts

@hi-ogawa hi-ogawa closed this as completed Feb 9, 2025
@noam-honig
Copy link
Author

noam-honig commented Feb 10, 2025 via email

@jycouet
Copy link

jycouet commented Feb 10, 2025

I roger what @noam-honig wrote.
As said also at the start, in remult there are hundreds of tests using this to make sure all databases have the same behavior.
And toMatchInlineSnapshot is such a amazing way of doing this.


If I understand correctly, the root of this was:

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

Isn't it more a transform issue ?
I think that here I would expect:

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

Or maybe having a waring only at the generation of the transform from a cleanstate is sufficient?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants