jest-preset-angular 7.1.0 → 8.1.1
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/.circleci/config.yml +6 -8
- package/.vscode/launch.json +36 -0
- package/CHANGELOG.md +50 -2
- package/README.md +53 -22
- package/build/AngularNoNgAttributesSnapshotSerializer.js +36 -0
- package/build/AngularSnapshotSerializer.js +49 -0
- package/{HTMLCommentSerializer.js → build/HTMLCommentSerializer.js} +11 -14
- package/{InlineHtmlStripStylesTransformer.js → build/InlineFilesTransformer.js} +31 -32
- package/build/StripStylesTransformer.js +122 -0
- package/build/TransformUtils.js +20 -0
- package/build/reflectMetadata.js +21 -0
- package/build/setupJest.js +12 -0
- package/build/zone-patch/index.js +203 -0
- package/index.js +1 -1
- package/jest-preset.js +8 -5
- package/package.json +9 -9
- package/tsconfig.json +2 -2
- package/AngularSnapshotSerializer.js +0 -67
- package/setupJest.js +0 -37
- package/zone-patch/LICENSE +0 -29
- package/zone-patch/README.md +0 -6
- package/zone-patch/index.js +0 -108
package/.circleci/config.yml
CHANGED
|
@@ -9,6 +9,8 @@ jobs:
|
|
|
9
9
|
executor: node-10
|
|
10
10
|
steps:
|
|
11
11
|
- checkout
|
|
12
|
+
- run: 'sudo yarn global add greenkeeper-lockfile@2'
|
|
13
|
+
- run: 'greenkeeper-lockfile-update'
|
|
12
14
|
- restore_cache:
|
|
13
15
|
keys:
|
|
14
16
|
# if lock file changes, we still use increasingly general patterns to restore cache
|
|
@@ -43,19 +45,15 @@ jobs:
|
|
|
43
45
|
steps:
|
|
44
46
|
- attach_workspace:
|
|
45
47
|
at: .
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
# skipping cache here as it created issues with the preset in the root
|
|
49
|
+
# not being used, as older version in cache was available
|
|
50
|
+
- run: 'sudo yarn global add greenkeeper-lockfile@2'
|
|
51
|
+
- run: 'greenkeeper-lockfile-update'
|
|
50
52
|
- run:
|
|
51
53
|
name: Install Example Dependencies
|
|
52
54
|
command: |
|
|
53
55
|
cd example
|
|
54
56
|
yarn install --frozen-lockfile
|
|
55
|
-
- save_cache:
|
|
56
|
-
key: yarn-cache-example-{{ .Branch }}-{{ checksum "yarn.lock" }}
|
|
57
|
-
paths:
|
|
58
|
-
- ./example/node_modules
|
|
59
57
|
- run:
|
|
60
58
|
name: Test Example
|
|
61
59
|
command: |
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
|
3
|
+
// Hover to view descriptions of existing attributes.
|
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"type": "node",
|
|
9
|
+
"request": "attach",
|
|
10
|
+
"name": "Attach to 9226",
|
|
11
|
+
"port": 9226,
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"type": "node",
|
|
15
|
+
"name": "vscode-jest-tests",
|
|
16
|
+
"request": "launch",
|
|
17
|
+
"args": [
|
|
18
|
+
"--runInBand"
|
|
19
|
+
],
|
|
20
|
+
"cwd": "${workspaceFolder}",
|
|
21
|
+
"console": "integratedTerminal",
|
|
22
|
+
"internalConsoleOptions": "neverOpen",
|
|
23
|
+
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"type": "node",
|
|
27
|
+
"request": "launch",
|
|
28
|
+
"name": "Launch Program",
|
|
29
|
+
"program": "${workspaceFolder}/index.js",
|
|
30
|
+
"preLaunchTask": "tsc: build - tsconfig.json",
|
|
31
|
+
"outFiles": [
|
|
32
|
+
"${workspaceFolder}/./**/*.js"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
}
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,53 @@
|
|
|
1
1
|
## Changelog (master)
|
|
2
2
|
|
|
3
|
+
### v8.1.1
|
|
4
|
+
|
|
5
|
+
#### Fixes
|
|
6
|
+
|
|
7
|
+
* Zone.js patch handles test with `done` correctly ([#357](https://github.com/thymikee/jest-preset-angular/pull/357))
|
|
8
|
+
|
|
9
|
+
### v8.1.0
|
|
10
|
+
|
|
11
|
+
#### Features
|
|
12
|
+
|
|
13
|
+
* Run `test.each` in Proxyzone ([#340](https://github.com/thymikee/jest-preset-angular/pull/340)).
|
|
14
|
+
* Update `peerDependencies` for `jest` to v25 ([#346](https://github.com/thymikee/jest-preset-angular/pull/346)).
|
|
15
|
+
* Update `dependencies` for `pretty-format` and `ts-jest` to v25 ([#346](https://github.com/thymikee/jest-preset-angular/pull/346)).
|
|
16
|
+
* Remove `jest-environment-jsdom-fifteen` from dependencies and preset, closes ([#325](https://github.com/thymikee/jest-preset-angular/issues/325))
|
|
17
|
+
|
|
18
|
+
#### Chore && Maintenance
|
|
19
|
+
|
|
20
|
+
* Update project `devDependencies`.
|
|
21
|
+
* Update example app dependencies.
|
|
22
|
+
|
|
23
|
+
### v8.0.0
|
|
24
|
+
|
|
25
|
+
#### Features
|
|
26
|
+
* (**BREAKING**): Refine ast-transformer behavior: only transform `styles`-assignments inside @Component ([#261](https://github.com/thymikee/jest-preset-angular/pull/261)) and TypeScript v2.9 `createStringLiteral` is polyfilled if an older version is used ([#272](https://github.com/thymikee/jest-preset-angular/issues/272)).
|
|
27
|
+
* (**BREAKING**): Restructure project with `src` and `build` folder ([#307](https://github.com/thymikee/jest-preset-angular/pull/307)).
|
|
28
|
+
* (**BREAKING**): Support `tsconfig.spec.json` in root folder by default ([#309](https://github.com/thymikee/jest-preset-angular/pull/309)).
|
|
29
|
+
* (**BREAKING**): Enable AngularNoAngularNoNgAttributesSnapshotSerializer (created in [#97](https://github.com/thymikee/jest-preset-angular/pull/97)) by default ([#318](https://github.com/thymikee/jest-preset-angular/pull/318)).
|
|
30
|
+
* Remove `core-js` dependency by using internal, minimal reflect-metadata ([#315](https://github.com/thymikee/jest-preset-angular/pull/315)).
|
|
31
|
+
|
|
32
|
+
#### Chore && Maintenance
|
|
33
|
+
* Update example app to match Angular 8 Boilerplate ([#311](https://github.com/thymikee/jest-preset-angular/pull/311)).
|
|
34
|
+
* Update example app to not use karma, extract jest config, fix paths ([#316](https://github.com/thymikee/jest-preset-angular/pull/316)).
|
|
35
|
+
* Update jsdom to current version 15 ([#318](https://github.com/thymikee/jest-preset-angular/pull/318)).
|
|
36
|
+
|
|
37
|
+
#### Migration Guide
|
|
38
|
+
* If the `astTransformers` are referenced in a custom `jest` config, `[ 'jest-preset-angular/build/InlineFilesTransformer', 'jest-preset-angular/build/StripStylesTransformer']` have to be set instead.
|
|
39
|
+
* Serializers, transformers and `setupJest` have to be referenced from the `jest-preset-angular/build/`-folder in a custom config. Existing references have to be aligned.
|
|
40
|
+
* If your `tsconfig.spec.json` is located in `src`, move it to your root folder and adjust the referenced files and paths inside, or align your jest configuration as discussed in the [README.md](https://github.com/thymikee/jest-preset-angular/blob/master/README.md#custom-tsconfig).
|
|
41
|
+
* In an Angular 8 project or higher, `core-js` should no longer be a dependency declared in `package.json`, unless it's imported manually in the code.
|
|
42
|
+
|
|
43
|
+
### v7.1.0
|
|
44
|
+
|
|
45
|
+
#### Features
|
|
46
|
+
* Added `AngularNoNgAttributesSnapshotSerializer`. Using this serializer makes snapshots clearer and more human-readable. You have to apply this serializer manually by redefining `snapshotSerializers` `jest` option.
|
|
47
|
+
|
|
48
|
+
#### Chore && Maintenance
|
|
49
|
+
* Fixed a CI cache issue in the example app, which would not always use the current version of the preset in the test runs.
|
|
50
|
+
|
|
3
51
|
### v7.0.0
|
|
4
52
|
|
|
5
53
|
#### Features
|
|
@@ -19,7 +67,7 @@
|
|
|
19
67
|
#### Migration Guide
|
|
20
68
|
|
|
21
69
|
* If `global` and `transform` are not set in your configuration in `jest.config.json`, `jest.config.js` or `package.json`, you are done.
|
|
22
|
-
* If the `global` value of the configuration was
|
|
70
|
+
* If the `global` value of the configuration was overridden, adjust
|
|
23
71
|
* The option `"__TRANSFORM_HTML__": true` is not required anymore. Instead the `"stringifyContentPathRegex": "\\.html$"` should be used inside the `ts-jest`-configuration.
|
|
24
72
|
* Change the assignment identifier from `tsConfigFile` to `tsConfig`.
|
|
25
73
|
* Add the `astTransformers: [require.resolve('jest-preset-angular/InlineHtmlStripStylesTransformer')]` so Jest can work with `templateUrl`-assignments in Component decorators.
|
|
@@ -48,7 +96,7 @@
|
|
|
48
96
|
|
|
49
97
|
### v5.2.2
|
|
50
98
|
|
|
51
|
-
* Fix: specify angular modules as
|
|
99
|
+
* Fix: specify angular modules as peerDependencies ([#141](https://github.com/thymikee/jest-preset-angular/pull/141))
|
|
52
100
|
* Fix: update `STYLE_URLS_REGEX` to not break on multiple new lines ([#139](https://github.com/thymikee/jest-preset-angular/pull/139))
|
|
53
101
|
* Docs: add more troubleshooting docs ([#129](https://github.com/thymikee/jest-preset-angular/pull/129))
|
|
54
102
|
|
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ import 'jest-preset-angular';
|
|
|
28
28
|
import './jestGlobalMocks'; // browser mocks globally available for every test
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
_Note: feel free to copy the `jestGlobalMocks.ts` file from the example directory and save it next to the `setupJest.ts` file._
|
|
31
|
+
_Note: feel free to copy the [`jestGlobalMocks.ts`](https://github.com/thymikee/jest-preset-angular/blob/master/example/src/jestGlobalMocks.ts) file from the example directory and save it next to the `setupJest.ts` file._
|
|
32
32
|
|
|
33
33
|
...and include this in your `package.json`:
|
|
34
34
|
|
|
@@ -51,15 +51,17 @@ By Angular CLI defaults you'll have a `src/test.ts` file which will be picked up
|
|
|
51
51
|
module.exports = {
|
|
52
52
|
globals: {
|
|
53
53
|
'ts-jest': {
|
|
54
|
-
tsConfig: '<rootDir>/
|
|
54
|
+
tsConfig: '<rootDir>/tsconfig.spec.json',
|
|
55
55
|
stringifyContentPathRegex: '\\.html$',
|
|
56
|
-
astTransformers: [
|
|
56
|
+
astTransformers: [
|
|
57
|
+
'jest-preset-angular/build/InlineFilesTransformer',
|
|
58
|
+
'jest-preset-angular/build/StripStylesTransformer'
|
|
59
|
+
],
|
|
57
60
|
},
|
|
58
61
|
},
|
|
59
62
|
transform: {
|
|
60
63
|
'^.+\\.(ts|js|html)$': 'ts-jest',
|
|
61
64
|
},
|
|
62
|
-
testEnvironment: 'jest-environment-jsdom-thirteen',
|
|
63
65
|
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
|
|
64
66
|
moduleNameMapper: {
|
|
65
67
|
'^src/(.*)$': '<rootDir>/src/$1',
|
|
@@ -69,8 +71,8 @@ module.exports = {
|
|
|
69
71
|
},
|
|
70
72
|
transformIgnorePatterns: ['node_modules/(?!@ngrx)'],
|
|
71
73
|
snapshotSerializers: [
|
|
72
|
-
'jest-preset-angular/AngularSnapshotSerializer.js',
|
|
73
|
-
'jest-preset-angular/HTMLCommentSerializer.js',
|
|
74
|
+
'jest-preset-angular/build/AngularSnapshotSerializer.js',
|
|
75
|
+
'jest-preset-angular/build/HTMLCommentSerializer.js',
|
|
74
76
|
],
|
|
75
77
|
};
|
|
76
78
|
```
|
|
@@ -84,7 +86,8 @@ module.exports = {
|
|
|
84
86
|
- `"moduleFileExtensions"` – our modules are TypeScript and JavaScript files
|
|
85
87
|
- `"moduleNameMapper"` – if you're using absolute imports here's how to tell Jest where to look for them; uses regex
|
|
86
88
|
- `"setupFilesAfterEnv"` – this is the heart of our config, in this file we'll setup and patch environment within tests are running
|
|
87
|
-
- `"transformIgnorePatterns"` – unfortunately some modules (like @ngrx
|
|
89
|
+
- `"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.
|
|
90
|
+
- `"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 [`example` app configuration](https://github.com/thymikee/jest-preset-angular/blob/master/example/package.json#L47-L51)).
|
|
88
91
|
|
|
89
92
|
## [AST Transformer](https://github.com/thymikee/jest-preset-angular/blob/master/src/InlineHtmlStripStylesTransformer.ts)
|
|
90
93
|
|
|
@@ -92,7 +95,7 @@ Jest doesn't run in browser nor through dev server. It uses jsdom to abstract br
|
|
|
92
95
|
|
|
93
96
|
## Angular testing environment setup
|
|
94
97
|
|
|
95
|
-
If you look at your `src/test.ts` (or similar bootstrapping test file) file you'll see similarities to [`setupJest.js`](https://github.com/thymikee/jest-preset-angular/blob/master/setupJest.js). What we're doing here is we're adding globals required by Angular. With [jest-zone-patch](https://github.com/thymikee/jest-zone-patch) we also make sure Jest test methods run in Zone context. Then we initialize the Angular testing environment like normal.
|
|
98
|
+
If you look at your `src/test.ts` (or similar bootstrapping test file) file you'll see similarities to [`setupJest.js`](https://github.com/thymikee/jest-preset-angular/blob/master/setupJest.js). 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.
|
|
96
99
|
|
|
97
100
|
## Snapshot testing
|
|
98
101
|
|
|
@@ -134,7 +137,7 @@ exports[`CalcComponent should snap 1`] = `
|
|
|
134
137
|
|
|
135
138
|
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`
|
|
136
139
|
|
|
137
|
-
> By default it's set to `true` Angular
|
|
140
|
+
> By default it's set to `true` Angular 7.x, although it may change to be set to `false` in upcoming versions
|
|
138
141
|
> (if that occurs, you can stop reading right here, because your issue has been already solved)
|
|
139
142
|
|
|
140
143
|
Your `TestBed` setup should look like following:
|
|
@@ -228,6 +231,22 @@ describe('Component snapshots', () => {
|
|
|
228
231
|
|
|
229
232
|
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.
|
|
230
233
|
|
|
234
|
+
### Can't resolve all parameters for SomeClass(?)
|
|
235
|
+
|
|
236
|
+
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:
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
"compilerOptions": {
|
|
240
|
+
"emitDecoratorMetadata": true
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
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 `setupJest.ts`:
|
|
244
|
+
```typescript
|
|
245
|
+
require('core-js/es/reflect');
|
|
246
|
+
require('core-js/proposals/reflect-metadata');
|
|
247
|
+
```
|
|
248
|
+
Note that this might also be related to other issues with the dependency injection and parameter type reflection.
|
|
249
|
+
|
|
231
250
|
### @Input() bindings are not reflected into fixture when `ChangeDetectionStrategy.OnPush` is used
|
|
232
251
|
|
|
233
252
|
This issue is not related to Jest, [it's a known Angular bug](https://github.com/angular/angular/issues/12313)
|
|
@@ -247,7 +266,7 @@ beforeEach(async(() => {
|
|
|
247
266
|
|
|
248
267
|
### The animation trigger "transformMenu" has failed
|
|
249
268
|
|
|
250
|
-
JSDOM missing transform property
|
|
269
|
+
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.
|
|
251
270
|
|
|
252
271
|
Add this to your `jestGlobalMocks` file
|
|
253
272
|
|
|
@@ -302,15 +321,19 @@ Override `globals` object in Jest config:
|
|
|
302
321
|
"jest": {
|
|
303
322
|
"globals": {
|
|
304
323
|
"ts-jest": {
|
|
305
|
-
"
|
|
306
|
-
|
|
307
|
-
|
|
324
|
+
"tsConfig": "<rootDir>/tsconfig.custom.json",
|
|
325
|
+
"stringifyContentPathRegex": "\\.html$",
|
|
326
|
+
"astTransformers": [
|
|
327
|
+
"jest-preset-angular/build/InlineFilesTransformer",
|
|
328
|
+
"jest-preset-angular/build/StripStylesTransformer"
|
|
329
|
+
],
|
|
330
|
+
}
|
|
308
331
|
}
|
|
309
332
|
}
|
|
310
333
|
}
|
|
311
334
|
```
|
|
312
335
|
|
|
313
|
-
If you choose to overide `globals` in order to point at a specific tsconfig, you will need to add
|
|
336
|
+
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.
|
|
314
337
|
|
|
315
338
|
### Unexpected token [import|export|other]
|
|
316
339
|
|
|
@@ -365,7 +388,7 @@ A default `tsconfig.spec.json` after modifying will look like this
|
|
|
365
388
|
}
|
|
366
389
|
```
|
|
367
390
|
|
|
368
|
-
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
|
|
391
|
+
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`.
|
|
369
392
|
|
|
370
393
|
#### Allow JS files in your TS `compilerOptions`
|
|
371
394
|
|
|
@@ -383,7 +406,9 @@ This tells `ts-jest` (a preprocessor this preset using to transform TS files) to
|
|
|
383
406
|
|
|
384
407
|
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.
|
|
385
408
|
|
|
386
|
-
1. Install
|
|
409
|
+
1. Install dependencies required by Jest official documentation for [Babel integration](https://jest-bot.github.io/jest/docs/babel.html).
|
|
410
|
+
|
|
411
|
+
2. Install `@babel/preset-env` and add `babel.config.js` (or modify existing if needed) with the following content:
|
|
387
412
|
```js
|
|
388
413
|
module.exports = function(api) {
|
|
389
414
|
api.cache(true);
|
|
@@ -401,13 +426,13 @@ module.exports = function(api) {
|
|
|
401
426
|
|
|
402
427
|
*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!`*.
|
|
403
428
|
|
|
404
|
-
|
|
429
|
+
3. Update Jest configuration (by default TypeScript process untranspiled JS files which is source of the problem):
|
|
405
430
|
|
|
406
431
|
```js
|
|
407
432
|
{
|
|
408
433
|
"jest": {
|
|
409
434
|
"transform": {
|
|
410
|
-
"^.+\\.(ts|html)$": "
|
|
435
|
+
"^.+\\.(ts|html)$": "ts-jest",
|
|
411
436
|
"^.+\\.js$": "babel-jest"
|
|
412
437
|
},
|
|
413
438
|
}
|
|
@@ -416,6 +441,8 @@ module.exports = function(api) {
|
|
|
416
441
|
|
|
417
442
|
### Observable ... is not a function
|
|
418
443
|
|
|
444
|
+
Note: This fix is only relevant to Angular v5 and lower.
|
|
445
|
+
|
|
419
446
|
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 `setupJest.ts` file:
|
|
420
447
|
|
|
421
448
|
```js
|
|
@@ -450,12 +477,16 @@ The same declaration can be applied to other vendor libraries.
|
|
|
450
477
|
|
|
451
478
|
Reference: https://github.com/facebook/jest/issues/708
|
|
452
479
|
|
|
453
|
-
###
|
|
480
|
+
### Configure other JSDOM versions
|
|
454
481
|
|
|
455
|
-
|
|
482
|
+
**Jest** v25 by default uses **JSDOM** 15 to support Node 8, which should also be compatible with Jest 24 and earlier.
|
|
456
483
|
|
|
457
|
-
|
|
458
|
-
|
|
484
|
+
If you need a newer JSDOM than the one that ships with Jest and run Node >=10, you can install `jest-environment-jsdom-sixteen` package, and edit your Jest config like so:
|
|
485
|
+
|
|
486
|
+
```
|
|
487
|
+
{
|
|
488
|
+
"testEnvironment": "jest-environment-jsdom-sixteen"
|
|
489
|
+
}
|
|
459
490
|
```
|
|
460
491
|
|
|
461
492
|
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`.
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
3
|
+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
|
|
4
|
+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
|
|
5
|
+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
|
|
6
|
+
r[k] = a[j];
|
|
7
|
+
return r;
|
|
8
|
+
};
|
|
9
|
+
var jestDOMElementSerializer = require('pretty-format').plugins.DOMElement;
|
|
10
|
+
var attributesToRemovePatterns = ['ng-reflect', '_nghost', '_ngcontent', 'ng-version'];
|
|
11
|
+
var hasAttributesToRemove = function (attribute) {
|
|
12
|
+
return attributesToRemovePatterns
|
|
13
|
+
.some(function (removePattern) { return attribute.name.startsWith(removePattern); });
|
|
14
|
+
};
|
|
15
|
+
var serialize = function (node) {
|
|
16
|
+
var rest = [];
|
|
17
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
18
|
+
rest[_i - 1] = arguments[_i];
|
|
19
|
+
}
|
|
20
|
+
var nodeCopy = node.cloneNode(true);
|
|
21
|
+
Object.values(nodeCopy.attributes)
|
|
22
|
+
.filter(hasAttributesToRemove)
|
|
23
|
+
.forEach(function (attribute) { return nodeCopy.attributes.removeNamedItem(attribute.name); });
|
|
24
|
+
return jestDOMElementSerializer.serialize.apply(jestDOMElementSerializer, __spreadArrays([nodeCopy], rest));
|
|
25
|
+
};
|
|
26
|
+
var serializeTestFn = function (val) {
|
|
27
|
+
return val.attributes !== undefined && Object.values(val.attributes)
|
|
28
|
+
.some(hasAttributesToRemove);
|
|
29
|
+
};
|
|
30
|
+
var test = function (val) {
|
|
31
|
+
return jestDOMElementSerializer.test(val) && serializeTestFn(val);
|
|
32
|
+
};
|
|
33
|
+
module.exports = {
|
|
34
|
+
serialize: serialize,
|
|
35
|
+
test: test
|
|
36
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var printAttributes = function (val, attributes, print, indent, colors, opts) {
|
|
3
|
+
return attributes
|
|
4
|
+
.sort()
|
|
5
|
+
.map(function (attribute) {
|
|
6
|
+
return (opts.spacing +
|
|
7
|
+
indent(colors.prop.open + attribute + colors.prop.close + '=') +
|
|
8
|
+
colors.value.open +
|
|
9
|
+
(val.componentInstance[attribute] &&
|
|
10
|
+
val.componentInstance[attribute].constructor
|
|
11
|
+
? '{[Function ' +
|
|
12
|
+
val.componentInstance[attribute].constructor.name +
|
|
13
|
+
']}'
|
|
14
|
+
: "\"" + val.componentInstance[attribute] + "\"") +
|
|
15
|
+
colors.value.close);
|
|
16
|
+
})
|
|
17
|
+
.join('');
|
|
18
|
+
};
|
|
19
|
+
var print = function (val, print, indent, opts, colors) {
|
|
20
|
+
var componentAttrs = '';
|
|
21
|
+
var componentName = val.componentRef._elDef.element.name;
|
|
22
|
+
var nodes = (val.componentRef._view.nodes || [])
|
|
23
|
+
.filter(function (node) { return node && node.hasOwnProperty('renderElement'); })
|
|
24
|
+
.map(function (node) { return Array.from(node.renderElement.childNodes).map(print).join(''); })
|
|
25
|
+
.join(opts.edgeSpacing);
|
|
26
|
+
var attributes = Object.keys(val.componentInstance);
|
|
27
|
+
if (attributes.length) {
|
|
28
|
+
componentAttrs += printAttributes(val, attributes, print, indent, colors, opts);
|
|
29
|
+
}
|
|
30
|
+
return ('<' +
|
|
31
|
+
componentName +
|
|
32
|
+
componentAttrs +
|
|
33
|
+
(componentAttrs.length ? '\n' : '') +
|
|
34
|
+
'>\n' +
|
|
35
|
+
indent(nodes) +
|
|
36
|
+
'\n</' +
|
|
37
|
+
componentName +
|
|
38
|
+
'>');
|
|
39
|
+
};
|
|
40
|
+
var test = function (val) {
|
|
41
|
+
return val !== undefined &&
|
|
42
|
+
val !== null &&
|
|
43
|
+
typeof val === 'object' &&
|
|
44
|
+
Object.prototype.hasOwnProperty.call(val, 'componentRef');
|
|
45
|
+
};
|
|
46
|
+
module.exports = {
|
|
47
|
+
print: print,
|
|
48
|
+
test: test
|
|
49
|
+
};
|
|
@@ -6,20 +6,17 @@
|
|
|
6
6
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
7
7
|
*
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
9
|
'use strict';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const print = () => '';
|
|
21
|
-
|
|
10
|
+
var HTML_ELEMENT_REGEXP = /Comment/;
|
|
11
|
+
var test = function (value) {
|
|
12
|
+
return value !== undefined &&
|
|
13
|
+
value !== null &&
|
|
14
|
+
value.nodeType === 8 &&
|
|
15
|
+
value.constructor !== undefined &&
|
|
16
|
+
HTML_ELEMENT_REGEXP.test(value.constructor.name);
|
|
17
|
+
};
|
|
18
|
+
var print = function () { return ''; };
|
|
22
19
|
module.exports = {
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
print: print,
|
|
21
|
+
test: test
|
|
25
22
|
};
|
|
@@ -5,25 +5,27 @@
|
|
|
5
5
|
*
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
var TransformUtils_1 = require("./TransformUtils");
|
|
9
|
+
// replace original ts-jest ConfigSet with this simple interface, as it would require
|
|
10
|
+
// jest-preset-angular to add several babel devDependencies to get the other types
|
|
11
|
+
// inside the ConfigSet right
|
|
8
12
|
/** Angular component decorator TemplateUrl property name */
|
|
9
13
|
var TEMPLATE_URL = 'templateUrl';
|
|
10
14
|
/** Angular component decorator StyleUrls property name */
|
|
11
15
|
var STYLE_URLS = 'styleUrls';
|
|
12
|
-
/** Angular component decorator Styles property name */
|
|
13
|
-
var STYLES = 'styles';
|
|
14
16
|
/** Angular component decorator Template property name */
|
|
15
17
|
var TEMPLATE = 'template';
|
|
16
18
|
/** Node require function name */
|
|
17
19
|
var REQUIRE = 'require';
|
|
18
20
|
/**
|
|
19
|
-
* Property names
|
|
21
|
+
* Property names anywhere in an angular project to transform
|
|
20
22
|
*/
|
|
21
|
-
var TRANSFORM_PROPS = [TEMPLATE_URL,
|
|
23
|
+
var TRANSFORM_PROPS = [TEMPLATE_URL, STYLE_URLS];
|
|
22
24
|
/**
|
|
23
25
|
* Transformer ID
|
|
24
26
|
* @internal
|
|
25
27
|
*/
|
|
26
|
-
exports.name = 'angular-component-inline-
|
|
28
|
+
exports.name = 'angular-component-inline-files';
|
|
27
29
|
// increment this each time the code is modified
|
|
28
30
|
/**
|
|
29
31
|
* Transformer Version
|
|
@@ -39,14 +41,15 @@ function factory(cs) {
|
|
|
39
41
|
* Our compiler (typescript, or a module with typescript-like interface)
|
|
40
42
|
*/
|
|
41
43
|
var ts = cs.compilerModule;
|
|
44
|
+
var createStringLiteral = TransformUtils_1.getCreateStringLiteral(ts);
|
|
42
45
|
/**
|
|
43
|
-
* Traverses the AST down to the relevant assignments in the
|
|
44
|
-
*
|
|
46
|
+
* Traverses the AST down to the relevant assignments anywhere in the file
|
|
47
|
+
* and returns a boolean indicating if it should be transformed.
|
|
45
48
|
*/
|
|
46
49
|
function isPropertyAssignmentToTransform(node) {
|
|
47
|
-
return ts.isPropertyAssignment(node) &&
|
|
50
|
+
return (ts.isPropertyAssignment(node) &&
|
|
48
51
|
ts.isIdentifier(node.name) &&
|
|
49
|
-
TRANSFORM_PROPS.includes(node.name.text);
|
|
52
|
+
TRANSFORM_PROPS.includes(node.name.text));
|
|
50
53
|
}
|
|
51
54
|
/**
|
|
52
55
|
* Clones the assignment and manipulates it depending on its name.
|
|
@@ -57,29 +60,29 @@ function factory(cs) {
|
|
|
57
60
|
var assignmentNameText = mutableAssignment.name.text;
|
|
58
61
|
switch (assignmentNameText) {
|
|
59
62
|
case TEMPLATE_URL:
|
|
60
|
-
//
|
|
61
|
-
|
|
63
|
+
// replace 'templateUrl' with 'template'
|
|
64
|
+
// reuse the right-hand-side literal (the filepath) from the assignment
|
|
65
|
+
var pathLiteral = mutableAssignment.initializer;
|
|
62
66
|
// fix templatePathLiteral if it was a non-relative path
|
|
63
|
-
if (ts.isStringLiteral(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
templatePathLiteral = ts.createStringLiteral("./" + templatePathStringLiteral.text);
|
|
67
|
+
if (ts.isStringLiteral(pathLiteral)) {
|
|
68
|
+
// match if it does not start with ./ or ../ or /
|
|
69
|
+
if (pathLiteral.text &&
|
|
70
|
+
!pathLiteral.text.match(/^(\.\/|\.\.\/|\/)/)) {
|
|
71
|
+
// make path relative by prepending './'
|
|
72
|
+
pathLiteral = createStringLiteral("./" + pathLiteral.text);
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
|
-
// replace 'templateUrl' with 'template'
|
|
73
|
-
mutableAssignment.name = ts.createIdentifier(TEMPLATE);
|
|
74
75
|
// replace current initializer with require(path)
|
|
75
|
-
|
|
76
|
+
var requireCall = ts.createCall(
|
|
76
77
|
/* expression */ ts.createIdentifier(REQUIRE),
|
|
77
78
|
/* type arguments */ undefined,
|
|
78
|
-
/* arguments array */ [
|
|
79
|
+
/* arguments array */ [pathLiteral]);
|
|
80
|
+
mutableAssignment.name = ts.createIdentifier(TEMPLATE);
|
|
81
|
+
mutableAssignment.initializer = requireCall;
|
|
79
82
|
break;
|
|
80
|
-
case STYLES:
|
|
81
83
|
case STYLE_URLS:
|
|
82
|
-
// replace
|
|
84
|
+
// replace styleUrls value with emtpy array
|
|
85
|
+
// inlining all urls would be way more complicated and slower
|
|
83
86
|
mutableAssignment.initializer = ts.createArrayLiteral();
|
|
84
87
|
break;
|
|
85
88
|
}
|
|
@@ -96,24 +99,20 @@ function factory(cs) {
|
|
|
96
99
|
* @param node The node to be visited
|
|
97
100
|
*/
|
|
98
101
|
var visitor = function (node) {
|
|
99
|
-
var resultNode;
|
|
102
|
+
var resultNode = node;
|
|
100
103
|
// before we create a deep clone to modify, we make sure that
|
|
101
104
|
// this is an assignment which we want to transform
|
|
102
105
|
if (isPropertyAssignmentToTransform(node)) {
|
|
103
106
|
// get transformed node with changed properties
|
|
104
107
|
resultNode = transfromPropertyAssignmentForJest(node);
|
|
105
108
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
resultNode = ts.visitEachChild(node, visitor, ctx);
|
|
109
|
-
}
|
|
109
|
+
// look for interesting assignments inside this node in any case
|
|
110
|
+
resultNode = ts.visitEachChild(resultNode, visitor, ctx);
|
|
110
111
|
// finally return the currently visited node
|
|
111
112
|
return resultNode;
|
|
112
113
|
};
|
|
113
114
|
return visitor;
|
|
114
115
|
}
|
|
115
|
-
return function (ctx) {
|
|
116
|
-
return function (sf) { return ts.visitNode(sf, createVisitor(ctx, sf)); };
|
|
117
|
-
};
|
|
116
|
+
return function (ctx) { return function (sf) { return ts.visitNode(sf, createVisitor(ctx, sf)); }; };
|
|
118
117
|
}
|
|
119
118
|
exports.factory = factory;
|