@angular-extensions/pretty-html-log

中文文档
5.1.1发布于 2024/07/13MITTypeScriptES Module近 30 天下载 53.7万

Improved debugging of Angular component tests with Jest!

原始使用文档
文档目录14 节
  1. Why you should use this module
  2. Features
  3. Getting started
  4. Installation
  5. Usage
  6. API
  7. Examples
  8. Pass in specific DebugElement
  9. Examples
  10. Print Angular comments
  11. Change the theme
  12. Further resources
  13. Blog post
  14. Video tutorial

@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
)
PropertyDescription
ngHTMLElementValue to extract the html from and pretty print it to the console: ComponentFixture
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

版本与发布记录

@angular-extensions/pretty-html-log 共发布 17 个版本,首次发布于 2019/11/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.1.1latest2024/07/132 年前
5.1.02024/07/132 年前
5.0.22022/10/044 年前
5.0.12022/01/124 年前
5.0.02022/01/044 年前
4.0.12021/03/175 年前

关于 @angular-extensions/pretty-html-log 的常见问题

@angular-extensions/pretty-html-log 是什么?

Improved debugging of Angular component tests with Jest!

如何安装 @angular-extensions/pretty-html-log?

在项目目录运行 npm install @angular-extensions/pretty-html-log,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(>=9)、@angular/core(>=9)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@angular-extensions/pretty-html-log 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@angular-extensions/pretty-html-log 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@angular-extensions/pretty-html-log 可以商用吗?

@angular-extensions/pretty-html-log 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@angular-extensions/pretty-html-log 的下载量有多少?

根据 npm 官方下载统计,@angular-extensions/pretty-html-log 在 2026/10/06 – 2026/11/04 这 30 天内被下载 537,147 次,日均约 17,905 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@angular-extensions/pretty-html-log 还在维护吗?

最新版本 5.1.1 发布于 2024/07/13(2 年前);至今共发布 17 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/11/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。