Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): add path to module mapper for jest tests #532

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/lib/application/application.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 () => {
Expand All @@ -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',
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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',
Expand All @@ -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',
]);
});
});
23 changes: 23 additions & 0 deletions src/lib/application/files/ts/jest.config.ts
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/',
});

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;
19 changes: 1 addition & 18 deletions src/lib/application/files/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
9 changes: 0 additions & 9 deletions src/lib/application/files/ts/test/jest-e2e.json

This file was deleted.

19 changes: 19 additions & 0 deletions src/lib/application/files/ts/test/jest-e2e.ts
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/../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;
6 changes: 4 additions & 2 deletions src/lib/application/files/ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
"incremental": true,
"resolveJsonModule": true,
"paths": {}
}
}
}