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. Import `@angular-extensions/pretty-html-log` inside your jest.setup.ts
95
+
96
+
```typescript
97
+
import'jest-preset-angular/setup-jest';
98
+
import'@angular-extensions/pretty-html-log';
99
+
```
100
+
101
+
4. Start using it inside your tests without the usage of import 🤩
102
+
103
+
```typescript
104
+
describe('my test suite', () => {
105
+
it('should be green', () => {
106
+
phl(fixture); // This will pretty print the fixture
107
+
expect(myTable.getRows()).toBe(5);
108
+
});
109
+
});
110
+
```
67
111
68
112
## API
69
113
70
-
The `console.logNgHTML()` method has the following signature:
114
+
The `phl` method has the following signature:
71
115
72
-
```
116
+
```typescript
73
117
<T>(
74
118
ngHTMLElement: NgHTMLElement<T>,
75
119
enableComments=false,
@@ -90,14 +134,14 @@ The `console.logNgHTML()` method has the following signature:
90
134
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
141
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 +183,16 @@ are not printed by default. However, there are cases where you want to print tho
139
183
can pass `true` as an additional flag tot he `logNgHTML` method.
140
184
141
185
```typescript
142
-
console.logNgHTML(fixture, true);
186
+
phl(fixture, true);
143
187
```
144
188
145
189
### Change the theme
146
190
147
191
`@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.
192
+
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
193
151
194
```typescript
152
195
import { THEMES } from'pretty-html-log';
153
196
154
197
console.logNgHTML(fixture, false, THEMES.VSCODE);
155
198
```
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