-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Describe the bug
I'm using SvelteKit with Vitest. I updated SvelteKit and Vitest to their latest versions - 1.5.0 and 0.28.4 respectfully. However, updating Vite from 4.0.4 to 4.1.0 caused 15% of my tests to fail (60/400).
Most of the errors seem unreasonable. For example, removing a DOM element throws a Failed to execute removeChild on Node
error. Or Found multiple elements with the text: ...
where there's just a single element containing that exact copy; Error: Unable to fire a "click" event - please provide a DOM element.
, Error: unable to find element with text
, etc.
Reproduction
Here's a simple StackBlitz example.
- Open the link
- Wait for the installation to complete
- Stop the execution in the terminal (Cmd + C)
- Run
npm run test
- The test fails
Then do the following:
- Open
package.json
- Change the version of
vite
from4.1.0
to4.0.4
- Update the dependencies
- Open the terminal again and run
npm run test
- The test succeeds
The example explained
It's a simple component with an if statement:
<script lang="ts">
import { onMount } from "svelte"
let showMessage = false
onMount(() => {
showMessage = true
})
</script>
{#if showMessage}
<div>Hello</div>
{/if}
Test file:
import { render } from "@testing-library/svelte"
import Hello from "../Hello.svelte"
describe("Hello component", () => {
it("should show message", async () => {
const { getByText } = render(Hello)
expect(getByText("Hello")).toBeInTheDocument()
})
})
Result with Vite 4.1.0:
TestingLibraryElementError: Unable to find an element with the text: Hello. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
This is just a single example but similar issues occur where there's some sort of an asynchronous code used (or conditionals), like promises, setTimeout
, useFakeTimers()
and advanceTimersByTime()
, TestingLIbrary's waitFor()
function, etc.
More dependencies & config
See StackBlitz example for more info
package.json
"@sveltejs/kit": "^1.5.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/svelte": "^3.2.2",
"jsdom": "^21.1.0",
vite.config.ts
export default defineConfig({
plugins: [sveltekit()],
test: {
include: ['src/**/*.{test,spec}.{js,ts}'],
globals: true,
environment: "jsdom",
setupFiles: ["src/setupTest.js"],
}
});
setupTest.js
import "@testing-library/jest-dom"
System Info
System:
OS: macOS 13.1
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 1.84 GB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 19.2.0 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 8.13.1
Browsers:
Brave Browser: 109.1.47.186
Chrome: 109.0.5414.119
Firefox: 105.0.1
Safari: 16.2
npmPackages:
vite: 4.1.0 => 4.1.0
vitest: ^0.28.4 => 0.28.4
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status