npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@angular-extensions/pretty-html-log

v5.1.1

Published

**Improved debugging of Angular component tests with Jest!**

Downloads

131,542

Readme

@angular-extensions/pretty-html-log

Improved debugging of Angular component tests with Jest!

The @angular-extension/pretty-html-log is a module that makes debugging component tests with Jest a breeze. It adds a phl method which pretty prints the innerHTML of a ComponentFixture, a DebugElement, a NativeElement or an HTML string.

logNgHTML

Why you should use this module

When debugging component tests, it’s often necessary to inspect the DOM. The most common approach to do so is by using the good old console.log which has some downsides.

First of all, it’s annoying to always type

fixture.debugElement.nativeElement.innerHTML;

Moreover, the console.log statement doesn’t print the HTML in a very readable way. Therefore we still need to copy the string in a new HTML file and format it to be able to inspect it. Not with @angular-extensions/pretty-html-log!

logNgHTML

Features

  • Provides a phl method that highlights and pretty prints a fixture, debugElement, nativeElement or even a plain HTML string - you don't have to worry how to get to the HTML, just pass the thing you want to print to the phl method.
  • in case you are using prettier (which you should ;)), pretty-html-log will pick up your prettier config and pretty print the HTML string based on your prettier configuration. 🤩

Getting started

Installation

This module will only be used during development and can therefore be installed as a dev dependency.

npm i -D @angular-extensions/pretty-html-log

Usage

pretty-html-log can be used in two different ways. The first and most simple way is to simply import the phl function into your test.

Usage via import

The @angular-extensions/pretty-html-log package provides a phl method that you can use to pretty print a fixture, debugElement, nativeElement or even a plain HTML string. Simply import it while debugging and pretty print that HTML.

import { phl } from '@angular-extensions/pretty-html-log';

describe('my test suite', () => {
  it('should be green', () => {
    phl(fixture); // This will pretty print the fixture
    expect(myTable.getRows()).toBe(5);
  });
});

Importing the phl function is straightforward. However, since phl is often used as a temporary tool, it can be bothersome to add an import at the top of the file. This is easy to forget and requires manual cleanup afterward.

Wouldn't it be cool if we can simply use the phl function without the need of an import. Let's check out how to setup the phl function globally.

To make sure unused import statement get cleaned up we should configure our eslint to clean up unused imports. More: https://www.npmjs.com/package/eslint-plugin-unused-imports.

Usage via globals

To make phl globally available, import and run the following method within a jest.setup.ts file.

import { setupPhl } from '@angular-extensions/pretty-html-log';

setupPhl();

API

The phl method has the following signature:

<T>(
  ngHTMLElement: NgHTMLElement<T>,
  enableComments = false,
  theme = THEMES.DRACULA
)

| Property | Description | | ------------------------------- | ------------------------------------------------------------------------------------------------------------------ | | ngHTMLElement | Value to extract the html from and pretty print it to the console: ComponentFixture | DebugElement | DebugElement[] | HTMLElement | HTMLElement[] | string; | | enableComments (default: false) | When set to true we print the generated comments by Angular. For example: <!--bindings={"ng-reflect-ng-for-of":... | | theme: (default: DRACULA) | pretty-html-log themes (DRACULA, VSCODE and MATERIAL) |

Examples

Pass in specific DebugElement

In your test you can simply write the following line.

phl(fixture.debugElement.query(By.css('mat-tab-body')));

Which will print the following string to your console. Depending on your test configuration you might run into an issue with the patch of the console. In such cases its best to report an issue and use the logNgHTML function directly.

phl(fixture.debugElement.query(By.css('mat-tab-body')));

logNgHTML

Examples

Log the content innerHTML of a fixture

phl(fixture);

of a debugElement (or multiple debugElements)

phl(fixture.debugElement);

of a nativeElement (or multiple nativeElements)

phl(fixture.debugElement.nativeElement);

or even a simple HTML string

phl('<h1>Foo</h1>');

Print Angular comments

Angular adds some comments to our HTML file. Usually, when debugging our tests, we don't need them. Therefore they are not printed by default. However, there are cases where you want to print those comments. To do so, you can pass true as an additional flag tot he logNgHTML method.

phl(fixture, true);

Change the theme

@angular-extensions/pretty-html-log allows you to print the html logs in different themes. Currently, we support (DRACULA, VSCODE and MATERIAL). The themes can be importet from pretty-html-log, the base library @angular-extensions/pretty-html-log depends on.

import { THEMES } from 'pretty-html-log';

console.logNgHTML(fixture, false, THEMES.VSCODE);

Further resources

Blog post

Improved debugging of Angular component tests in Jest is a write up on AngularInDepth that shows how @angular-extensions/pretty-html-log is used and set up in a project.

Video tutorial

IMAGE ALT TEXT HERE