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

Snapshot tests fail with concurrent execution (Error: Snapshot cannot be used outside of test) #2729

Closed
6 tasks done
asdfjkalsdfla opened this issue Jan 22, 2023 · 4 comments · Fixed by #2733
Closed
6 tasks done
Labels
documentation Improvements or additions to documentation

Comments

@asdfjkalsdfla
Copy link
Contributor

asdfjkalsdfla commented Jan 22, 2023

Describe the bug

As of 0.27.2, concurrent async tests that use snapshots fail with Error: Snapshot cannot be used outside of test message. These tests worked fine under 0.27.1.

Related tickets from the past

#551

Reproduction

https://stackblitz.com/edit/vitejs-vite-kcqha8?file=package.json,package-lock.json,main.test.jsx&terminal=dev

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    vite: ^4.0.4 => 4.0.4 
    vitest: ^0.27.2 => 0.27.2

Used Package Manager

npm

Validations

@sheremet-va
Copy link
Member

You need to use "expect" from test context, when using snapshots in concurrent tests:

it("test", ({ expect }) => {
  expect(1).toMatchSnapshot()
})

I don't know why it worked for you before, but it shouldn't have, and was never intended to work with global expect.

@asdfjkalsdfla
Copy link
Contributor Author

asdfjkalsdfla commented Jan 22, 2023

Interesting. The small case in #551 also uses a global expect vs expect from the test case. Any way to return back to the previous behavior? If not, I'm happy to submit an update to the documentation to make this clearer.

@sheremet-va
Copy link
Member

Interesting. The small case in #551 also uses a global expect vs expect from the test case. Any way to return back to the previous behavior?

Do you mean the reproduction? It never worked, and couldn't have worked, because snapshot needs to know information about the current test. If you run tests concurrently, you don't have a reliable way to mark the test as "current", because they are running at the same time. To bypass that, you need to use expect from the context.

If not, I'm happy to submit an update to the documentation to make this clearer.

Feel free to submit PR with the change using ::: warning syntax in toMatchSnapshot/toMatchInlineSnapshot sections

@sheremet-va sheremet-va added the documentation Improvements or additions to documentation label Jan 22, 2023
@asdfjkalsdfla
Copy link
Contributor Author

Yeah, I was referring to the reproduction in the original report of #551. That case also worked without issues in 0.27.1 and stopped working in 0.27.2.

I'll get a PR for the documentation update over. In addition to the snapshot page, I was thinking of updating the concurrent example on the feature page to show it using a context specific expect.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation
Projects
None yet
2 participants