Skip to content

Instantly share code, notes, and snippets.

@sidouglas
Last active December 16, 2022 04:27
Show Gist options
  • Save sidouglas/3505fbeda6b5d8924496746366252bfe to your computer and use it in GitHub Desktop.
Save sidouglas/3505fbeda6b5d8924496746366252bfe to your computer and use it in GitHub Desktop.
React Testing Library Dump Component html to a file
// dump to your desktop the contents of a component at a point in time
// htmlDump(component);
function htmlDump(component: RenderResult) {
const fs = require('fs');
const path = require('path');
const os = require('os');
const rtl = require('react-testing-library');
const dir = os.homedir() + '/Desktop/htmlDump';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const scriptName = path.basename(__filename) + '.' + Date.now() + '.html';
const data = rtl.prettyDOM(component.baseElement, undefined, {
highlight: false
})!;
fs.writeFile(`${dir}/${scriptName}`, data, { encoding: 'utf-8' }, err => {
err && console.log(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment