Skip to content

Make jsdom elements look like real DOM elements in Chrome Devtools console

Notifications You must be signed in to change notification settings

jsdom/jsdom-devtools-formatter

Repository files navigation

jsdom-devtools-formatter Build Status

In a nutshell: Instead of trying to understand what jsdom's elements represents by inspecting their implementation objects like so: before.png

…let's just inspect them like they were real HTML elements: after.png

Typical use-case would be some script/test that utilizes jsdom in a Node.js environment, e.g. Jest.

How to use

npm install jsdom-devtools-formatter
// in some file.js
const jsdomDevtoolsFormatter = require('jsdom-devtools-formatter');
jsdomDevtoolsFormatter.install();
// You can also opt-out at some later point by:
jsdomDevtoolsFormatter.uninstall();

E.g. for Jest you need to install the formatter in a setupFilesAfterEnv configuration file.

As a one-time thing also need to:

  • Open Chrome's Devtools
  • Click the "⠇" in the upper-right corner > Settings
  • Under "Console", check "Enable custom formatters"

Development

The source code is all plain vanilla JS and standard CommonJS modules. Tests are written using Jest

See package.json's scripts sections for all available commands. The most useul ones are probably:

Automated Tests

# run all tests once:
npm test

# run tests in "watch mode"
npm test -- --watch

Manual Testing

In addition to verifying logical changes using the automated tests, it's recommended to verify that things "look & feel" as expected using the manual tests:

  • Open chrome://inspect and click the "Open dedicated DevTools for Node" link
  • Then run:
npm run test:manual_node
# -or-
npm run test:manual_jest

It should stop at the debugger call site, from there you can follow the inlined comment with instructions to play with the console output.

Related resources