You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a PoC. Jest 24 works without any changes from our side. It added some defaults to TS but we use slightly different ones (e.g. no `.tsx`).
However, until `ts-jest` updates, we'll likely need to hold off with the upgrade, because of peer dep warnings.
Fixes#223
A preset of [Jest](http://facebook.github.io/jest) configuration for [Angular](https://angular.io/) projects.
6
7
7
8
This is a part of the article: [Testing Angular faster with Jest](https://www.xfive.co/blog/testing-angular-faster-jest/).
8
9
9
-
*Note: This preset does not suport 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).*
10
+
_Note: This preset does not suport 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)._
10
11
11
12
## Installation
12
13
@@ -25,66 +26,62 @@ import 'jest-preset-angular';
25
26
import'./jestGlobalMocks'; // browser mocks globally available for every test
26
27
```
27
28
28
-
*Note: feel free to copy the `jestGlobalMocks.ts` file from the example directory and save it next to the `setupJest.ts` file.*
29
+
_Note: feel free to copy the `jestGlobalMocks.ts` file from the example directory and save it next to the `setupJest.ts` file._
*`<rootDir>` is a special syntax for root of your project (here by default it's project's root /)
79
-
* 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
80
-
*`"transform"` – run every TS, JS, or HTML file through so called *preprocessor* (we'll get there); this lets Jest understand non-JS syntax
81
-
*`"testMatch"` – we want to run Jest on files that matches this glob
82
-
*`"moduleFileExtensions"` – our modules are TypeScript and JavaScript files
83
-
*`"moduleNameMapper"` – if you're using absolute imports here's how to tell Jest where to look for them; uses regex
84
-
*`"setupTestFrameworkScriptFile"` – this is the heart of our config, in this file we'll setup and patch environment within tests are running
85
-
*`"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.
73
+
74
+
-`<rootDir>` is a special syntax for root of your project (here by default it's project's root /)
75
+
- 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
76
+
-`"transform"` – run every TS, JS, or HTML file through so called _preprocessor_ (we'll get there); this lets Jest understand non-JS syntax
77
+
-`"testMatch"` – we want to run Jest on files that matches this glob
78
+
-`"moduleFileExtensions"` – our modules are TypeScript and JavaScript files
79
+
-`"moduleNameMapper"` – if you're using absolute imports here's how to tell Jest where to look for them; uses regex
80
+
-`"setupFilesAfterEnv"` – this is the heart of our config, in this file we'll setup and patch environment within tests are running
81
+
-`"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.
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.
89
86
90
87
## Angular testing environment setup
@@ -98,6 +95,7 @@ If you look at your `src/test.ts` (or similar bootstrapping test file) file you'
@@ -293,7 +295,7 @@ Override `globals` object in Jest config:
293
295
{
294
296
"jest": {
295
297
"globals": {
296
-
"ts-jest": {
298
+
"ts-jest": {
297
299
"tsConfigFile": "src/tsconfig.custom.json"
298
300
},
299
301
"__TRANSFORM_HTML__": true
@@ -309,7 +311,9 @@ If you choose to overide `globals` in order to point at a specific tsconfig, you
309
311
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.
310
312
311
313
#### Adjust your `tsconfig.spec.json`:
314
+
312
315
Since Angular released v6, the default `tsconfig.json` and `tsconfig.spec.json` have been changed. Therefore, `jest` will throw an error
@@ -320,6 +324,7 @@ Since Angular released v6, the default `tsconfig.json` and `tsconfig.spec.json`
320
324
What you need to do is adjust your `tsconfig.spec.json` to add the option `"module": "commonjs",`
321
325
322
326
A default `tsconfig.spec.json` after modifying will look like this
327
+
323
328
```
324
329
{
325
330
"extends": "../tsconfig.json",
@@ -353,19 +358,23 @@ A default `tsconfig.spec.json` after modifying will look like this
353
358
}
354
359
}
355
360
```
361
+
356
362
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 transforemed, even though they're `node_modules`.
357
363
358
364
#### Allow JS files in your TS `compilerOptions`
365
+
359
366
```json
360
367
{
361
368
"compilerOptions": {
362
369
"allowJs": true
363
370
}
364
371
}
365
372
```
373
+
366
374
This tells `ts-jest` (a preprocessor this preset using to transform TS files) to treat JS files the same as TS ones.
367
375
368
376
#### Transpile js files through `babel-jest`
377
+
369
378
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.
370
379
371
380
1. Install `@babel/preset-env` and add `babel.config.js` (or modify existing if needed) with the following content:
*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!`*.
388
397
389
398
2. Update Jest configuration (by default TypeScript process untranspiled JS files which is source of the problem):
399
+
390
400
```js
391
401
{
392
402
"jest": {
@@ -445,4 +455,3 @@ By default we use JSDOM v13, which requires Node v8+. If you want to use Node in
445
455
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 commit comments