Skip to content

Instantly share code, notes, and snippets.

@Cerberus
Created May 27, 2022 05:23
Show Gist options
  • Save Cerberus/ae25db5417c2156f141e3fa2c17005fd to your computer and use it in GitHub Desktop.
Save Cerberus/ae25db5417c2156f141e3fa2c17005fd to your computer and use it in GitHub Desktop.
Component Testing (+ React 18) Mount via JSX
import ReactDOM from 'react-dom/client'
import { mount } from '@cypress/react'
import { ReactNode } from 'react'
export const open = (jsx: ReactNode) => {
let myRoot: ReactDOM.Root
mount(<div />, {
ReactDom: {
// @ts-ignore ignore return type
render: (_: any, rootEl: Element | null) => {
const root = ReactDOM.createRoot(rootEl!)
myRoot = root
root.render(jsx)
},
unmountComponentAtNode: () => {
// NOTE: Clear client-store & localStorage here to prevent side-effect to next testing
myRoot.unmount()
return true
},
},
})
}
@Cerberus
Copy link
Author

On test file

import { open } from './ct-open-by-url'
describe('Home', () => {
	it('Simple', () => {
		open(<Component />)
	})
})

@Cerberus
Copy link
Author

pkg:

react: 18.1.0
@cypress/react: 5.12.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment