Skip to content

Commit b8a1312

Browse files
authored
Merge pull request #43 from angular-extensions/feature/v5
Feature/v5
2 parents 573ae21 + 85066cf commit b8a1312

18 files changed

+37976
-15919
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ speed-measure-plugin*.json
3232
.history/*
3333

3434
# misc
35+
/.angular/cache
3536
/.sass-cache
3637
/connect.lock
3738
/coverage

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ dist: trusty
22
sudo: required
33
language: node_js
44
node_js:
5-
- '10'
5+
- '16'
66

77
os:
88
- linux

README.md

Lines changed: 71 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,107 @@
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. 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+
```
67111

68112
## API
69113

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

72-
```
116+
```typescript
73117
<T>(
74118
ngHTMLElement: NgHTMLElement<T>,
75119
enableComments = false,
@@ -90,14 +134,14 @@ The `console.logNgHTML()` method has the following signature:
90134
In your test you can simply write the following line.
91135

92136
```typescript
93-
console.logNgHTML(fixture.debugElement.query(By.css('mat-tab-body')));
137+
phl(fixture.debugElement.query(By.css('mat-tab-body')));
94138
```
95139

96140
Which will print the following string to your console. Depending on your test configuration you
97141
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.
98142

99143
```typescript
100-
logNgHTML(fixture.debugElement.query(By.css('mat-tab-body')));
144+
phl(fixture.debugElement.query(By.css('mat-tab-body')));
101145
```
102146

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

113157
```typescript
114-
console.logNgHTML(fixture);
158+
phl(fixture);
115159
```
116160

117161
of a debugElement (or multiple debugElements)
118162

119163
```typescript
120-
console.logNgHTML(fixture.debugElement);
164+
phl(fixture.debugElement);
121165
```
122166

123167
of a nativeElement (or multiple nativeElements)
124168

125169
```typescript
126-
console.logNgHTML(fixture.debugElement.nativeElement);
170+
phl(fixture.debugElement.nativeElement);
127171
```
128172

129173
or even a simple HTML string
130174

131175
```typescript
132-
console.logNgHTML('<h1>Foo</h1>');
176+
phl('<h1>Foo</h1>');
133177
```
134178

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

141185
```typescript
142-
console.logNgHTML(fixture, true);
186+
phl(fixture, true);
143187
```
144188

145189
### Change the theme
146190

147191
`@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.
150193

151194
```typescript
152195
import { THEMES } from 'pretty-html-log';
153196

154197
console.logNgHTML(fixture, false, THEMES.VSCODE);
155198
```
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-
```

angular.json

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@
2828
"tsConfig": "projects/pretty-html-log/tsconfig.spec.json",
2929
"karmaConfig": "projects/pretty-html-log/karma.conf.js"
3030
}
31-
},
32-
"lint": {
33-
"builder": "@angular-devkit/build-angular:tslint",
34-
"options": {
35-
"tsConfig": [
36-
"projects/pretty-html-log/tsconfig.lib.json",
37-
"projects/pretty-html-log/tsconfig.spec.json"
38-
],
39-
"exclude": ["**/node_modules/**"]
40-
}
4131
}
4232
}
4333
}

0 commit comments

Comments
 (0)