Simple and complete DOM testing utilities that encourage good testing practices.
—
Inspect and debug DOM state during tests with formatted output and Testing Playground integration.
Get formatted string representation of the DOM.
function prettyDOM(
dom?: Element | HTMLDocument,
maxLength?: number,
options?: PrettyDOMOptions
): string | false;
interface PrettyDOMOptions {
filterNode?: (node: Node) => boolean;
// Also includes all pretty-format options
}Usage:
import {prettyDOM, screen} from '@testing-library/dom';
// Pretty print entire body
console.log(prettyDOM());
// Specific element
const form = screen.getByRole('form');
console.log(prettyDOM(form));
// With custom filter
const output = prettyDOM(document.body, 10000, {
filterNode: (node) => node.nodeType === 1 // Only elements
});
// In test assertions
const button = screen.queryByRole('button');
if (!button) {
console.log('Current DOM:', prettyDOM());
throw new Error('Button not found');
}Log formatted DOM directly to console.
function logDOM(
dom?: Element | HTMLDocument,
maxLength?: number,
options?: PrettyDOMOptions
): void;Usage:
import {logDOM} from '@testing-library/dom';
// Log entire body
logDOM();
// Specific element
const modal = screen.getByRole('dialog');
logDOM(modal);
// With filter
logDOM(document.body, 10000, {
filterNode: (node) => node.nodeType === 1
});Screen object includes debug method.
interface Screen {
debug(
element?: Element | HTMLDocument | Array<Element | HTMLDocument>,
maxLength?: number,
options?: PrettyDOMOptions
): void;
}Usage:
import {screen} from '@testing-library/dom';
// Debug entire document
screen.debug();
// Debug specific element
const form = screen.getByRole('form');
screen.debug(form);
// Debug multiple elements
const buttons = screen.getAllByRole('button');
screen.debug(buttons);
// In test
test('form validation', () => {
screen.debug(); // See initial state
fireEvent.submit(form);
screen.debug(); // See state after submit
});Get URL to Testing Playground for query suggestions.
interface Screen {
logTestingPlaygroundURL(element?: Element | HTMLDocument): string;
}Usage:
// Get URL for entire document
const url = screen.logTestingPlaygroundURL();
console.log('Open in browser:', url);
// For specific element
const container = screen.getByTestId('app');
const url = screen.logTestingPlaygroundURL(container);test('finds button', () => {
try {
screen.getByRole('button', {name: /submit/i});
} catch (error) {
screen.debug(); // Log current DOM
throw error;
}
});const input = screen.getByLabelText('Email');
console.log('Initial state:');
screen.debug(input);
fireEvent.change(input, {target: {value: 'test@example.com'}});
console.log('After change:');
screen.debug(input);fireEvent.click(screen.getByRole('button', {name: /load/i}));
console.log('After click:');
screen.debug();
await waitFor(() => {
expect(screen.getByText('Loaded')).toBeInTheDocument();
});
console.log('After load:');
screen.debug();function debugWithTime(element) {
console.log(`[${new Date().toISOString()}]`);
console.log(prettyDOM(element));
}Install with Tessl CLI
npx tessl i tessl/npm-testing-library--dom