Skip to content

getByRole fails when running under vitest with --no-isolate --no-file-parallelismΒ #1337

@Janpot

Description

@Janpot
  • @testing-library/dom version: 10.4.0
  • Testing Framework and version: vitest 2.1.2
  • DOM Environment: jsdom 25.0.1

Relevant code or config:

Have two files with following content

// ./foo.test.ts
import { screen } from '@testing-library/dom';

describe('foo suite', () => {
  it('foo', async () => {
    console.log('foo');
    const button = document.createElement('button');
    document.body.appendChild(button);
    try {
      screen.debug();
      screen.getByRole('button');
    } finally {
      button.remove();
    }
  });
});

// ./bar.test.ts
import { screen } from '@testing-library/dom';

describe('bar suite', () => {
  it('bar', async () => {
    console.log('bar');
    const button = document.createElement('button');
    document.body.appendChild(button);
    try {
      screen.debug();
      screen.getByRole('button');
    } finally {
      button.remove();
    }
  });
});

What you did:

Run pnpm test run => works
Run pnpm test run -- --no-isolate --no-file-parallelism => one of the tests fail.

What happened:

One of the tests fails, it can't seem to locate the button, yet it's there when calling screen.debug()

bar
<body>
  <button />
</body>

/home/projects/vitest-dev-vitest-u1eg7t/test/suite.test.tsx:10:14
   8 |     document.body.appendChild(button);
   9 |     try {
> 10 |       screen.debug();
     |              ^


stdout | test/basic.test.tsx > foo suite > foo
foo
<body>
  <button />
</body>

/home/projects/vitest-dev-vitest-u1eg7t/test/basic.test.tsx:10:14
   8 |     document.body.appendChild(button);
   9 |     try {
> 10 |       screen.debug();
     |              ^


 ❯ test/basic.test.tsx (1)
   ❯ foo suite (1)
     Γ— foo
 βœ“ test/suite.test.tsx (1)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/basic.test.tsx > foo suite > foo
TestingLibraryElementError: Unable to find an accessible element with the role "button"

There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the `hidden` option to `true`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole

Ignored nodes: comments, script, style

Reproduction:

Stackblitz with reproduction: https://stackblitz.com/edit/vitest-dev-vitest-9xcyxz?file=test%2Fbar.test.ts

You can run pnpm test run and pnpm test run -- --no-isolate --no-file-parallelism to see the bug in action.

Problem description:

Migrating from mocha to vitest, trying to reach similar performance by disabling isolation

Suggested solution:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions