|
| 1 | +--- |
| 2 | +title: Diagnostics option |
| 3 | +--- |
| 4 | + |
| 5 | +The `diagnostics` option allows to enable/disable the error reporting. But it can also be used to filter which one should throw during tests, as well from which file it should be reported or not. |
| 6 | + |
| 7 | +A diagnostic can be: |
| 8 | +- an error in your TypeScript config (whether it's a file or inline) |
| 9 | +- syntax errors in some of your TypeScript files (source or tests) |
| 10 | +- type/semantic errors, what TypeScript has actually been made for 😁 |
| 11 | + |
| 12 | +If a diagnostic is not filtered out, it'll fail the compilatin within TSJest, and so will your related test. |
| 13 | + |
| 14 | +### Disabling/enabling |
| 15 | + |
| 16 | +By default all diagnostic are enabled. This is the same as setting the `diagnostics` option to `true`. To disable all diagnostics, set `diagnostics` to `false` (you might experience slightly better performence as well, especially if you disabled Jest cache). |
| 17 | + |
| 18 | +### Advanced configuration |
| 19 | + |
| 20 | +The option's value can also accpet an object for more advanced configuration. Each config. key is optional: |
| 21 | + |
| 22 | +- **`pretty`**: Enables/disable colorful and pretty output of errors (default: _enabled_). |
| 23 | +- **`ignoreCodes`**: List of TypeScript error codes to ignore. Complete list can be found [there](https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json). By default here are the ones ignored: |
| 24 | + - `6059`: _'rootDir' is expected to contain all source files._ |
| 25 | + - `18002`: _The 'files' list in config file is empty._ (it is strongly recommanded to include this one) |
| 26 | + - `18003`: _No inputs were found in config file._ |
| 27 | +- **`pathRegex`**: If specified, diagnostics of source files which path does **not** match will be ignored. |
| 28 | + |
| 29 | +### Examples |
| 30 | + |
| 31 | +#### Disabling diagnostics: |
| 32 | + |
| 33 | +<div class="row"><div class="col-md-6" markdown="block"> |
| 34 | +```js |
| 35 | +// jest.config.js |
| 36 | +module.exports = { |
| 37 | + // [...] |
| 38 | + globals: { |
| 39 | + 'ts-jest': { |
| 40 | + diagnostics: false |
| 41 | + } |
| 42 | + } |
| 43 | +}; |
| 44 | +``` |
| 45 | +</div><div class="col-md-6" markdown="block"> |
| 46 | +```js |
| 47 | +// OR package.json |
| 48 | +{ |
| 49 | + // [...] |
| 50 | + "jest": { |
| 51 | + "globals": { |
| 52 | + "ts-jest": { |
| 53 | + "diagnostics": false |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | +</div></div> |
| 60 | + |
| 61 | +#### Advanced options: |
| 62 | + |
| 63 | +##### Enabling diagnostics for test files only |
| 64 | + |
| 65 | +Assuming all your test files ends with `.spec.ts` or `.test.ts`, using the following config will enable error reporting only for those files: |
| 66 | + |
| 67 | +<div class="row"><div class="col-md-6" markdown="block"> |
| 68 | +```js |
| 69 | +// jest.config.js |
| 70 | +module.exports = { |
| 71 | + // [...] |
| 72 | + globals: { |
| 73 | + 'ts-jest': { |
| 74 | + diagnostics: { |
| 75 | + pathRegex: /\.(spec|test).ts$/ |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +}; |
| 80 | +``` |
| 81 | +</div><div class="col-md-6" markdown="block"> |
| 82 | +```js |
| 83 | +// OR package.json |
| 84 | +{ |
| 85 | + // [...] |
| 86 | + "jest": { |
| 87 | + "globals": { |
| 88 | + "ts-jest": { |
| 89 | + "diagnostics": { |
| 90 | + "pathRegex": "\\.(spec|test)\\.ts$" |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | +</div></div> |
| 98 | + |
| 99 | +##### Ignoring some error codes: |
| 100 | + |
| 101 | +All TypeScript error codes can be found [there](https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json). The `ignoreCodes` option accepts this values: |
| 102 | +1. A single `number` (example: `1009`): unique error code to ignore |
| 103 | +2. A `string`, can be: |
| 104 | + 1. Unique code (example `"1009"`, `"TS1009"` or `"ts1009"`) |
| 105 | + 2. List of the above (example: `"1009, TS2571, 4072 ,ts6031 "`) |
| 106 | +3. An `array` of one or more from `1` or `2.1` (example: `[1009, "TS2571", "6031"]`) |
| 107 | + |
| 108 | +It's advised to use concise types along the list of course: |
| 109 | + |
| 110 | +<div class="row"><div class="col-md-6" markdown="block"> |
| 111 | +```js |
| 112 | +// jest.config.js |
| 113 | +module.exports = { |
| 114 | + // [...] |
| 115 | + globals: { |
| 116 | + 'ts-jest': { |
| 117 | + diagnostics: { |
| 118 | + ignoreCodes: [2571, 6031, 18003] |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +}; |
| 123 | +``` |
| 124 | +</div><div class="col-md-6" markdown="block"> |
| 125 | +```js |
| 126 | +// OR package.json |
| 127 | +{ |
| 128 | + // [...] |
| 129 | + "jest": { |
| 130 | + "globals": { |
| 131 | + "ts-jest": { |
| 132 | + "diagnostics": { |
| 133 | + "ignoreCodes": [2571, 6031, 18003] |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | +</div></div> |
0 commit comments