public icon indicating copy to clipboard operation
public copied to clipboard

[Docs] Improvement of the setup of paths for NodeJS with TypeScript and non-Jest

Open Alexsey opened this issue 1 year ago • 0 comments

Issue description or question

In the section on how to set up TypeScript paths under NodeJS environment for non-Jest test runner, there is a code example with requiring tsconfig.json

The problem with this code is that TypeScript allows tsconfig.json to have comments, trailing commas, and (probably) some other deviations from the JSON standard

I want to propose changing this code snippet to use the TypeScript native readConfigFile function instead of require, so it will work more reliably:

module.exports = () => {
  return {
    files: [
      'tsconfig.json',
      ...
    ],

    ...

    setup: () => {
      if (global._tsconfigPathsRegistered) return;
      const ts = require('typescript');
      const tsConfigPaths = require('tsconfig-paths');
      const tsConfig = ts.readConfigFile('./tsconfig.json', ts.sys.readFile).config;
      tsConfigPaths.register({
        baseUrl: tsConfig.compilerOptions.baseUrl,
        paths: tsConfig.compilerOptions.paths
      });
      global._tsconfigPathsRegistered = true;
    }
  };
};

This change allowed me to mitigate the crash on Wallaby startup because our tsconfig.json had comments

Alexsey avatar Dec 06 '23 13:12 Alexsey