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

Comments in tsconfig.json #139

Closed
jonaskello opened this issue Mar 23, 2017 · 6 comments · Fixed by #140
Closed

Comments in tsconfig.json #139

jonaskello opened this issue Mar 23, 2017 · 6 comments · Fixed by #140
Labels

Comments

@jonaskello
Copy link

  • Issue

Typescript allows comments in tsconfig.json. When I started using ts-jest it did not like my comments. I know JSON itself does not allow comments but it would be nice if ts-jest could follow typescript on this.

Specifically I got this error:

SyntaxError: /xxx/src/tsconfig.json: Unexpected token / in JSON at positio
n 67
        at JSON.parse (<anonymous>)
  • Expected behavior

Comments OK in tsconfig.json.

  • Proposed solution

I think one way to solve it would be to use the tsconfig package that replicates how typescript parses tsconfig.

@kulshekhar
Copy link
Owner

@jonaskello thank you for reporting this and proposing a solution. The fix is in and has been published.

@jonaskello
Copy link
Author

@kulshekhar Thanks for the quick fix, now I can add my comments back in :-).

@jonaskello
Copy link
Author

I tested some more and found one case where it does not work. If you have extends in tsconfig.json then it uses regular require, please see this line. So in the base tsconfig comments does not work yet.

@kulshekhar
Copy link
Owner

@jonaskello thanks again! The fix is in and published

@b3nk3
Copy link

b3nk3 commented Feb 8, 2024

This is still broken, when I have a comment in the tsconfig.json and using this:

import type { Config } from 'jest';
import { pathsToModuleNameMapper } from 'ts-jest';
import { compilerOptions } from './tsconfig.json';

const config: Config = {
  moduleNameMapper: {
    ...pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/', useESM: true }),
  },
};

export default config;

@SimonSimCity
Copy link

SimonSimCity commented May 15, 2024

@b3nk3 It works when replacing the import of the json-configuration by a custom solution described in https://stackoverflow.com/a/53898219/517914. According to the author, this should also work when extending the ts-config file.

/** @type {import('ts-jest').JestConfigWithTsJest} */
import type { Config } from 'jest';
import { pathsToModuleNameMapper } from 'ts-jest';
import ts from 'typescript';

const configFileName = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json');
const configFile = ts.readConfigFile(configFileName!, ts.sys.readFile);
const compilerOptions = ts.parseJsonConfigFileContent(configFile.config, ts.sys, './');

const config: Config = {
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.options.paths!, {
    prefix: '<rootDir>',
  }),
};

export default config;

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

Successfully merging a pull request may close this issue.

4 participants