Skip to content

Commit

Permalink
tests: migrate to happy-dom and yarn pnpm mode
Browse files Browse the repository at this point in the history
  • Loading branch information
khmm12 committed Oct 14, 2023
1 parent 26c4e69 commit 5c26e3d
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 345 deletions.
2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
enableGlobalCache: true

nodeLinker: pnp
nodeLinker: pnpm

packageExtensions:
"@types/testing-library__user-event@*":
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@testing-library/jest-dom": "^6.1.4",
"@testing-library/user-event": "^14.5.1",
"@types/chrome": "^0.0.246",
"@types/eslint": "^8.44.4",
"@types/node": "^18.18.5",
"@types/testing-library__user-event": "^4.2.0",
"@typescript-eslint/eslint-plugin": "^6.7.5",
Expand All @@ -53,8 +54,7 @@
"eslint-plugin-n": "^16.2.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-promise": "^6.1.1",
"jsdom": "^22.1.0",
"mq-polyfill": "^1.1.8",
"happy-dom": "^12.9.1",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.3",
"stylis": "^3",
Expand Down
4 changes: 2 additions & 2 deletions src/components/Modal/Modal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fireEvent, render, renderHook, screen } from '@test/helpers/solid'
import { fireEvent, render, renderHook, screen, waitForElementToBeRemoved } from '@test/helpers/solid'
import { createSignal } from 'solid-js'
import ShowWithTransition from '@/components/ShowWithTransition'
import Modal from '.'
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('Modal', () => {

setShouldOpen(false)

expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
await waitForElementToBeRemoved(() => screen.queryByRole('dialog'))
})

it('closes on outside click', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ export default function Modal(props: ModalProps): JSX.Element {
const transition = useContext(ShowWithTransitionContext)

createEffect(() => {
// Manually call if by some reasons animations are not supported
if (typeof AnimationEvent === 'undefined' && !transition.isOpened) transition.onAfterExit()
// Emulate transition end event in test environment
if (import.meta.env.TEST && !transition.isOpened)
setTimeout(() => {
transition.onAfterExit()
}, 0)
}, transition.isOpened)

useDialogHooks({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports[`SettingsButton > renders a button 1`] = `
<svg
aria-hidden="true"
class="svg_s1y7swdn"
viewBox="0 0 128 128"
viewbox="0 0 128 128"
xmlns="http://www.w3.org/2000/svg"
>
<g
Expand Down
2 changes: 1 addition & 1 deletion src/components/SettingsDialog/SettingsDialog.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, renderHook, screen, waitFor, waitForElementToBeRemoved } from '@test/helpers/solid'
import userEvent from '@testing-library/user-event'
import { format } from 'date-fns'
import { Suspense } from 'solid-js'
import userEvent from '@testing-library/user-event'
import createSettingsStorage, { type Settings } from '@/hooks/createSettingsStorage'
import toISODate from '@/utils/to-iso-date'
import SettingsDialog, { type SettingsDialogProps } from '.'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createComputed, createEffect, type JSX, on } from 'solid-js'
import { createForm } from '@felte/solid'
import { createComputed, createEffect, type JSX, on } from 'solid-js'
import type { Settings } from '@/hooks/createSettingsStorage'
import createUniqueIds from '@/hooks/createUniqueIds'
import toISODate from '@/utils/to-iso-date'
Expand Down
25 changes: 2 additions & 23 deletions test-support/extensions/window.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
import matchMediaPolyfill from 'mq-polyfill'

if (window.matchMedia == null) {
matchMediaPolyfill(window)

// mq-polyfill still doesn't support addEventListener/removeEventListener

window.MediaQueryList.prototype.addEventListener = function (this: MediaQueryList, _type, listener) {
Reflect.apply(this.addListener, this, [listener])
}

window.MediaQueryList.prototype.removeEventListener = function (this: MediaQueryList, _type, listener) {
Reflect.apply(this.removeListener, this, [listener])
}
}
import { type Window } from 'happy-dom'

window.resizeTo = function resizeTo(width, height) {
Object.assign(window, {
innerWidth: width,
innerHeight: height,
outerWidth: width,
outerHeight: height,
})

window.dispatchEvent(new window.Event('resize'))
;(window as unknown as Window).happyDOM.setWindowSize({ width, height })
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"types": ["chrome", "vite/client"]
},
"include": ["typings", "src", "test-support/env.d.ts"],
"include": ["typings", "src", "test-support"],
"exclude": ["./dist"],
"references": [
{ "path": "./tsconfig.node.json" }
Expand Down
9 changes: 3 additions & 6 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { fileURLToPath } from 'node:url'
import linaria from '@linaria/vite'
import browserslistToEsbuild from 'browserslist-to-esbuild'
import Stylis from 'stylis'
import { configDefaults } from 'vitest/config'
import linaria from '@linaria/vite'
import { defineConfig } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'
import solidPlugin from 'vite-plugin-solid'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import { configDefaults } from 'vitest/config'
import manifestPlugin from './lib/vite/manifest-plugin'

export default defineConfig((config) => ({
Expand Down Expand Up @@ -45,10 +45,7 @@ export default defineConfig((config) => ({
setupFiles: ['test-support/setup.ts'],
watch: false,
globals: true,
environment: 'jsdom',
transformMode: {
ssr: [],
},
environment: 'happy-dom',
deps: {
// We need browser variants
inline: [/solid-js/, /@solidjs\/testing-library/, /@felte\/solid/, /solid-transition-group/, /@solid-primitives/],
Expand Down
Loading

0 comments on commit 5c26e3d

Please sign in to comment.