Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created February 7, 2023 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/52f90ca4c84ebfaf706aa476da99c476 to your computer and use it in GitHub Desktop.
Save sibelius/52f90ca4c84ebfaf706aa476da99c476 to your computer and use it in GitHub Desktop.
ssr test html
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();
});
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