Skip to content

Commit

Permalink
use happy-dom instead of jsdom
Browse files Browse the repository at this point in the history
Takes care of the lack of reactivity seen in #119
  • Loading branch information
yanick committed Jan 23, 2024
1 parent c0ff791 commit c83597e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
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>
19 changes: 19 additions & 0 deletions src/__tests__/gh119.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test, expect } from 'vitest'

Check failure on line 2 in src/__tests__/gh119.test.js

View workflow job for this annotation

GitHub Actions / main (16)

'tick' is defined but never used

Check failure on line 2 in src/__tests__/gh119.test.js

View workflow job for this annotation

GitHub Actions / main (18)

'tick' is defined but never used
import { tick } from 'svelte'
import userEvent from '@testing-library/user-event'
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)
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
4 changes: 1 addition & 3 deletions src/test-setup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as matchers from '@testing-library/jest-dom/dist/matchers'
import '@testing-library/jest-dom/vitest'
import { afterEach, expect } from 'vitest'

Check failure on line 3 in src/test-setup.js

View workflow job for this annotation

GitHub Actions / main (16)

'expect' is defined but never used

Check failure on line 3 in src/test-setup.js

View workflow job for this annotation

GitHub Actions / main (18)

'expect' is defined but never used
import { act, cleanup } from './pure.js'

expect.extend(matchers)

afterEach(async () => {
await act()
cleanup()
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'],
},
})

0 comments on commit c83597e

Please sign in to comment.