jest-preset-angular 8.3.1 → 8.3.2
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/README.md +500 -0
- package/jest-preset.js +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
# jest-preset-angular
|
|
2
|
+
|
|
3
|
+
[](https://circleci.com/gh/thymikee/jest-preset-angular)
|
|
4
|
+
[](https://www.npmjs.com/package/jest-preset-angular) [](https://greenkeeper.io/)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
A preset of [Jest](http://facebook.github.io/jest) configuration for [Angular](https://angular.io/) projects.
|
|
8
|
+
|
|
9
|
+
This is a part of the article: [Testing Angular faster with Jest](https://www.xfive.co/blog/testing-angular-faster-jest/).
|
|
10
|
+
|
|
11
|
+
_Note: This preset does not support AngularJS (1.x). If you want to set up Jest with AngularJS, please see [this blog post](https://medium.com/aya-experience/testing-an-angularjs-app-with-jest-3029a613251)._
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add -D jest jest-preset-angular @types/jest
|
|
17
|
+
# or
|
|
18
|
+
npm install -D jest jest-preset-angular @types/jest
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
This will install `jest`, `@types/jest`, `ts-jest` as dependencies needed to run with Angular projects.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
In `src` directory create `setup-jest.ts` file with following contents:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import 'jest-preset-angular';
|
|
29
|
+
import './jest-global-mocks'; // browser mocks globally available for every test
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
_Note: feel free to copy the [`jest-global-mocks.ts`](https://github.com/thymikee/jest-preset-angular/blob/master/e2e/test-app-v9/jest-global-mocks.ts) file from the test app directory and save it next to the `setup-jest.ts` file._
|
|
33
|
+
|
|
34
|
+
...and include this in your `package.json`:
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"jest": {
|
|
39
|
+
"preset": "jest-preset-angular",
|
|
40
|
+
"setupFilesAfterEnv": ["<rootDir>/src/setup-jest.ts"]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Avoid karma conflicts
|
|
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.
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
## Exposed [configuration](https://github.com/thymikee/jest-preset-angular/blob/master/jest-preset.js)
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
module.exports = {
|
|
53
|
+
globals: {
|
|
54
|
+
'ts-jest': {
|
|
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
|
+
],
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Brief explanation of config
|
|
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`
|
|
399
|
+
|
|
400
|
+
```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
|
+
{
|
|
438
|
+
"jest": {
|
|
439
|
+
"transform": {
|
|
440
|
+
"^.+\\.(ts|html)$": "ts-jest",
|
|
441
|
+
"^.+\\.js$": "babel-jest"
|
|
442
|
+
},
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### Observable ... is not a function
|
|
448
|
+
|
|
449
|
+
Note: This fix is only relevant to Angular v5 and lower.
|
|
450
|
+
|
|
451
|
+
Since v1.0 this preset doesn't import whole `rxjs` library by default for variety of reasons. This may result in breaking your tests that relied on this behavior. It may however become cumbersome to include e.g. `rxjs/add/operator/map` or `rxjs/add/operator/do` for every test, so as a workaround you can include common operators or other necessary imports in your `setup-jest.ts` file:
|
|
452
|
+
|
|
453
|
+
```js
|
|
454
|
+
import 'jest-preset-angular';
|
|
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+.
|
|
488
|
+
|
|
489
|
+
If you need a different JSDOM version than the one that ships with Jest, you can install a jsdom environment
|
|
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
|
+
```
|
|
497
|
+
|
|
498
|
+
If you use JSDOM v11 or lower, you might have to mock `localStorage` or `sessionStorage` on your own or using some third-party library by loading it in `setupFilesAfterEnv`.
|
|
499
|
+
|
|
500
|
+
Reference: https://jestjs.io/docs/en/configuration.html#testenvironment-string, https://github.com/jsdom/jsdom/blob/master/Changelog.md#1200
|
package/jest-preset.js
CHANGED