[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
intojest.config.js
(obviously bad) - or: reading a
tsconfig.json
usingts.readConfigFile
only (bad because this doesn’t follow inheritance defined usingextends
)
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:
- Created 2 years ago
- Reactions:4
- Comments:9
Top GitHub Comments
@PhiLhoSoft You also can try use paths configuration for jest moduleNameMapper with jest-resolver-tsconfig-paths like this.
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:
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.