question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature]: Make it easier to extend an existing tsconfig

See original GitHub issue

🚀 Feature Proposal

I tried extending an existing tsconfig.json file by parsing it and then extending it like so:

const ts = require('typescript');
const loaded = ts.readConfigFile('./tsconfig.json', ts.sys.readFile);
const basePath = path.dirname(configPath); // equal to "getDirectoryPath" from ts, at least in our case.
const { options } = ts.parseJsonConfigFileContent(loaded.config, ts.sys, basePath);


module.exports = {
	// […]

	// A set of global variables that need to be available in all test environments
	globals: {
		'ts-jest': {
			tsconfig: {
				...options,
				importHelpers: false,
			}
		},
	},

	// […]
};

However, the parseJsonConfigFileContent transforms some of the strings in compilerOptions to their enum values, like target, module, jsx, and moduleResolution, which will yield errors such as:

    error TS5024: Compiler option 'target' requires a value of type string.
    error TS5024: Compiler option 'module' requires a value of type string.

I propose adding a ts-jest config option compilerOptions and deprecate setting config options directly on tsconfig. Instead tsconfig can either point to a path, in which case the compilerOptions passed (if any) extend the config from that path or to false, in which case the default config is used (as before) and compilerOptions (if any) can be used to override it.

Motivation

Extending an existing tsconfig.json requires either:

  • creating a custom tsconfig file that extends from the other (bad because it needlessly requires a file, even when I only want to override a single config)
  • or: copy-pasting the entire compilerOptions into jest.config.js (obviously bad)
  • or: reading a tsconfig.json using ts.readConfigFile only (bad because this doesn’t follow inheritance defined using extends)

It would be nice if this was a built-in feature of ts-jest.

Example

module.exports = {
	// […]

	// A set of global variables that need to be available in all test environments
	globals: {
		'ts-jest': {
			tsconfig: './tsconfig.json',
			compilerOptions: {
				importHelpers: false
			},
		},
	},

	// […]
};

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

3reactions
viT-1commented, Jan 12, 2022

@PhiLhoSoft You also can try use paths configuration for jest moduleNameMapper with jest-resolver-tsconfig-paths like this.

1reaction
dzearingcommented, Nov 30, 2022

Just hit this. I have a monorepo full of packages (about 150) I want to use each package’s default tsconfig but override a few things for jest runs. There isn’t a great way to do this without creating test-only configs for each package.

In my ts-jest config:

{
  // use the project config where jest is run.
  tsconfig: './tsconfig.json'
}

So… how can I turn on cjs and turn off isolatedModules for jest runs but still use everything else from the config?

I was hoping tsconfig was array, but it’s not. All I can think of is to drop a temporary config on disk which extends the original and reference that. Which is undesirable.

If we had what the OP suggested which was separate compilerOptions, that’d work great, but yeah it requires merging.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Option: extends - TypeScript
How this setting affects your build.
Read more >
TSConfig Reference - Docs on every TSConfig option
The value of extends is a string which contains a path to another configuration file to inherit from. The path may use Node.js...
Read more >
Documentation - What is a tsconfig.json - TypeScript
The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file...
Read more >
Documentation - tsc CLI Options - TypeScript
Flag Type Default ‑‑allowJs boolean false ‑‑allowUmdGlobalAccess boolean false ‑‑allowUnreachableCode boolean
Read more >
Documentation - Project References - TypeScript
A long-awaited feature is smart incremental builds for TypeScript projects. In 3.0 you can use the --build flag with tsc . This is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found