jest-preset-angular 8.3.2 → 8.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +440 -0
- package/LICENSE.md +21 -0
- package/README.md +40 -462
- package/build/AngularNoNgAttributesSnapshotSerializer.js +18 -35
- package/build/AngularSnapshotSerializer.js +20 -20
- package/build/HTMLCommentSerializer.js +9 -9
- package/build/InlineFilesTransformer.js +16 -18
- package/build/StripStylesTransformer.js +14 -24
- package/build/TransformUtils.js +1 -1
- package/build/reflect-metadata.js +7 -7
- package/build/serializers/html-comment.js +8 -0
- package/build/serializers/index.js +6 -0
- package/build/serializers/ng-snapshot.js +59 -0
- package/build/serializers/no-ng-attributes.js +42 -0
- package/build/setup-jest.js +3 -3
- package/build/utils/ngcc-jest-processor.js +38 -0
- package/build/zone-patch/index.js +28 -68
- package/index.js +7 -0
- package/jest-preset.js +3 -3
- package/ngcc-jest-processor.js +1 -0
- package/package.json +60 -1
- package/setup-jest.js +1 -0
- package/AngularNoNgAttributesSnapshotSerializer.js +0 -57
- package/AngularSnapshotSerializer.js +0 -79
- package/HTMLCommentSerializer.js +0 -25
- package/zone-patch/README.md +0 -6
- package/zone-patch/index.js +0 -136
package/README.md
CHANGED
|
@@ -1,500 +1,78 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">Jest Preset Angular</h1>
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://www.npmjs.com/package/jest-preset-angular) [](https://greenkeeper.io/)
|
|
5
|
-

|
|
3
|
+
<p align="center">A preset of Jest configuration for Angular projects.</p>
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://actions-badge.atrox.dev/thymikee/jest-preset-angular/goto?ref=master"><img alt="Build Status" src="https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fthymikee%2Fjest-preset-angular%2Fbadge%3Fref%3Dmaster&style=flat-square" /></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/jest-preset-angular"><img src="https://img.shields.io/npm/v/jest-preset-angular/latest.svg?style=flat-square" alt="NPM Version" /></a>
|
|
8
|
+
<a href="https://www.npmjs.com/package/jest-preset-angular"><img src="https://img.shields.io/npm/v/jest-preset-angular/next.svg?style=flat-square" alt="NPM Version" /></a>
|
|
9
|
+
<a href="https://github.com/thymikee/jest-preset-angular/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/jest-preset-angular.svg?style=flat-square" alt="GitHub license" /></a>
|
|
10
|
+
</p>
|
|
8
11
|
|
|
9
12
|
This is a part of the article: [Testing Angular faster with Jest](https://www.xfive.co/blog/testing-angular-faster-jest/).
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
## Getting Started
|
|
12
15
|
|
|
13
|
-
|
|
16
|
+
These instructions will get you setup to use `jest-preset-angular` in your project. For more detailed documentation,
|
|
17
|
+
please check [online documentation](https://thymikee.github.io/jest-preset-angular).
|
|
18
|
+
|
|
19
|
+
Install using [`yarn`](https://yarnpkg.com/en/package/jest-preset-angular):
|
|
14
20
|
|
|
15
21
|
```bash
|
|
16
|
-
yarn add -D jest jest-preset-angular
|
|
17
|
-
# or
|
|
18
|
-
npm install -D jest jest-preset-angular @types/jest
|
|
22
|
+
yarn add -D jest jest-preset-angular
|
|
19
23
|
```
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
## Usage
|
|
24
|
-
|
|
25
|
-
In `src` directory create `setup-jest.ts` file with following contents:
|
|
25
|
+
Or [`npm`](https://www.npmjs.com/package/jest-preset-angular):
|
|
26
26
|
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
import './jest-global-mocks'; // browser mocks globally available for every test
|
|
27
|
+
```bash
|
|
28
|
+
npm install -D jest jest-preset-angular
|
|
30
29
|
```
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
## Configuration
|
|
33
32
|
|
|
34
|
-
|
|
33
|
+
In your project root, create `setup-jest.ts` file with following contents:
|
|
35
34
|
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
"jest": {
|
|
39
|
-
"preset": "jest-preset-angular",
|
|
40
|
-
"setupFilesAfterEnv": ["<rootDir>/src/setup-jest.ts"]
|
|
41
|
-
}
|
|
42
|
-
}
|
|
35
|
+
```ts
|
|
36
|
+
import 'jest-preset-angular/setup-jest';
|
|
43
37
|
```
|
|
44
38
|
|
|
45
|
-
|
|
46
|
-
By Angular CLI defaults you'll have a `src/test.ts` file which will be picked up by jest. To circumvent this you can either rename it to `src/karmaTest.ts` or hide it from jest by adding `<rootDir>/src/test.ts` to jest `testPathIgnorePatterns` option.
|
|
39
|
+
Add the following section:
|
|
47
40
|
|
|
48
|
-
|
|
49
|
-
## Exposed [configuration](https://github.com/thymikee/jest-preset-angular/blob/master/jest-preset.js)
|
|
41
|
+
- to your root `jest.config.js`
|
|
50
42
|
|
|
51
43
|
```js
|
|
44
|
+
// jest.config.js
|
|
52
45
|
module.exports = {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
tsconfig: '<rootDir>/tsconfig.spec.json',
|
|
56
|
-
stringifyContentPathRegex: '\\.html$',
|
|
57
|
-
astTransformers: {
|
|
58
|
-
before: [
|
|
59
|
-
'jest-preset-angular/build/InlineFilesTransformer',
|
|
60
|
-
'jest-preset-angular/build/StripStylesTransformer',
|
|
61
|
-
],
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
transform: {
|
|
66
|
-
'^.+\\.(ts|js|html)$': 'ts-jest',
|
|
67
|
-
},
|
|
68
|
-
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
|
|
69
|
-
moduleNameMapper: {
|
|
70
|
-
'^src/(.*)$': '<rootDir>/src/$1',
|
|
71
|
-
'^app/(.*)$': '<rootDir>/src/app/$1',
|
|
72
|
-
'^assets/(.*)$': '<rootDir>/src/assets/$1',
|
|
73
|
-
'^environments/(.*)$': '<rootDir>/src/environments/$1',
|
|
74
|
-
},
|
|
75
|
-
transformIgnorePatterns: ['node_modules/(?!@ngrx)'],
|
|
76
|
-
snapshotSerializers: [
|
|
77
|
-
'jest-preset-angular/build/AngularSnapshotSerializer.js',
|
|
78
|
-
'jest-preset-angular/build/HTMLCommentSerializer.js',
|
|
79
|
-
],
|
|
46
|
+
preset: 'jest-preset-angular',
|
|
47
|
+
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
|
|
80
48
|
};
|
|
81
49
|
```
|
|
82
50
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
- `<rootDir>` is a special syntax for root of your project (here by default it's project's root /)
|
|
86
|
-
- we're using some `"globals"` to pass information about where our tsconfig.json file is that we'd like to be able to transform HTML files through ts-jest
|
|
87
|
-
- `"transform"` – run every TS, JS, or HTML file through so called _preprocessor_ (we'll get there); this lets Jest understand non-JS syntax
|
|
88
|
-
- `"testMatch"` – we want to run Jest on files that matches this glob
|
|
89
|
-
- `"moduleFileExtensions"` – our modules are TypeScript and JavaScript files
|
|
90
|
-
- `"moduleNameMapper"` – if you're using absolute imports here's how to tell Jest where to look for them; uses regex
|
|
91
|
-
- `"setupFilesAfterEnv"` – this is the heart of our config, in this file we'll setup and patch environment within tests are running
|
|
92
|
-
- `"transformIgnorePatterns"` – unfortunately some modules (like @ngrx) are released as TypeScript files, not pure JavaScript; in such cases we cannot ignore them (all node_modules are ignored by default), so they can be transformed through TS compiler like any other module in our project.
|
|
93
|
-
- `"snapshotSerializers"` - array of serializers which will be applied to snapshot the code. Note: by default angular adds some angular-specific attributes to the code (like `ng-reflect-*`, `ng-version="*"`, `_ngcontent-c*` etc). This package provides serializer to remove such attributes. This makes snapshots cleaner and more human-readable. To remove such specific attributes use `AngularNoNgAttributesSnapshotSerializer` serializer. You need to add `AngularNoNgAttributesSnapshotSerializer` serializer manually (see [`test` app configuration](https://github.com/thymikee/jest-preset-angular/blob/master/e2e/test-app-v9/package.json#L47-L51)).
|
|
94
|
-
|
|
95
|
-
## [AST Transformer](https://github.com/thymikee/jest-preset-angular/blob/master/src/InlineHtmlStripStylesTransformer.ts)
|
|
96
|
-
|
|
97
|
-
Jest doesn't run in browser nor through dev server. It uses jsdom to abstract browser environment. So we have to cheat a little and inline our templates and get rid of styles (we're not testing CSS) because otherwise Angular will try to make XHR call for our templates and fail miserably.
|
|
98
|
-
|
|
99
|
-
## Angular testing environment setup
|
|
100
|
-
|
|
101
|
-
If you look at [`setup-jest.ts`](https://github.com/thymikee/jest-preset-angular/blob/master/src/setup-jest.ts), what we're doing here is we're adding globals required by Angular. With the included [jest-zone-patch](https://github.com/thymikee/jest-preset-angular/tree/master/zone-patch) we also make sure Jest test methods run in Zone context. Then we initialize the Angular testing environment like normal.
|
|
102
|
-
|
|
103
|
-
## Snapshot testing
|
|
104
|
-
|
|
105
|
-
**Since version 1.1.0** it's possible to [snapshot test](http://facebook.github.io/jest/docs/snapshot-testing.html#snapshot-testing-with-jest) your Angular components. Please note it's still under active development and may be a subject of change. You can lookup [test app](/e2e/test-app-v9/src/app) for details
|
|
106
|
-
|
|
107
|
-
Example:
|
|
108
|
-
|
|
109
|
-
`calc-component.spec.ts`
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
// some initialization code
|
|
113
|
-
test('renders markup to snapshot', () => {
|
|
114
|
-
const fixture = TestBed.createComponent(AppComponent);
|
|
115
|
-
expect(fixture).toMatchSnapshot();
|
|
116
|
-
});
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
`__snapshots__/calc-component.spec.ts.snap`
|
|
120
|
-
|
|
121
|
-
```js
|
|
122
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
123
|
-
|
|
124
|
-
exports[`CalcComponent should snap 1`] = `
|
|
125
|
-
<app-calc
|
|
126
|
-
prop1={[Function Number]}
|
|
127
|
-
>
|
|
128
|
-
<p
|
|
129
|
-
class="a-default-class"
|
|
130
|
-
ng-reflect-klass="a-default-class"
|
|
131
|
-
ng-reflect-ng-class="[object Object]"
|
|
132
|
-
>
|
|
133
|
-
calc works!
|
|
134
|
-
</p>
|
|
135
|
-
</app-calc>
|
|
136
|
-
`;
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### Removing empty lines and white-spaces in component snapshots
|
|
140
|
-
|
|
141
|
-
You will immediately notice, that your snapshot files contain a lot of white spaces and blank lines. This is not an issue with Jest, rather with Angular. It can be mitigated via Angular compiler by setting `preserveWhitespaces: false`
|
|
142
|
-
|
|
143
|
-
> By default it's set to `true` Angular 7.x, although it may change to be set to `false` in upcoming versions
|
|
144
|
-
> (if that occurs, you can stop reading right here, because your issue has been already solved)
|
|
145
|
-
|
|
146
|
-
Your `TestBed` setup should look like following:
|
|
147
|
-
|
|
148
|
-
```ts
|
|
149
|
-
describe('Component snapshot tests', ()=>{
|
|
150
|
-
// you need to turn TS checking because it's an private API
|
|
151
|
-
const compilerConfig = {preserveWhitespaces: false} as any
|
|
152
|
-
|
|
153
|
-
beforeEach(() => {
|
|
154
|
-
TestBed.configureCompiler(compilerConfig)
|
|
155
|
-
.configureTestingModule({...});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
})
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
This is indeed very repetitive, so you can extract this in a helper function:
|
|
162
|
-
|
|
163
|
-
```ts
|
|
164
|
-
// test-config.helper.ts
|
|
165
|
-
|
|
166
|
-
import { TestBed } from '@angular/core/testing';
|
|
167
|
-
|
|
168
|
-
type CompilerOptions = Partial<{
|
|
169
|
-
providers: any[];
|
|
170
|
-
useJit: boolean;
|
|
171
|
-
preserveWhitespaces: boolean;
|
|
172
|
-
}>;
|
|
173
|
-
export type ConfigureFn = (testBed: typeof TestBed) => void;
|
|
174
|
-
|
|
175
|
-
export const configureTests = (
|
|
176
|
-
configure: ConfigureFn,
|
|
177
|
-
compilerOptions: CompilerOptions = {}
|
|
178
|
-
) => {
|
|
179
|
-
const compilerConfig: CompilerOptions = {
|
|
180
|
-
preserveWhitespaces: false,
|
|
181
|
-
...compilerOptions,
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
const configuredTestBed = TestBed.configureCompiler(compilerConfig);
|
|
185
|
-
|
|
186
|
-
configure(configuredTestBed);
|
|
187
|
-
|
|
188
|
-
return configuredTestBed.compileComponents().then(() => configuredTestBed);
|
|
189
|
-
};
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
And setup your test with that function like following:
|
|
193
|
-
|
|
194
|
-
```ts
|
|
195
|
-
// foo.component.spec.ts
|
|
196
|
-
|
|
197
|
-
import { async, ComponentFixture } from '@angular/core/testing'
|
|
198
|
-
|
|
199
|
-
import { configureTests, ConfigureFn } from '../test-config.helper'
|
|
200
|
-
|
|
201
|
-
import { AppComponent } from './foo.component';
|
|
202
|
-
|
|
203
|
-
describe('Component snapshots', () => {
|
|
204
|
-
|
|
205
|
-
let fixture: ComponentFixture<FooComponent>;
|
|
206
|
-
let component: FooComponent;
|
|
207
|
-
|
|
208
|
-
beforeEach(
|
|
209
|
-
async(() => {
|
|
210
|
-
const configure: ConfigureFn = testBed => {
|
|
211
|
-
testBed.configureTestingModule({
|
|
212
|
-
declarations: [FooComponent],
|
|
213
|
-
imports: [...],
|
|
214
|
-
schemas: [NO_ERRORS_SCHEMA],
|
|
215
|
-
});
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
configureTests(configure).then(testBed => {
|
|
219
|
-
fixture = testBed.createComponent(FooComponent);
|
|
220
|
-
component = fixture.componentInstance;
|
|
221
|
-
fixture.detectChanges();
|
|
222
|
-
});
|
|
223
|
-
})
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
it(`should create snapshots without blank lines/white spaces`, () => {
|
|
227
|
-
expect(fixture).toMatchSnapshot();
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
})
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
## Troubleshooting
|
|
234
|
-
|
|
235
|
-
Problems may arise if you're using custom builds (this preset is tailored for `angular-cli` as firstly priority). Please be advised that every entry in default configuration may be overridden to best suite your app's needs.
|
|
236
|
-
|
|
237
|
-
### Can't resolve all parameters for SomeClass(?)
|
|
238
|
-
|
|
239
|
-
With Angular 8 and higher, a [change to the way the Angular CLI works](https://github.com/thymikee/jest-preset-angular/issues/288) may be causing your metadata to be lost. You can update your `tsconfig.spec.json` to include the `emitDecoratorMetadata` compiler option:
|
|
240
|
-
|
|
241
|
-
```
|
|
242
|
-
"compilerOptions": {
|
|
243
|
-
"emitDecoratorMetadata": true
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
In general, this is related to Angular's reflection and also depends on a reflection library, as e. g. included in `core-js`. We use our own minimal reflection that satisfy Angular's current requirements, but in case these change, you can install `core-js` and import the reflection library in your `setup-jest.ts`:
|
|
247
|
-
```typescript
|
|
248
|
-
require('core-js/es/reflect');
|
|
249
|
-
require('core-js/proposals/reflect-metadata');
|
|
250
|
-
```
|
|
251
|
-
Note that this might also be related to other issues with the dependency injection and parameter type reflection.
|
|
252
|
-
|
|
253
|
-
### @Input() bindings are not reflected into fixture when `ChangeDetectionStrategy.OnPush` is used
|
|
254
|
-
|
|
255
|
-
This issue is not related to Jest, [it's a known Angular bug](https://github.com/angular/angular/issues/12313)
|
|
256
|
-
|
|
257
|
-
To mitigate this, you need to wrap your component under test, into some container component with default change detection strategy (`ChangeDetectionStrategy.Default`) and pass props through it, or overwrite change detection strategy within `TestBed` setup, if it's not critical for the test.
|
|
258
|
-
|
|
259
|
-
```ts
|
|
260
|
-
// override change detection strategy
|
|
261
|
-
beforeEach(async(() => {
|
|
262
|
-
TestBed.configureTestingModule({ declarations: [PizzaItemComponent] })
|
|
263
|
-
.overrideComponent(PizzaItemComponent, {
|
|
264
|
-
set: { changeDetection: ChangeDetectionStrategy.Default },
|
|
265
|
-
})
|
|
266
|
-
.compileComponents();
|
|
267
|
-
}));
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
### The animation trigger "transformMenu" has failed
|
|
271
|
-
|
|
272
|
-
The currenly used JSDOM version handles this, but older versions used before v7 of this preset was missing transform property. To patch it for Angular Material, use this workaround.
|
|
273
|
-
|
|
274
|
-
Add this to your `jestGlobalMocks` file
|
|
275
|
-
|
|
276
|
-
```js
|
|
277
|
-
Object.defineProperty(document.body.style, 'transform', {
|
|
278
|
-
value: () => {
|
|
279
|
-
return {
|
|
280
|
-
enumerable: true,
|
|
281
|
-
configurable: true,
|
|
282
|
-
};
|
|
283
|
-
},
|
|
284
|
-
});
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
Reference: https://github.com/angular/material2/issues/7101
|
|
288
|
-
|
|
289
|
-
### Absolute imports
|
|
290
|
-
|
|
291
|
-
TypeScript supports absolute imports. The preset (starting from v3.0.0) by default understands absolute imports referring to `src`, `app`, `assets` and `environments` directory, so instead:
|
|
292
|
-
|
|
293
|
-
```ts
|
|
294
|
-
import MyComponent from '../../src/app/my.component';
|
|
295
|
-
import MyStuff from '../../src/testing/my.stuff';
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
you can use:
|
|
299
|
-
|
|
300
|
-
```ts
|
|
301
|
-
import MyComponent from 'app/my.component';
|
|
302
|
-
import MyStuff from 'src/testing/my.stuff';
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
However, if your directory structure differ from that provided by `angular-cli` you can adjust `moduleNameMapper` in Jest config:
|
|
306
|
-
|
|
307
|
-
```json
|
|
308
|
-
{
|
|
309
|
-
"jest": {
|
|
310
|
-
"moduleNameMapper": {
|
|
311
|
-
"app/(.*)": "<rootDir>/src/to/app/$1", // override default, why not
|
|
312
|
-
"testing/(.*)": "<rootDir>/app/testing/$1" // add new mapping
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
### Custom tsconfig
|
|
319
|
-
|
|
320
|
-
Override `globals` object in Jest config:
|
|
321
|
-
|
|
322
|
-
```json
|
|
323
|
-
{
|
|
324
|
-
"jest": {
|
|
325
|
-
"globals": {
|
|
326
|
-
"ts-jest": {
|
|
327
|
-
"tsconfig": "<rootDir>/tsconfig.custom.json",
|
|
328
|
-
"stringifyContentPathRegex": "\\.html$",
|
|
329
|
-
"astTransformers": {
|
|
330
|
-
"before": [
|
|
331
|
-
"jest-preset-angular/build/InlineFilesTransformer",
|
|
332
|
-
"jest-preset-angular/build/StripStylesTransformer"
|
|
333
|
-
]
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
```
|
|
340
|
-
|
|
341
|
-
If you choose to overide `globals` in order to point at a specific tsconfig, you will need to add the `astTransformers` to the `globals.ts-jest` section too, otherwise you will get parse errors on any html templates.
|
|
342
|
-
|
|
343
|
-
### Unexpected token [import|export|other]
|
|
344
|
-
|
|
345
|
-
This means, that a file is not transformed through TypeScript compiler, e.g. because it is a JS file with TS syntax, or it is published to npm as uncompiled source files. Here's what you can do.
|
|
346
|
-
|
|
347
|
-
#### Adjust your `tsconfig.spec.json`:
|
|
348
|
-
|
|
349
|
-
Since Angular released v6, the default `tsconfig.json` and `tsconfig.spec.json` have been changed. Therefore, `jest` will throw an error
|
|
350
|
-
|
|
351
|
-
```
|
|
352
|
-
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import 'jest-preset-angular';
|
|
353
|
-
^^^^^^
|
|
354
|
-
SyntaxError: Unexpected token import
|
|
355
|
-
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
What you need to do is adjust your `tsconfig.spec.json` to add the option `"module": "commonjs",`
|
|
359
|
-
|
|
360
|
-
A default `tsconfig.spec.json` after modifying will look like this
|
|
361
|
-
|
|
362
|
-
```
|
|
363
|
-
{
|
|
364
|
-
"extends": "../tsconfig.json",
|
|
365
|
-
"compilerOptions": {
|
|
366
|
-
"outDir": "../out-tsc/spec",
|
|
367
|
-
"module": "commonjs",
|
|
368
|
-
"types": [
|
|
369
|
-
"jest",
|
|
370
|
-
"jquery",
|
|
371
|
-
"jsdom",
|
|
372
|
-
"node"
|
|
373
|
-
]
|
|
374
|
-
},
|
|
375
|
-
"files": [
|
|
376
|
-
"polyfills.ts"
|
|
377
|
-
],
|
|
378
|
-
"include": [
|
|
379
|
-
"**/*.spec.ts",
|
|
380
|
-
"**/*.d.ts"
|
|
381
|
-
]
|
|
382
|
-
```
|
|
383
|
-
|
|
384
|
-
#### Adjust your `transformIgnorePatterns` whitelist:
|
|
385
|
-
|
|
386
|
-
```json
|
|
387
|
-
{
|
|
388
|
-
"jest": {
|
|
389
|
-
"transformIgnorePatterns": [
|
|
390
|
-
"node_modules/(?!@ngrx|angular2-ui-switch|ng-dynamic)"
|
|
391
|
-
]
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
```
|
|
395
|
-
|
|
396
|
-
By default Jest doesn't transform `node_modules`, because they should be valid JavaScript files. However, it happens that library authors assume that you'll compile their sources. So you have to tell this to Jest explicitly. Above snippet means that `@ngrx`, `angular2-ui-switch` and `ng-dynamic` will be transformed, even though they're `node_modules`.
|
|
397
|
-
|
|
398
|
-
#### Allow JS files in your TS `compilerOptions`
|
|
51
|
+
- or to your root `package.json`
|
|
399
52
|
|
|
400
53
|
```json
|
|
401
|
-
{
|
|
402
|
-
"compilerOptions": {
|
|
403
|
-
"allowJs": true
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
```
|
|
407
|
-
|
|
408
|
-
This tells `ts-jest` (a preprocessor this preset using to transform TS files) to treat JS files the same as TS ones.
|
|
409
|
-
|
|
410
|
-
#### Transpile js files through `babel-jest`
|
|
411
|
-
|
|
412
|
-
Some vendors publish their sources without transpiling. You need to say jest to transpile such files manually since `typescript` (and thus `ts-jest` used by this preset) do not transpile them.
|
|
413
|
-
|
|
414
|
-
1. Install dependencies required by Jest official documentation for [Babel integration](https://jest-bot.github.io/jest/docs/babel.html).
|
|
415
|
-
|
|
416
|
-
2. Install `@babel/preset-env` and add `babel.config.js` (or modify existing if needed) with the following content:
|
|
417
|
-
```js
|
|
418
|
-
module.exports = function(api) {
|
|
419
|
-
api.cache(true);
|
|
420
|
-
|
|
421
|
-
const presets = ['@babel/preset-env'];
|
|
422
|
-
const plugins = [];
|
|
423
|
-
|
|
424
|
-
return {
|
|
425
|
-
presets,
|
|
426
|
-
plugins,
|
|
427
|
-
};
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
```
|
|
431
|
-
|
|
432
|
-
*Note: do not use a `.babelrc` file otherwise the packages that you specify in the next step will not be picked up. CF [Babel documentation](https://babeljs.io/docs/en/configuration#what-s-your-use-case) and the comment `You want to compile node_modules? babel.config.js is for you!`*.
|
|
433
|
-
|
|
434
|
-
3. Update Jest configuration (by default TypeScript process untranspiled JS files which is source of the problem):
|
|
435
|
-
|
|
436
|
-
```js
|
|
437
54
|
{
|
|
438
55
|
"jest": {
|
|
439
|
-
"
|
|
440
|
-
|
|
441
|
-
"^.+\\.js$": "babel-jest"
|
|
442
|
-
},
|
|
56
|
+
"preset": "jest-preset-angular",
|
|
57
|
+
"setupFilesAfterEnv": ["<rootDir>/setup-jest.ts"]
|
|
443
58
|
}
|
|
444
59
|
}
|
|
445
60
|
```
|
|
446
61
|
|
|
447
|
-
|
|
62
|
+
## Built With
|
|
448
63
|
|
|
449
|
-
|
|
64
|
+
- [TypeScript](https://www.typescriptlang.org/) - JavaScript that scales
|
|
65
|
+
- [Angular](https://angular.io/) - The modern web developer's platform
|
|
66
|
+
- [`ts-jest`](https://kulshekhar.github.io/ts-jest) - Jest processor for TypeScript
|
|
450
67
|
|
|
451
|
-
|
|
68
|
+
## Authors/maintainers
|
|
452
69
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
// common rxjs imports
|
|
457
|
-
import 'rxjs/add/operator/map';
|
|
458
|
-
import 'rxjs/add/operator/switchMap';
|
|
459
|
-
import 'rxjs/add/operator/do';
|
|
460
|
-
import 'rxjs/add/operator/catch';
|
|
461
|
-
// ...
|
|
462
|
-
|
|
463
|
-
import './jestGlobalMocks';
|
|
464
|
-
```
|
|
465
|
-
|
|
466
|
-
### Allow vendor libraries like jQuery, etc...
|
|
467
|
-
|
|
468
|
-
The same like normal Jest configuration, you can load jQuery in your Jest setup file. For example your Jest setup file is `setup-jest.ts` you can declare jQuery:
|
|
469
|
-
|
|
470
|
-
```js
|
|
471
|
-
window.$ = require('path/to/jquery');
|
|
472
|
-
```
|
|
473
|
-
|
|
474
|
-
or
|
|
475
|
-
|
|
476
|
-
```js
|
|
477
|
-
import $ from 'jquery';
|
|
478
|
-
global.$ = global.jQuery = $;
|
|
479
|
-
```
|
|
480
|
-
|
|
481
|
-
The same declaration can be applied to other vendor libraries.
|
|
482
|
-
|
|
483
|
-
Reference: https://github.com/facebook/jest/issues/708
|
|
484
|
-
|
|
485
|
-
### Configure other JSDOM versions
|
|
486
|
-
|
|
487
|
-
**Jest** v26 by default uses **JSDOM** 16 to support Node 10+.
|
|
70
|
+
- **Michał Pierzchała** - [thymikee](https://github.com/thymikee)
|
|
71
|
+
- **Ahn** - [ahnpnl](https://github.com/ahnpnl)
|
|
72
|
+
- **Thomas** - [wtho](https://github.com/wtho)
|
|
488
73
|
|
|
489
|
-
|
|
490
|
-
package, e.g. `jest-environment-jsdom-sixteen` and edit your Jest config like so:
|
|
491
|
-
|
|
492
|
-
```
|
|
493
|
-
{
|
|
494
|
-
"testEnvironment": "jest-environment-jsdom-sixteen"
|
|
495
|
-
}
|
|
496
|
-
```
|
|
74
|
+
See also the list of [contributors](https://github.com/thymikee/jest-preset-angular/contributors) who participated in this project.
|
|
497
75
|
|
|
498
|
-
|
|
76
|
+
## License
|
|
499
77
|
|
|
500
|
-
|
|
78
|
+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return r;
|
|
8
|
-
};
|
|
9
|
-
var jestDOMElementSerializer = require('pretty-format').plugins.DOMElement;
|
|
10
|
-
var attributesToRemovePatterns = ['ng-reflect', '_nghost', '_ngcontent', 'ng-version'];
|
|
11
|
-
var attributesToClean = {
|
|
2
|
+
const { rootLogger } = require('ts-jest/dist/utils/logger');
|
|
3
|
+
rootLogger.warn("The snapshot serializer under import path `'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js'` is deprecated and will be removed in v9.0.0. Please switch to `'jest-preset-angular/build/serializers/no-ng-attributes'`");
|
|
4
|
+
const jestDOMElementSerializer = require('pretty-format').plugins.DOMElement;
|
|
5
|
+
const attributesToRemovePatterns = ['ng-reflect', '_nghost', '_ngcontent', 'ng-version'];
|
|
6
|
+
const attributesToClean = {
|
|
12
7
|
class: [/^(?:mat|cdk|ng).*-\w*\d+-\d+$/, /^ng-star-inserted$/],
|
|
13
8
|
id: [/^(?:mat|cdk|ng).*-\d+$/],
|
|
14
9
|
for: [/^(?:mat|cdk|ng).*-\d+$/],
|
|
@@ -16,29 +11,19 @@ var attributesToClean = {
|
|
|
16
11
|
'aria-labelledby': [/^(?:mat|cdk|ng).*-\d+$/],
|
|
17
12
|
'aria-controls': [/^(?:mat|cdk|ng).*-\d+$/],
|
|
18
13
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return Object.keys(attributesToClean).some(function (removePatternKey) { return attribute.name === removePatternKey; });
|
|
24
|
-
};
|
|
25
|
-
var serialize = function (node) {
|
|
26
|
-
var rest = [];
|
|
27
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
28
|
-
rest[_i - 1] = arguments[_i];
|
|
29
|
-
}
|
|
30
|
-
var nodeCopy = node.cloneNode(true);
|
|
14
|
+
const hasAttributesToRemove = (attribute) => attributesToRemovePatterns.some((removePattern) => attribute.name.startsWith(removePattern));
|
|
15
|
+
const hasAttributesToClean = (attribute) => Object.keys(attributesToClean).some((removePatternKey) => attribute.name === removePatternKey);
|
|
16
|
+
const serialize = (node, ...rest) => {
|
|
17
|
+
const nodeCopy = node.cloneNode(true);
|
|
31
18
|
Object.values(nodeCopy.attributes)
|
|
32
19
|
.filter(hasAttributesToRemove)
|
|
33
|
-
.forEach(
|
|
20
|
+
.forEach((attribute) => nodeCopy.attributes.removeNamedItem(attribute.name));
|
|
34
21
|
Object.values(nodeCopy.attributes)
|
|
35
22
|
.filter(hasAttributesToClean)
|
|
36
|
-
.forEach(
|
|
23
|
+
.forEach((attribute) => {
|
|
37
24
|
attribute.value = attribute.value
|
|
38
25
|
.split(' ')
|
|
39
|
-
.filter(
|
|
40
|
-
return !attributesToClean[attribute.name].some(function (attributeCleanRegex) { return attributeCleanRegex.test(attrValue); });
|
|
41
|
-
})
|
|
26
|
+
.filter((attrValue) => !attributesToClean[attribute.name].some((attributeCleanRegex) => attributeCleanRegex.test(attrValue)))
|
|
42
27
|
.join(' ');
|
|
43
28
|
if (attribute.value === '') {
|
|
44
29
|
nodeCopy.attributes.removeNamedItem(attribute.name);
|
|
@@ -47,14 +32,12 @@ var serialize = function (node) {
|
|
|
47
32
|
nodeCopy.attributes.setNamedItem(attribute);
|
|
48
33
|
}
|
|
49
34
|
});
|
|
50
|
-
return jestDOMElementSerializer.serialize
|
|
51
|
-
};
|
|
52
|
-
var serializeTestFn = function (val) {
|
|
53
|
-
return val.attributes !== undefined &&
|
|
54
|
-
Object.values(val.attributes).some(function (attribute) { return hasAttributesToRemove(attribute) || hasAttributesToClean(attribute); });
|
|
35
|
+
return jestDOMElementSerializer.serialize(nodeCopy, ...rest);
|
|
55
36
|
};
|
|
56
|
-
|
|
37
|
+
const serializeTestFn = (val) => val.attributes !== undefined &&
|
|
38
|
+
Object.values(val.attributes).some((attribute) => hasAttributesToRemove(attribute) || hasAttributesToClean(attribute));
|
|
39
|
+
const test = (val) => jestDOMElementSerializer.test(val) && serializeTestFn(val);
|
|
57
40
|
module.exports = {
|
|
58
|
-
serialize
|
|
59
|
-
test
|
|
41
|
+
serialize,
|
|
42
|
+
test,
|
|
60
43
|
};
|