Created
February 7, 2023 15:57
-
-
Save sibelius/52f90ca4c84ebfaf706aa476da99c476 to your computer and use it in GitHub Desktop.
ssr test html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it('test SSR', async () => { | |
const response = await request(app.callback()).get('/').send(); | |
expect(response.status).toBe(200); | |
const { getByText } = renderHtml(response.text); | |
expect(getByText('my data')).toBeTruthy(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { prettyDOM, within } from '@testing-library/react'; | |
import { JSDOM } from 'jsdom'; | |
export const renderHtml = (html: string) => { | |
const { | |
window: { document }, | |
} = new JSDOM(); | |
const container = JSDOM.fragment(html); | |
document.adoptNode(container); | |
(container as any).outerHTML = html; // Fixes prettyDOM for container | |
return { | |
document, | |
container, | |
debug(el?: ParentNode) { | |
if (el) { | |
// eslint-disable-next-line | |
console.log(prettyDOM(el as HTMLElement)); | |
} else { | |
// eslint-disable-next-line | |
console.log( | |
...[].map.call(container.children, (child: HTMLElement) => | |
prettyDOM(child), | |
), | |
); | |
} | |
}, | |
...within(container), | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment