You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
25
28
26
29
## Why you should use this module
27
30
28
31
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
30
34
31
35
```typescript
32
36
fixture.debugElement.nativeElement.innerHTML;
33
37
```
34
38
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**!
- 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.
45
46
- in case you are using prettier (which you should ;)), pretty-html-log will pick
46
47
up your prettier config and pretty print the HTML string based on your prettier configuration. 🤩
47
48
48
49
## Getting started
49
50
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.
52
54
53
55
```
54
56
npm i -D @angular-extensions/pretty-html-log
55
57
```
56
58
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.
phl(fixture); // This will pretty print the fixture
69
+
expect(myTable.getRows()).toBe(5);
70
+
});
71
+
});
62
72
```
63
73
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
+
```
67
104
68
105
## API
69
106
70
-
The `console.logNgHTML()` method has the following signature:
107
+
The `phl` method has the following signature:
71
108
72
-
```
109
+
```typescript
73
110
<T>(
74
111
ngHTMLElement: NgHTMLElement<T>,
75
112
enableComments=false,
@@ -90,14 +127,14 @@ The `console.logNgHTML()` method has the following signature:
90
127
In your test you can simply write the following line.
Which will print the following string to your console. Depending on your test configuration you
97
134
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.
@@ -139,34 +176,16 @@ are not printed by default. However, there are cases where you want to print tho
139
176
can pass `true` as an additional flag tot he `logNgHTML` method.
140
177
141
178
```typescript
142
-
console.logNgHTML(fixture, true);
179
+
phl(fixture, true);
143
180
```
144
181
145
182
### Change the theme
146
183
147
184
`@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.
150
186
151
187
```typescript
152
188
import { THEMES } from'pretty-html-log';
153
189
154
190
console.logNgHTML(fixture, false, THEMES.VSCODE);
155
191
```
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`.
0 commit comments