Skip to content

Commit a692cc6

Browse files
authored
feat: Support tsconfig.spec.json in root dir (#309)
Angular version >= 8 project do locate all of their tsconfig files in the root directory by default. This preset now supports the config files in the root directory by default as well, without any necessity to adjust the jest configuration.
1 parent 7c72e93 commit a692cc6

File tree

7 files changed

+29
-26
lines changed

7 files changed

+29
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
* (**BREAKING**): Restructure project with `src` and `build` folder ([#307](https://github.com/thymikee/jest-preset-angular/pull/307)).
66

7+
* (**BREAKING**): Support `tsconfig.spec.json` in root folder by default ([#309](https://github.com/thymikee/jest-preset-angular/pull/309)).
8+
79
#### Migration Guide
810
* 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.
911
* 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.
12+
* 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).
1013

1114
### v7.1.0
1215

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ By Angular CLI defaults you'll have a `src/test.ts` file which will be picked up
5151
module.exports = {
5252
globals: {
5353
'ts-jest': {
54-
tsConfig: '<rootDir>/src/tsconfig.spec.json',
54+
tsConfig: '<rootDir>/tsconfig.spec.json',
5555
stringifyContentPathRegex: '\\.html$',
5656
astTransformers: [
5757
require.resolve('./build/InlineFilesTransformer'),
@@ -306,7 +306,7 @@ Override `globals` object in Jest config:
306306
"jest": {
307307
"globals": {
308308
"ts-jest": {
309-
"tsConfig": "<rootDir>/src/tsconfig.custom.json",
309+
"tsConfig": "<rootDir>/tsconfig.custom.json",
310310
"stringifyContentPathRegex": "\\.html$",
311311
"astTransformers": [
312312
"jest-preset-angular/build/InlineFilesTransformer",
@@ -373,7 +373,7 @@ A default `tsconfig.spec.json` after modifying will look like this
373373
}
374374
```
375375

376-
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`.
376+
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`.
377377

378378
#### Allow JS files in your TS `compilerOptions`
379379

example/angular.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"index": "src/index.html",
1818
"main": "src/main.ts",
1919
"polyfills": "src/polyfills.ts",
20-
"tsConfig": "src/tsconfig.app.json",
20+
"tsConfig": "tsconfig.app.json",
2121
"assets": [
2222
"src/favicon.ico",
2323
"src/assets"
@@ -69,7 +69,7 @@
6969
"options": {
7070
"main": "src/karmaTest.ts",
7171
"polyfills": "src/polyfills.ts",
72-
"tsConfig": "src/tsconfig.spec.json",
72+
"tsConfig": "tsconfig.spec.json",
7373
"karmaConfig": "src/karma.conf.js",
7474
"styles": [
7575
"styles.css"
@@ -85,8 +85,8 @@
8585
"builder": "@angular-devkit/build-angular:tslint",
8686
"options": {
8787
"tsConfig": [
88-
"src/tsconfig.app.json",
89-
"src/tsconfig.spec.json"
88+
"tsconfig.app.json",
89+
"tsconfig.spec.json"
9090
],
9191
"exclude": [
9292
"**/node_modules/**"

example/src/tsconfig.app.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

example/tsconfig.app.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./out-tsc/app",
5+
"module": "es2015",
6+
"baseUrl": "src"
7+
},
8+
"exclude": [
9+
"src/karmaTest.ts",
10+
"src/**/*.spec.ts"
11+
]
12+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
2-
"extends": "../tsconfig.json",
2+
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"outDir": "../out-tsc/spec",
4+
"outDir": "./out-tsc/spec",
55
"module": "commonjs",
66
"target": "es5",
7-
"baseUrl": "",
7+
"baseUrl": "src",
88
"types": ["jest", "node"],
99
"allowJs": true
1010
},
1111
"files": [
12-
"karmaTest.ts"
12+
"src/karmaTest.ts"
1313
],
1414
"include": [
15-
"**/*.spec.ts",
16-
"**/*.d.ts"
15+
"src/**/*.spec.ts",
16+
"src/**/*.d.ts"
1717
]
1818
}

jest-preset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
globals: {
33
'ts-jest': {
4-
tsConfig: '<rootDir>/src/tsconfig.spec.json',
4+
tsConfig: '<rootDir>/tsconfig.spec.json',
55
stringifyContentPathRegex: '\\.html$',
66
astTransformers: [
77
require.resolve('./build/InlineFilesTransformer'),

0 commit comments

Comments
 (0)