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

Usage with Jest's test.concurrent.each #1091

Open
jimmycallin opened this issue Jun 19, 2022 · 3 comments
Open

Usage with Jest's test.concurrent.each #1091

jimmycallin opened this issue Jun 19, 2022 · 3 comments

Comments

@jimmycallin
Copy link

  • @testing-library/dom version: 13.3.0
  • Testing Framework and version: jest@28.1.1
  • DOM Environment: jsdom

Relevant code or config:

/**
 * @jest-environment jsdom
 */

import { render } from "@testing-library/react";
import React from "react";

// this works fine, since we don't run concurrently
test.each([{ param: 1 }, { param: 2 }])("it works $param", ({ param }) => {
  const { getByTestId } = render(<div data-testid="test-container">Hello</div>);
  expect(getByTestId("test-container")).toBeTruthy();
});

// this won't work though, since it will find multiple DOM nodes with id test-container
test.concurrent.each([{ param: 1 }, { param: 2 }])(
  "it works $param",
  ({ param }) => {
    const { getByTestId } = render(
      <div data-testid="test-container">Hello</div>
    );
    expect(getByTestId("test-container")).toBeTruthy();
  }
);
;

What you did:

I have a set of tests that are relying on jest's test.concurrent feature, and I would like to be able to run these concurrently using test.concurrent.each. Just switching from test.concurrent to test.concurrent.each doesn't seem to work.

What happened:

The render method seems to add these to the same JSDOM document, causing issues with e.g. getByTestId

TestingLibraryElementError: Found multiple elements by: [data-testid="test-container"]

Reproduction:

See code snippet on top, or this repository: https://github.com/jimmycallin/rtl-concurrent-each

Problem description:

test.concurrent.each is a supported feature in jest, and it would be great to be able to run multiple testing-library tests in parallel using this feature.

Suggested solution:

@jimmycallin jimmycallin changed the title Testing-library won't work with Testing-library doesn't work with jest.concurrent.each Jun 19, 2022
@jimmycallin jimmycallin changed the title Testing-library doesn't work with jest.concurrent.each Testing-library doesn't work with jest's test.concurrent.each Jun 19, 2022
@eps1lon
Copy link
Member

eps1lon commented Jun 19, 2022

It's expected that it doesn't work in the normal configuration because we render into the global document. But since now two tests are rendering concurrently into the same document, the test queries cannot behave the same way.

You would have to create isolated containers for each test. We should probably add explicit documentation for how to achive that.

@eps1lon eps1lon transferred this issue from testing-library/dom-testing-library Jun 19, 2022
@eps1lon eps1lon changed the title Testing-library doesn't work with jest's test.concurrent.each Usage with Jest's test.concurrent.each Jun 19, 2022
@jimmycallin
Copy link
Author

Thanks, that make sense!

For reference, how would I create isolated containers for each test?

@alexkrolick
Copy link
Collaborator

https://jestjs.io/docs/configuration#testenvironment-string

One approach could be to use a custom environment that creates a new JSDOM global in handleTestEvent. This wouldn't just be useful for concurrent tests, it would also isolate regular serial tests a lot better. But there's probably a pretty large perf cost to doing it.

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

No branches or pull requests

3 participants