Skip to content

Commit bf5f393

Browse files
committed
feat: 🎸 add phl method
1 parent 2c65eae commit bf5f393

File tree

6 files changed

+85
-66
lines changed

6 files changed

+85
-66
lines changed

README.md

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# @angular-extensions/pretty-html-log
22

3-
> Improved debugging of Angular component tests.
3+
> **Improved debugging of Angular component tests with Jest!**
4+
>
45
> The @angular-extension/pretty-html-log is a module that makes debugging component tests with Jest a breeze.
5-
> It adds a console.logNgHTML method which pretty prints the innerHTML of a ComponentFixture, a DebugElement, a NativeElement or an HTML string.
6+
> It adds a `phl` method which pretty prints the _innerHTML_ of a `ComponentFixture`, a `DebugElement`, a `NativeElement` or an HTML string.
67
78
![logNgHTML](https://raw.githubusercontent.com/angular-extensions/pretty-html-log/master/images/logo.png)
89

@@ -12,64 +13,100 @@
1213
- [Why you should use this module](#why-you-should-use-this-module)
1314
- [Features](#features)
1415
- [Getting started](#getting-started)
16+
- [Installation](#installation)
17+
- [Usage with an import](#usage-with-an-import)
18+
- [Provide phl as a Jest global](#provide-phl-as-a-jest-global)
1519
- [API](#api)
1620
- [Examples](#examples)
1721
- [Pass in specific DebugElement](#pass-in-specific-debugelement)
1822
- [Blog post](#blog-post)
1923
- [Examples](#examples-1)
2024
- [Print Angular comments](#print-angular-comments)
2125
- [Change the theme](#change-the-theme)
22-
- [FAQ](#faq)
2326

2427
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2528

2629
## Why you should use this module
2730

2831
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.
29-
First of all, it’s annoying always to type
32+
33+
First of all, it’s annoying to always type
3034

3135
```typescript
3236
fixture.debugElement.nativeElement.innerHTML;
3337
```
3438

35-
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`
39+
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**!
3640

3741
![logNgHTML](https://raw.githubusercontent.com/angular-extensions/pretty-html-log/master/images/before-after.png)
3842

3943
## Features
4044

41-
- patches the console and adds a new method `console.logNgHTML`
42-
- pretty prints a fixture, debugElement, nativeElement or plain HTML string - you don't have to worry
43-
how to get to the HTML, just pass the thing you want to print to the `console.logNgHTML` method.
44-
- highlights the HTML
45+
- 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.
4546
- in case you are using prettier (which you should ;)), pretty-html-log will pick
4647
up your prettier config and pretty print the HTML string based on your prettier configuration. 🤩
4748

4849
## Getting started
4950

50-
This module will only be used during development and can therefore
51-
be installed as a dev dependency.
51+
### Installation
52+
53+
This module will only be used during development and can therefore be installed as a dev dependency.
5254

5355
```
5456
npm i -D @angular-extensions/pretty-html-log
5557
```
5658

57-
This module is best used with Angular and Jest. Create a
58-
`setupJest.ts` file in your `src` directory and add the following line **after your jest-preset-angular import. ⚠️ The order can matter**:
59+
### Usage with an import
60+
61+
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.
5962

6063
```typescript
61-
import '@angular-extensions/pretty-html-log';
64+
import { phl } from '@angular-extensions/pretty-html-log';
65+
66+
describe('my test suite', () => {
67+
it('should be green', () => {
68+
phl(fixture); // This will pretty print the fixture
69+
expect(myTable.getRows()).toBe(5);
70+
});
71+
});
6272
```
6373

64-
This import adds a `logNgHTML` method to your console. You can then
65-
use this method during tests to pretty print `ComponentFixtures`,
66-
`DebugElements`, `NativeElements` or even plain HTML `strings` .
74+
> Note that this way adds a import method. To make sure this import statement gets cleaned up we should configure our eslint to clean up unused imports. More: https://www.npmjs.com/package/eslint-plugin-unused-imports.
75+
76+
### Provide phl as a Jest global
77+
78+
Maybe you don't want to use a plugin that cleans up unused imports or maybe this import statement just annoys you. If that's the case, you have to option to provide the `phl` method as a Jest global. Similar to `it`, `describe` or `expect`.
79+
80+
1. rename you jest config from `jest.config.js` to `jest.config.mjs`. Using the `.mjs` extension allows us to use ES Modules inside our Jest config. Jest officially supports `.mjs` configuration files.
81+
82+
2. Import `phl` from `@angular-extensions/pretty-html-log` and provide it as a global inside your `jest.config.mjs`:
83+
84+
```json
85+
import {phl} from "@angular-extensions/pretty-html-log";
86+
87+
module.exports = {
88+
globals: {
89+
phl
90+
}
91+
};
92+
```
93+
94+
3. Start using it inside your tests without the usage of import 🤩
95+
96+
```typescript
97+
describe('my test suite', () => {
98+
it('should be green', () => {
99+
phl(fixture); // This will pretty print the fixture
100+
expect(myTable.getRows()).toBe(5);
101+
});
102+
});
103+
```
67104

68105
## API
69106

70-
The `console.logNgHTML()` method has the following signature:
107+
The `phl` method has the following signature:
71108

72-
```
109+
```typescript
73110
<T>(
74111
ngHTMLElement: NgHTMLElement<T>,
75112
enableComments = false,
@@ -90,14 +127,14 @@ The `console.logNgHTML()` method has the following signature:
90127
In your test you can simply write the following line.
91128

92129
```typescript
93-
console.logNgHTML(fixture.debugElement.query(By.css('mat-tab-body')));
130+
phl(fixture.debugElement.query(By.css('mat-tab-body')));
94131
```
95132

96133
Which will print the following string to your console. Depending on your test configuration you
97134
might run into an issue with the patch of the console. In such cases its best to report an [issue](https://github.com/angular-extensions/pretty-html-log/issues) and use the `logNgHTML` function directly.
98135

99136
```typescript
100-
logNgHTML(fixture.debugElement.query(By.css('mat-tab-body')));
137+
phl(fixture.debugElement.query(By.css('mat-tab-body')));
101138
```
102139

103140
![logNgHTML](https://raw.githubusercontent.com/angular-extensions/pretty-html-log/master/images/output.png)
@@ -111,25 +148,25 @@ logNgHTML(fixture.debugElement.query(By.css('mat-tab-body')));
111148
Log the content innerHTML of a fixture
112149

113150
```typescript
114-
console.logNgHTML(fixture);
151+
phl(fixture);
115152
```
116153

117154
of a debugElement (or multiple debugElements)
118155

119156
```typescript
120-
console.logNgHTML(fixture.debugElement);
157+
phl(fixture.debugElement);
121158
```
122159

123160
of a nativeElement (or multiple nativeElements)
124161

125162
```typescript
126-
console.logNgHTML(fixture.debugElement.nativeElement);
163+
phl(fixture.debugElement.nativeElement);
127164
```
128165

129166
or even a simple HTML string
130167

131168
```typescript
132-
console.logNgHTML('<h1>Foo</h1>');
169+
phl('<h1>Foo</h1>');
133170
```
134171

135172
### Print Angular comments
@@ -139,34 +176,16 @@ are not printed by default. However, there are cases where you want to print tho
139176
can pass `true` as an additional flag tot he `logNgHTML` method.
140177

141178
```typescript
142-
console.logNgHTML(fixture, true);
179+
phl(fixture, true);
143180
```
144181

145182
### Change the theme
146183

147184
`@angular-extensions/pretty-html-log` allows you to print the html logs in different themes.
148-
Currently, we support (DRACULA, VSCODE and MATERIAL). The themes can be importet from `pretty-html-log`, the
149-
base library `@angular-extensions/pretty-html-log` depends on.
185+
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.
150186

151187
```typescript
152188
import { THEMES } from 'pretty-html-log';
153189

154190
console.logNgHTML(fixture, false, THEMES.VSCODE);
155191
```
156-
157-
# FAQ
158-
159-
I use the module but I don't get autocompletion when typing `console.logNgHTML`, furthermore I get the following error when I run my tests. `console.logNgHTML is not a function`. This is usually a sign that your `tsconfig.json` doesn't include the `setupJest.ts` file. Make sure that the `setupJest.ts` is included in your `tsconfig.json`.
160-
161-
```json
162-
{
163-
"extends": "../tsconfig.json",
164-
"compilerOptions": {
165-
"outDir": "./out-tsc/spec",
166-
"types": ["jest", "node"],
167-
"esModuleInterop": true
168-
},
169-
"files": ["polyfills.ts", "../jest.setup.ts"],
170-
"include": ["**/*.spec.ts", "**/*.d.ts"]
171-
}
172-
```

projects/pretty-html-log/src/lib/patchConsole.spec.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Theme } from 'pretty-html-log';
2+
3+
declare global {
4+
const phl: <T>(
5+
ngHTMLElement: any,
6+
enableComments?: boolean,
7+
theme?: Theme
8+
) => string;
9+
}

projects/pretty-html-log/src/lib/logNgHTML.spec.ts renamed to projects/pretty-html-log/src/lib/phl.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
import { Component, DebugElement } from '@angular/core';
88
import { By } from '@angular/platform-browser';
99
import * as prettyHTMLLog from 'pretty-html-log';
10-
import { highlight, THEMES } from 'pretty-html-log';
10+
import { THEMES } from 'pretty-html-log';
1111

12-
import { logNgHTML } from './logNgHTML';
12+
import { phl } from './phl';
1313
import * as fixturePrettier from './prettiers/fixture/pretty-fixture';
1414
import * as debugElementPrettier from './prettiers/debugElement/pretty-debugElement';
1515
import * as htmlElementPrettier from './prettiers/htmlElement/pretty-htmlelement';
@@ -27,7 +27,7 @@ import * as prettierUtil from './prettiers/prettier-util';
2727
})
2828
export class MockComponent {}
2929

30-
describe('LogNgHTML', () => {
30+
describe('phl', () => {
3131
let component: MockComponent;
3232
let fixture: ComponentFixture<MockComponent>;
3333
const theme = THEMES.DRACULA;
@@ -50,7 +50,7 @@ describe('LogNgHTML', () => {
5050

5151
it('should call prettyPrintFixture incase we provide a component fixture', () => {
5252
jest.spyOn(fixturePrettier, 'fixturePrettier');
53-
logNgHTML<MockComponent>(fixture, enableComments, theme);
53+
phl<MockComponent>(fixture, enableComments, theme);
5454
expect(fixturePrettier.fixturePrettier).toHaveBeenCalledWith(
5555
fixture,
5656
enableComments,
@@ -62,7 +62,7 @@ describe('LogNgHTML', () => {
6262
jest.spyOn(console, 'log');
6363
jest.spyOn(debugElementPrettier, 'prettyPrintDebugElements');
6464
const debugElements = fixture.debugElement.queryAll(By.css('li'));
65-
logNgHTML(debugElements, enableComments, theme);
65+
phl(debugElements, enableComments, theme);
6666
expect(debugElementPrettier.prettyPrintDebugElements).toHaveBeenCalledWith(
6767
debugElements,
6868
enableComments,
@@ -76,7 +76,7 @@ describe('LogNgHTML', () => {
7676
const htmlElements = fixture.debugElement
7777
.queryAll(By.css('li'))
7878
.map((debugElement: DebugElement) => debugElement.nativeElement);
79-
logNgHTML(htmlElements, enableComments, theme);
79+
phl(htmlElements, enableComments, theme);
8080
expect(htmlElementPrettier.prettyPrintHtmlElements).toHaveBeenCalledWith(
8181
htmlElements,
8282
enableComments,
@@ -87,7 +87,7 @@ describe('LogNgHTML', () => {
8787
it('should call prettyPrintDebugElement in case we provide a debug element', () => {
8888
jest.spyOn(debugElementPrettier, 'prettyPrintDebugElement');
8989
const debugElement = fixture.debugElement.queryAll(By.css('li'))[0];
90-
logNgHTML(debugElement, enableComments, theme);
90+
phl(debugElement, enableComments, theme);
9191
expect(debugElementPrettier.prettyPrintDebugElement).toHaveBeenCalledWith(
9292
debugElement,
9393
enableComments,
@@ -99,7 +99,7 @@ describe('LogNgHTML', () => {
9999
jest.spyOn(htmlElementPrettier, 'prettyPrintHtmlElement');
100100
const htmlElement = fixture.debugElement.queryAll(By.css('li'))[0]
101101
.nativeElement;
102-
logNgHTML(htmlElement, enableComments, theme);
102+
phl(htmlElement, enableComments, theme);
103103
expect(htmlElementPrettier.prettyPrintHtmlElement).toHaveBeenCalledWith(
104104
htmlElement,
105105
enableComments,
@@ -111,7 +111,7 @@ describe('LogNgHTML', () => {
111111
jest.spyOn(console, 'log');
112112
jest.spyOn(prettyHTMLLog, 'highlight');
113113
const htmlString = '<h1>Foo</h1>';
114-
logNgHTML(htmlString, enableComments, theme);
114+
phl(htmlString, enableComments, theme);
115115
expect(prettyHTMLLog.highlight).toHaveBeenCalledWith(htmlString, theme);
116116
});
117117

@@ -123,7 +123,7 @@ describe('LogNgHTML', () => {
123123
jest
124124
.spyOn(prettierUtil, 'removeComments')
125125
.mockReturnValue(commentFreeHTMLString);
126-
logNgHTML(htmlString, false, theme);
126+
phl(htmlString, false, theme);
127127
expect(prettyHTMLLog.highlight).toHaveBeenCalledWith(
128128
commentFreeHTMLString,
129129
theme

projects/pretty-html-log/src/lib/logNgHTML.ts renamed to projects/pretty-html-log/src/lib/phl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type NgHTMLElement<T> =
2121
| HTMLElement[]
2222
| string;
2323

24-
export const logNgHTML = <T>(
24+
export const phl = <T>(
2525
ngHTMLElement: NgHTMLElement<T>,
2626
enableComments = false,
2727
theme = THEMES.DRACULA
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/*
22
* Public API Surface of pretty-html-log
33
*/
4-
5-
export * from './lib/patchConsole';
6-
export * from './lib/logNgHTML';
4+
export * from './lib/phl';

0 commit comments

Comments
 (0)