From 79ca44092214254caefd763852117f1874d742c8 Mon Sep 17 00:00:00 2001 From: abouroubi Date: Mon, 30 Nov 2020 18:07:02 +0100 Subject: [PATCH 1/2] refactor(tests): add path to module mapper for jest tests --- src/lib/application/files/ts/jest.config.ts | 23 +++++++++++++++++++ src/lib/application/files/ts/package.json | 19 +-------------- .../application/files/ts/test/jest-e2e.json | 9 -------- src/lib/application/files/ts/test/jest-e2e.ts | 19 +++++++++++++++ src/lib/application/files/ts/tsconfig.json | 6 +++-- 5 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 src/lib/application/files/ts/jest.config.ts delete mode 100644 src/lib/application/files/ts/test/jest-e2e.json create mode 100644 src/lib/application/files/ts/test/jest-e2e.ts diff --git a/src/lib/application/files/ts/jest.config.ts b/src/lib/application/files/ts/jest.config.ts new file mode 100644 index 000000000..6edbac5da --- /dev/null +++ b/src/lib/application/files/ts/jest.config.ts @@ -0,0 +1,23 @@ +import type { Config } from '@jest/types'; +import { pathsToModuleNameMapper } from 'ts-jest/utils'; +import { compilerOptions } from './tsconfig.json'; + + + +const moduleNameMapper = pathsToModuleNameMapper(compilerOptions.paths, { + prefix: '/', +}); + +const config: Config.InitialOptions = { + moduleFileExtensions: ['js', 'json', 'ts'], + rootDir: 'src', + testRegex: '.spec.ts$', + moduleNameMapper, + transform: { + '^.+\\.(t|j)s$': 'ts-jest', + }, + coverageDirectory: '../coverage', + testEnvironment: 'node', +}; + +export default config; diff --git a/src/lib/application/files/ts/package.json b/src/lib/application/files/ts/package.json index 53bf36e7d..12cb76c83 100644 --- a/src/lib/application/files/ts/package.json +++ b/src/lib/application/files/ts/package.json @@ -18,7 +18,7 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json" + "test:e2e": "jest --config ./test/jest-e2e.ts" }, "dependencies": { "@nestjs/common": "^7.5.1", @@ -49,22 +49,5 @@ "ts-node": "^9.0.0", "tsconfig-paths": "^3.9.0", "typescript": "^4.0.5" - }, - "jest": { - "moduleFileExtensions": [ - "js", - "json", - "ts" - ], - "rootDir": "src", - "testRegex": ".*\\.spec\\.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - }, - "collectCoverageFrom": [ - "**/*.(t|j)s" - ], - "coverageDirectory": "../coverage", - "testEnvironment": "node" } } diff --git a/src/lib/application/files/ts/test/jest-e2e.json b/src/lib/application/files/ts/test/jest-e2e.json deleted file mode 100644 index e9d912f3e..000000000 --- a/src/lib/application/files/ts/test/jest-e2e.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "moduleFileExtensions": ["js", "json", "ts"], - "rootDir": ".", - "testEnvironment": "node", - "testRegex": ".e2e-spec.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - } -} diff --git a/src/lib/application/files/ts/test/jest-e2e.ts b/src/lib/application/files/ts/test/jest-e2e.ts new file mode 100644 index 000000000..2aedd9fc0 --- /dev/null +++ b/src/lib/application/files/ts/test/jest-e2e.ts @@ -0,0 +1,19 @@ +import type { Config } from '@jest/types'; +import { pathsToModuleNameMapper } from 'ts-jest/utils'; +import { compilerOptions } from '../tsconfig.json'; +const moduleNameMapper = pathsToModuleNameMapper(compilerOptions.paths, { + prefix: '/../src/', +}); + +const config: Config.InitialOptions = { + moduleFileExtensions: ['js', 'json', 'ts'], + rootDir: '.', + testRegex: '.e2e-spec.ts$', + moduleNameMapper, + transform: { + '^.+\\.(t|j)s$': 'ts-jest', + }, + testEnvironment: 'node', +}; + +export default config; diff --git a/src/lib/application/files/ts/tsconfig.json b/src/lib/application/files/ts/tsconfig.json index bf10a2398..b2a343417 100644 --- a/src/lib/application/files/ts/tsconfig.json +++ b/src/lib/application/files/ts/tsconfig.json @@ -10,6 +10,8 @@ "sourceMap": true, "outDir": "./dist", "baseUrl": "./", - "incremental": true + "incremental": true, + "resolveJsonModule": true, + "paths": {} } -} +} \ No newline at end of file From 8978e1828970fface1d68cbc07d92aa345a5dfde Mon Sep 17 00:00:00 2001 From: abouroubi Date: Wed, 2 Dec 2020 20:06:42 +0100 Subject: [PATCH 2/2] test(tests): :white_check_mark: fix application factory tests --- src/lib/application/application.factory.test.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib/application/application.factory.test.ts b/src/lib/application/application.factory.test.ts index faee992e8..091492b38 100644 --- a/src/lib/application/application.factory.test.ts +++ b/src/lib/application/application.factory.test.ts @@ -21,6 +21,7 @@ describe('Application Factory', () => { '/project/.gitignore', '/project/.prettierrc', '/project/README.md', + '/project/jest.config.ts', '/project/nest-cli.json', '/project/package.json', '/project/tsconfig.build.json', @@ -31,7 +32,7 @@ describe('Application Factory', () => { '/project/src/app.service.ts', '/project/src/main.ts', '/project/test/app.e2e-spec.ts', - '/project/test/jest-e2e.json', + '/project/test/jest-e2e.ts', ]); }); it('should manage name to dasherize', async () => { @@ -45,6 +46,7 @@ describe('Application Factory', () => { '/awesome-project/.gitignore', '/awesome-project/.prettierrc', '/awesome-project/README.md', + '/awesome-project/jest.config.ts', '/awesome-project/nest-cli.json', '/awesome-project/package.json', '/awesome-project/tsconfig.build.json', @@ -55,7 +57,7 @@ describe('Application Factory', () => { '/awesome-project/src/app.service.ts', '/awesome-project/src/main.ts', '/awesome-project/test/app.e2e-spec.ts', - '/awesome-project/test/jest-e2e.json', + '/awesome-project/test/jest-e2e.ts', ]); }); it('should manage javascript files', async () => { @@ -96,6 +98,7 @@ describe('Application Factory', () => { '/scope-package/.gitignore', '/scope-package/.prettierrc', '/scope-package/README.md', + '/scope-package/jest.config.ts', '/scope-package/nest-cli.json', '/scope-package/package.json', '/scope-package/tsconfig.build.json', @@ -106,7 +109,7 @@ describe('Application Factory', () => { '/scope-package/src/app.service.ts', '/scope-package/src/main.ts', '/scope-package/test/app.e2e-spec.ts', - '/scope-package/test/jest-e2e.json', + '/scope-package/test/jest-e2e.ts', ]); }); });