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

Concurrent testing breaks snapshot assertions #551

Closed
NullVoxPopuli opened this issue Jan 17, 2022 · 8 comments · Fixed by #900
Closed

Concurrent testing breaks snapshot assertions #551

NullVoxPopuli opened this issue Jan 17, 2022 · 8 comments · Fixed by #900

Comments

@NullVoxPopuli
Copy link

Describe the bug

When using snapshot assertions and concurrent tests, I get this error:

Error: Snapshot cannot be used outside of test

Reproduction

import { describe, expect, test } from 'vitest';

describe('repro', () => {
  test.concurrent('a', async () => {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    expect('foo').toMatchSnapshot();
  });

  test.concurrent('b', async () => {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    expect('bar').toMatchSnapshot();
  });
});

System Info

System:
    OS: Linux 5.13 Ubuntu 21.10 21.10 (Impish Indri)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Memory: 723.05 MB / 31.14 GB
    Container: Yes
    Shell: 5.1.8 - /bin/bash
  Binaries:
    Node: 16.13.2 - ~/.volta/tools/image/node/16.13.2/bin/node
    Yarn: 1.22.17 - ~/.volta/tools/image/yarn/1.22.17/bin/yarn
    npm: 7.24.2 - ~/Development/NullVoxPopuli/ember-apply/node_modules/.bin/npm
  Browsers:
    Chrome: 96.0.4664.93
    Firefox: 96.0.1



### Used Package Manager

yarn

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
@antfu
Copy link
Member

antfu commented Jan 17, 2022

Yes this is the limitation of JS as you can't keep the context in async concurrent execution. I am not sure if there is a solution to that (similar to Vue's async setup: https://antfu.me/posts/async-with-composition-api). Otherwise we might need to document it

@sheremet-va
Copy link
Member

Yes this is the limitation of JS as you can't keep the context in async concurrent execution. I am not sure if there is a solution to that (similar to Vue's async setup: https://antfu.me/posts/async-with-composition-api). Otherwise we might need to document it

I think we can bind test to expect and pass it inside a context (first function argument), if the this idea will ever be accepted 👀

@antfu
Copy link
Member

antfu commented Apr 5, 2022

I think we can bind test to expect and pass it inside a context

Yes, there is a pending pr for it #900

@antfu antfu added the bug label Apr 10, 2022
@zbyte64
Copy link

zbyte64 commented Apr 23, 2022

Bumped into this while trying to migrate from jest. Is there a workaround?

@ggoodman
Copy link

You might get some mileage on newer versions of node with AsyncLocalStorage

@antfu
Copy link
Member

antfu commented Apr 25, 2022

@ggoodman
Copy link

@antfu I love this solution! Do you think that you will move more stuff into this model of explicit context?

I'm thinking of how the node:test and tap modules thread the it function through this mechanism of context propagation.

@Alioth996
Copy link

@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants