title |
---|
Diagnostics option |
The diagnostics
option configures error reporting.
It can both be enabled/disabled entirely or limited to a specific type of errors and/or files.
If a diagnostic is not filtered out, ts-jest
will fail the compilation and your test.
By default all diagnostics are enabled. This is the same as setting the diagnostics
option to true
.
To disable all diagnostics, set diagnostics
to false
.
This might lead to slightly better performance, especially if you're not using Jest's cache.
The diagnostics
option's value can also accept an object for more advanced configuration. Each config. key is optional:
warnOnly
: If specified andtrue
, diagnostics will be reported but won't stop compilation (default: disabled).ignoreCodes
: List of TypeScript error codes to ignore. Complete list can be found there. By default here are the ones ignored:6059
: 'rootDir' is expected to contain all source files.18002
: The 'files' list in config file is empty. (it is strongly recommended including this one)18003
: No inputs were found in config file.
exclude
: If specified, diagnostics of source files which path matches will be ignored. This works a bit similar totsconfig
option exclude with the only difference is that in TypeScript,exclude
will also exclude files from compilation process.pretty
: Enables/disables colorful and pretty output of errors (default: enabled).
// jest.config.js
module.exports = {
// [...]
globals: {
'ts-jest': {
diagnostics: false,
},
},
}
// OR package.json
{
// [...]
"jest": {
"globals": {
"ts-jest": {
"diagnostics": false
}
}
}
}
Assuming all your test files ends with .spec.ts
or .test.ts
, using the following config will enable error reporting only for those files:
// jest.config.js
module.exports = {
// [...]
globals: {
'ts-jest': {
diagnostics: {
exclude: ['**/*.spec.ts'],
},
},
},
}
// OR package.json
{
// [...]
"jest": {
"globals": {
"ts-jest": {
"diagnostics": {
"exclude": ["**/*.spec.ts"]
}
}
}
}
}
While some diagnostics are stop-blockers for the compilation, most of them are not. If you want the compilation (and so your tests) to continue when encountering those, set the warnOnly
to true
:
// jest.config.js
module.exports = {
// [...]
globals: {
'ts-jest': {
diagnostics: {
warnOnly: true,
},
},
},
}
// OR package.json
{
// [...]
"jest": {
"globals": {
"ts-jest": {
"diagnostics": {
"warnOnly": true
}
}
}
}
}
All TypeScript error codes can be found there. The ignoreCodes
option accepts this values:
- A single
number
(example:1009
): unique error code to ignore - A
string
with a code (example"1009"
,"TS1009"
or"TS1009"
) - A
string
with a list of the above (example:"1009, TS2571, 4072"
) - An
array
of one or more from1
or3
(example:[1009, "TS2571", "6031"]
)
// jest.config.js
module.exports = {
// [...]
globals: {
'ts-jest': {
diagnostics: {
ignoreCodes: [2571, 6031, 18003],
},
},
},
}
// OR package.json
{
// [...]
"jest": {
"globals": {
"ts-jest": {
"diagnostics": {
"ignoreCodes": [2571, 6031, 18003]
}
}
}
}
}