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

cleanup is not automatically called after each test #1331

Closed
Stadly opened this issue Sep 28, 2023 · 4 comments
Closed

cleanup is not automatically called after each test #1331

Stadly opened this issue Sep 28, 2023 · 4 comments

Comments

@Stadly
Copy link

Stadly commented Sep 28, 2023

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

@kyle-n
Copy link
Contributor

kyle-n commented Oct 29, 2023

I ran into this issue as well. In order to avoid it, set globals: true in your vite.config.ts test property.

The code checks if there is a globally available afterEach() function before calling it with cleanup().

The setup documentation includes globals: true, but it's very easy to miss. If the library is going to require this flag in order to work, the documentation should highlight it.

@MatanBobi
Copy link
Member

Since it's not a code issue, I'm transferring this to the docs repo :)

@MatanBobi MatanBobi transferred this issue from testing-library/svelte-testing-library Oct 30, 2023
@mcous
Copy link
Contributor

mcous commented Feb 13, 2024

@timdeschryver little heads up: I think this issue can be closed! It was resolved by #1360. Vitest setup instructions now include:

  1. Add a vitest-setup.js file to optionally set up automatic post-test cleanup and @testing-library/jest-dom expect matchers.
import '@testing-library/svelte/vitest'
import '@testing-library/jest-dom/vitest'

I'm planning on doing an api.mdx pass sometime this week, so if you'd prefer to keep this issue open until some of that copy can be cleaned up, that works for me, too!

@MatanBobi
Copy link
Member

Thanks @mcous :)

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

4 participants