From 0b7413b235c7d796c8e992fc1a4cd5b67ac4b8bf Mon Sep 17 00:00:00 2001 From: Kilian Panot Date: Fri, 16 Aug 2024 16:37:31 +0900 Subject: [PATCH] fix(test): tsconfig.base parsing to support comments --- jest.config.ut.js | 10 +++++++--- packages/@o3r/core/package.json | 2 +- .../templates/workspace/jest.config.ut.js.template | 8 ++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/jest.config.ut.js b/jest.config.ut.js index 8d41e24f9e..76d3df0da3 100644 --- a/jest.config.ut.js +++ b/jest.config.ut.js @@ -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 @@ -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, `/${relativePath}/${path}`]) diff --git a/packages/@o3r/core/package.json b/packages/@o3r/core/package.json index a025a7c566..b57943f625 100644 --- a/packages/@o3r/core/package.json +++ b/packages/@o3r/core/package.json @@ -154,7 +154,7 @@ "@nx/eslint-plugin": "~19.5.0", "jsonc-eslint-parser": "~2.4.0", "eslint-import-resolver-node": "^0.3.9", - "eslint-plugin-jest": "~28.7.0", + "eslint-plugin-jest": "~28.8.0", "eslint-plugin-jsdoc": "~48.11.0", "eslint-plugin-prefer-arrow": "~1.2.3", "eslint-plugin-unicorn": "^54.0.0", diff --git a/packages/@o3r/testing/schematics/ng-add/templates/workspace/jest.config.ut.js.template b/packages/@o3r/testing/schematics/ng-add/templates/workspace/jest.config.ut.js.template index 231b08b47f..35e3244f72 100644 --- a/packages/@o3r/testing/schematics/ng-add/templates/workspace/jest.config.ut.js.template +++ b/packages/@o3r/testing/schematics/ng-add/templates/workspace/jest.config.ut.js.template @@ -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] = `/${relativePath}/${path}`;