Skip to content

cleanup is not automatically called after each test #1331

Closed
@Stadly

Description

@Stadly

The documentation says that cleanup is done automatically.

I have a different experience, however. This test passes:

it('should render page', () => {
	const { getByRole } = render(Page);
	expect(getByRole("heading", { level: 1 }).textContent).toBe("Welcome to SvelteKit");
});

If I add another test after it, that one fails:

it('should have been cleaned up before next test is run', () => {
	const { getByRole } = render(Page);
	expect(getByRole("heading", { level: 1 }).textContent).toBe("Welcome to SvelteKit");
});

The error is:

TestingLibraryElementError: Found multiple elements with the role "heading"

Here are the matching elements:

Ignored nodes: comments, script, style
<h1>
  Welcome to SvelteKit
</h1>

Ignored nodes: comments, script, style
<h1>
  Welcome to SvelteKit
</h1>

(If this is intentional, then use the `*AllBy*` variant of the query (like `queryAllByText`, `getAllByText`, or `findAllByText`)).

If I add this to the top of my file, both tests pass:

import { cleanup } from "@testing-library/svelte";
import { afterEach } from "vitest";
afterEach(() => {
  cleanup();
});

Here is a small repo showcasing the issue: https://github.com/Stadly/reproduction/tree/testing-library/no-cleanup-with-svelte

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