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

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

Open
Alexsey opened this issue Dec 6, 2023 · 0 comments

Comments

@Alexsey
Copy link

Alexsey commented Dec 6, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant