Skip to content

Commit 357d30e

Browse files
committed
👀 JestJS testing for monorepos
1 parent abc086c commit 357d30e

11 files changed

+81
-29
lines changed

apps/api/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,19 @@ npm run api:start:prod
5757
```bash
5858
# unit tests
5959
npm run api:test
60+
# for api project
61+
npx jest --projects=apps/api --roots=src
62+
6063

6164
# e2e tests
6265
npm run api:test:e2e
66+
# for api project
67+
npx jest --projects=apps/api --roots=e2e
6368

6469
# test coverage
6570
npm run api:test:cov
71+
# for api project
72+
npx jest --projects=apps/api --coverage
6673
```
6774

6875
## Support

apps/api/test/app.e2e-spec.ts renamed to apps/api/e2e/app.e2e-spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as request from 'supertest';
1+
import request from 'supertest';
22
import { Test } from '@nestjs/testing';
33
import { AppModule } from '../src/app.module';
44
import { INestApplication } from '@nestjs/common';

apps/api/jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const jestBase = require('../../jest.config.base.js');
2+
module.exports = {
3+
...jestBase,
4+
"coverageDirectory": "../../dist/apps/api/coverage",
5+
};

apps/api/test/jest-e2e.json

-8
This file was deleted.

apps/api/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"experimentalDecorators": true,
1212
"target": "es6",
1313
"sourceMap": true,
14-
"outDir": "../../dist/apps/api",
14+
// "outDir": "../../dist/apps/api",
15+
"outDir": "../../dist/out-tsc/apps/api",
1516
"baseUrl": "./src"
1617
},
1718
"include": [

apps/api/tsconfig.spec.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
{
2-
"extends": "tsconfig.api.json",
2+
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"types": ["jest", "node"]
4+
"outDir": "../../dist/out-tsc/apps/api",
5+
"types": [
6+
"jest",
7+
"node"
8+
]
59
},
6-
"include": ["**/*.spec.ts", "**/*.d.ts"]
10+
"include": [
11+
"**/*.spec.ts",
12+
"**/*.d.ts"
13+
],
14+
"exclude": [
15+
"node_modules"
16+
]
717
}

apps/webapp-e2e/jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const jestBase = require('../../jest.config.base.js');
2+
module.exports = {
3+
...jestBase,
4+
"coverageDirectory": "../../dist/apps/webapp-e2e/coverage",
5+
};

apps/webapp/jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const jestBase = require('../../jest.config.base.js');
2+
module.exports = {
3+
...jestBase,
4+
"coverageDirectory": "../../dist/apps/webapp/coverage",
5+
};

jest.config.base.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
"moduleFileExtensions": ["js", "json", "ts"],
3+
"globals": {
4+
"ts-jest": {
5+
"tsConfigFile": "tsconfig.spec.json"
6+
},
7+
// // "__TRANSFORM_HTML__": true,
8+
// // "testResultsProcessor": "./node_modules/jest-html-reporter"
9+
},
10+
collectCoverage: true,
11+
"coverageThreshold": {
12+
"global": {
13+
"branches": 60,
14+
"functions": 60,
15+
"lines": 60,
16+
"statements": -20
17+
}
18+
},
19+
"transform": {
20+
"^.+\\.(t|j)s$": "ts-jest"
21+
},
22+
"testRegex": ".spec.ts$",
23+
// "moduleNameMapper": {
24+
// "@ngx-starter-kit/(.*)": "<rootDir>/libs/$1"
25+
// }
26+
projects: [
27+
'apps/*',
28+
// 'libs/*'
29+
],
30+
testPathIgnorePatterns: [
31+
'apps/webapp',
32+
'apps/webapp-e2e'
33+
],
34+
};

jest.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const common = require('./jest.config.base');
2+
3+
module.exports = {
4+
...common,
5+
verbose: true,
6+
};

package.json

+3-16
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
"api:prestart:prod": "rm -rf dist && tsc -p ./apps/api/tsconfig.json",
4646
"api:webpack": "webpack --config ./apps/api/webpack.config.js",
4747
"// - API Test": "API Testing",
48-
"api:test": "jest",
49-
"api:test:watch": "jest --watch",
48+
"api:test": "jest --roots=src",
49+
"api:test:watch": "jest --roots=src --watch",
50+
"api:test:e2e": "jest --roots=e2e",
5051
"api:test:cov": "jest --coverage",
51-
"api:test:e2e": "jest --config ./apps/api/test/jest-e2e.json",
5252
"// - CI": "Ci Testing"
5353
},
5454
"private": true,
@@ -152,18 +152,5 @@
152152
"webpack-bundle-analyzer": "^2.13.1",
153153
"webpack-cli": "^3.0.8",
154154
"webpack-node-externals": "^1.7.2"
155-
},
156-
"jest": {
157-
"moduleFileExtensions": [
158-
"js",
159-
"json",
160-
"ts"
161-
],
162-
"rootDir": "apps/api/src",
163-
"testRegex": ".spec.ts$",
164-
"transform": {
165-
"^.+\\.(t|j)s$": "ts-jest"
166-
},
167-
"coverageDirectory": "../../../dist/api/coverage"
168155
}
169156
}

0 commit comments

Comments
 (0)