Skip to content

Commit

Permalink
fix(test): tsconfig.base parsing to support comments (#2069)
Browse files Browse the repository at this point in the history
## Proposed change

Correctly parse tsconfig.base.json in UT config to support comments
  • Loading branch information
kpanot authored Aug 23, 2024
2 parents 7771b4e + 0b7413b commit b9ad713
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 7 additions & 3 deletions jest.config.ut.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { relative } = require('node:path');
const { resolve, relative } = require('node:path');
const { readFileSync } = require('node:fs');
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig.base');
const ts = require('typescript');

globalThis.ngJest = {
skipNgcc: true
Expand All @@ -11,10 +12,13 @@ globalThis.ngJest = {
* @param rootDir {string}
* @param isAngularSetup {boolean}
* @param options.tsconfig {string}
* @param options.baseTsconfig {string}
* @returns {import('ts-jest/dist/types').JestConfigWithTsJest}
*/
module.exports.getJestProjectConfig = (rootDir, isAngularSetup, options) => {
const relativePath = relative(rootDir, __dirname);
const baseTsconfigPath = options?.baseTsconfig ?? resolve(__dirname, './tsconfig.base.json');
const { compilerOptions } = ts.parseConfigFileTextToJson(baseTsconfigPath, readFileSync(baseTsconfigPath, { encoding: 'utf-8' })).config;
const relativePath = relative(rootDir, options?.baseTsconfig ? __dirname(options.baseTsconfig) : __dirname);
const moduleNameMapper = Object.fromEntries(
Object.entries(pathsToModuleNameMapper(compilerOptions.paths))
.map(([moduleName, path]) => [moduleName, `<rootDir>/${relativePath}/${path}`])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const { join, relative } = require('node:path');
const { join, resolve, relative } = require('node:path');
const { readFileSync } = require('node:fs');
const { pathsToModuleNameMapper } = require('ts-jest');
const ts = require('typescript');
// In the following statement, replace `./tsconfig.json` with the path to your `tsconfig` file
// which contains the path mapping (ie the `compilerOptions.paths` option):
const { compilerOptions } = require('./tsconfig.json');

globalThis.ngJest = {
skipNgcc: true
};

const baseTsconfigPath = resolve(__dirname, './tsconfig.json');

/**
* @param rootDir {string}
* @param isAngularSetup {boolean}
* @returns {import('ts-jest/dist/types').JestConfigWithTsJest}
*/
module.exports.getJestProjectConfig = (rootDir, isAngularSetup) => {
const { compilerOptions } = ts.parseConfigFileTextToJson(baseTsconfigPath, readFileSync(baseTsconfigPath, { encoding: 'utf-8' })).config;
const relativePath = relative(rootDir, __dirname);
const moduleNameMapper = Object.entries(pathsToModuleNameMapper(compilerOptions.paths || {})).reduce((acc, [moduleName, path]) => {
acc[moduleName] = `<rootDir>/${relativePath}/${path}`;
Expand Down

0 comments on commit b9ad713

Please sign in to comment.