CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-testing-library--dom

Simple and complete DOM testing utilities that encourage good testing practices.

Pending
Overview
Eval results
Files

debugging.mddocs/

Debugging Utilities

Inspect and debug DOM state during tests with formatted output and Testing Playground integration.

prettyDOM

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');
}

logDOM

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.debug

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
});

logTestingPlaygroundURL

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);

Common Patterns

Debugging Query Failures

test('finds button', () => {
  try {
    screen.getByRole('button', {name: /submit/i});
  } catch (error) {
    screen.debug();  // Log current DOM
    throw error;
  }
});

Debugging Element State

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);

Debugging Async Updates

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();

Custom Debug Output

function debugWithTime(element) {
  console.log(`[${new Date().toISOString()}]`);
  console.log(prettyDOM(element));
}

Install with Tessl CLI

npx tessl i tessl/npm-testing-library--dom@10.4.2

docs

async.md

config.md

debugging.md

events.md

index.md

queries.md

query-helpers.md

role-utilities.md

screen.md

within.md

tile.json