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.
Disabling/enabling
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.
Advanced configuration
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 totsconfigoption exclude with the only difference is that in TypeScript,excludewill also exclude files from compilation process.pretty: Enables/disables colorful and pretty output of errors (default: enabled).
Examples
Disabling diagnostics
import type { Config } from 'jest'
const jestConfig: Config = {
// [...]
transform: {
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
diagnostics: false,
},
],
},
}
export default jestConfig
Advanced options
Enabling diagnostics for test files only
Assuming all your test files ends with .spec.ts or .test.ts, using the following config will enable error reporting only for those files:
import type { Config } from 'jest'
const jestConfig: Config = {
// [...]
transform: {
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
diagnostics: {
exclude: ['!**/*.(spec|test).ts'],
},
},
],
},
}
export default jestConfig
Do not fail on first error
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:
import type { Config } from 'jest'
const jestConfig: Config = {
// [...]
transform: {
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
diagnostics: {
warnOnly: true,
},
},
],
},
}
export default jestConfig
Ignoring some error codes
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
stringwith a code (example"1009","TS1009"or"TS1009") - A
stringwith a list of the above (example:"1009, TS2571, 4072") - An
arrayof one or more from1or3(example:[1009, "TS2571", "6031"])
import type { Config } from 'jest'
const jestConfig: Config = {
// [...]
transform: {
// '^.+\\.[tj]sx?$' to process ts,js,tsx,jsx with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process ts,js,tsx,jsx,mts,mjs,mtsx,mjsx with `ts-jest`
'^.+\\.tsx?$': [
'ts-jest',
{
diagnostics: {
ignoreCodes: [2571, 6031, 18003],
},
},
],
},
}
export default jestConfig