Skip to content

Commit

Permalink
feat: Support tsconfig.spec.json in root dir (#309)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wtho authored Sep 18, 2019
1 parent 7c72e93 commit a692cc6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

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

* (**BREAKING**): Support `tsconfig.spec.json` in root folder by default ([#309](https://github.com/thymikee/jest-preset-angular/pull/309)).

#### Migration Guide
* 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.
* 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.
* 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).

### v7.1.0

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ By Angular CLI defaults you'll have a `src/test.ts` file which will be picked up
module.exports = {
globals: {
'ts-jest': {
tsConfig: '<rootDir>/src/tsconfig.spec.json',
tsConfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: [
require.resolve('./build/InlineFilesTransformer'),
Expand Down Expand Up @@ -306,7 +306,7 @@ Override `globals` object in Jest config:
"jest": {
"globals": {
"ts-jest": {
"tsConfig": "<rootDir>/src/tsconfig.custom.json",
"tsConfig": "<rootDir>/tsconfig.custom.json",
"stringifyContentPathRegex": "\\.html$",
"astTransformers": [
"jest-preset-angular/build/InlineFilesTransformer",
Expand Down Expand Up @@ -373,7 +373,7 @@ A default `tsconfig.spec.json` after modifying will look like this
}
```

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`.
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`.

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

Expand Down
8 changes: 4 additions & 4 deletions example/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"tsConfig": "tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
Expand Down Expand Up @@ -69,7 +69,7 @@
"options": {
"main": "src/karmaTest.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"styles.css"
Expand All @@ -85,8 +85,8 @@
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
"tsconfig.app.json",
"tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
Expand Down
12 changes: 0 additions & 12 deletions example/src/tsconfig.app.json

This file was deleted.

12 changes: 12 additions & 0 deletions example/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"module": "es2015",
"baseUrl": "src"
},
"exclude": [
"src/karmaTest.ts",
"src/**/*.spec.ts"
]
}
12 changes: 6 additions & 6 deletions example/src/tsconfig.spec.json → example/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"extends": "../tsconfig.json",
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"outDir": "./out-tsc/spec",
"module": "commonjs",
"target": "es5",
"baseUrl": "",
"baseUrl": "src",
"types": ["jest", "node"],
"allowJs": true
},
"files": [
"karmaTest.ts"
"src/karmaTest.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
2 changes: 1 addition & 1 deletion jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
globals: {
'ts-jest': {
tsConfig: '<rootDir>/src/tsconfig.spec.json',
tsConfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: [
require.resolve('./build/InlineFilesTransformer'),
Expand Down

0 comments on commit a692cc6

Please sign in to comment.