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

fix: use happy-dom instead of jsdom in tests #286

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"@commitlint/cli": "^17.6.6",
"@commitlint/config-conventional": "^17.6.6",
"@sveltejs/vite-plugin-svelte": "^2.4.2",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/jest-dom": "^6.2.1",
"@testing-library/user-event": "^14.5.2",
"@vitest/coverage-c8": "^0.33.0",
"all-contributors-cli": "^6.26.0",
"doctoc": "^2.2.1",
Expand All @@ -72,8 +73,8 @@
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-svelte": "^2.32.0",
"eslint-plugin-vitest-globals": "^1.3.1",
"happy-dom": "^13.2.1",
"husky": "^8.0.3",
"jsdom": "^22.1.0",
"lint-staged": "^13.2.3",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.0",
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/NonBound.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
// The text content of the paragraph is purposely not bound
// with any svelte variable. The purpose of this exercise
// is to reduce to the simplest form my use-case which is
// to test changes done in html elements of a svelte component
// by external helper class methods, which do not allow for
// bindings to exist.
const modify = () => {
const par = document.querySelector(".info");
par.innerText = "Modified by click";
};
</script>

<div>
<p class="info" data-testid="info">Click to modify</p>
<button on:click={modify} label="button">Modify</button>
</div>
17 changes: 17 additions & 0 deletions src/__tests__/gh119.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import userEvent from '@testing-library/user-event'
import { expect, test } from 'vitest'

import { render, screen, waitFor } from '../pure.js'
import Component from './fixtures/NonBound.svelte'

// fails with jsdom, but work with happy-dom

test('should modify the text after clicking the button', async () => {
render(Component)
const button = screen.getByRole('button')
userEvent.click(button)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be an await on userEvent.click, since it's an async method?

const info = screen.getByTestId('info')

// The test fails independently of using waitFor or not.
await waitFor(() => expect(info).toHaveTextContent('Modified by click'))
})
2 changes: 1 addition & 1 deletion src/__tests__/rerender.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @jest-environment jsdom
* @jest-environment happy-dom
*/
import { describe, expect, test } from 'vitest'

Expand Down
7 changes: 3 additions & 4 deletions src/test-setup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as matchers from '@testing-library/jest-dom/dist/matchers'
import { afterEach, expect } from 'vitest'
import '@testing-library/jest-dom/vitest'

import { act, cleanup } from './pure.js'
import { afterEach } from 'vitest'

expect.extend(matchers)
import { act, cleanup } from './pure.js'

afterEach(async () => {
await act()
Expand Down
10 changes: 5 additions & 5 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()],
test: {
environment: 'jsdom',
setupFiles: ['./src/test-setup.js'],
},
plugins: [svelte()],
test: {
environment: 'happy-dom',
setupFiles: ['./src/test-setup.js'],
},
})
Loading