Skip to content

Commit

Permalink
fix(test): tsconfig.base parsing to support comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kpanot committed Aug 19, 2024
1 parent f442d31 commit 0b7413b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 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
2 changes: 1 addition & 1 deletion packages/@o3r/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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 0b7413b

Please sign in to comment.