Skip to content

Commit 13b77d9

Browse files
committed
feat: add option to run TypeScript diagnostics
By creating a `program` we are enabled to run diagnostics on typescipt files in order to emit semantic errors. For the time being I only used one of the many methods this one is `getPreEmitDiagnostics` This enables syntactic & semantic TypeScript error reporting
1 parent 17c2862 commit 13b77d9

9 files changed

Lines changed: 329 additions & 215 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,19 @@ By default Jest ignores everything in `node_modules`. This setting prevents Jest
329329
}
330330
```
331331
### TS compiler && error reporting
332-
- ts-jest only returns syntax errors from [tsc](https://github.com/Microsoft/TypeScript/issues/4864#issuecomment-141567247)
333-
- Non syntactic errors do not show up in [jest](https://github.com/facebook/jest/issues/2168)
334-
- If you only want to run jest if tsc does not output any errors, a workaround is `tsc --noEmit -p . && jest`
332+
If you want to enable Syntactic & Semantic TypeScript error reporting you can enable this through `enableTsDiagnostics` flag;
333+
334+
```json
335+
{
336+
"jest": {
337+
"globals": {
338+
"ts-jest": {
339+
"enableTsDiagnostics": true
340+
}
341+
}
342+
}
343+
}
344+
```
335345

336346
### Known Limitations for hoisting
337347
If the `jest.mock()` calls is placed after actual code, (e.g. after functions or classes) and `skipBabel` is not set,

src/jest-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,5 @@ export interface TsJestConfig {
7474
babelConfig?: BabelTransformOpts;
7575
tsConfigFile?: string;
7676
enableInternalCache?: boolean;
77+
enableTsDiagnostics?: boolean;
7778
}

src/preprocessor.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
cacheFile,
88
getTSConfig,
99
getTSJestConfig,
10+
runTsDiagnostics,
1011
injectSourcemapHook,
1112
} from './utils';
1213

@@ -41,14 +42,18 @@ export function process(
4142
return src;
4243
}
4344

45+
const tsJestConfig = getTSJestConfig(jestConfig.globals);
46+
logOnce('tsJestConfig: ', tsJestConfig);
47+
48+
if (tsJestConfig.enableTsDiagnostics) {
49+
runTsDiagnostics(filePath, compilerOptions);
50+
}
51+
4452
const tsTranspiled = tsc.transpileModule(src, {
4553
compilerOptions,
4654
fileName: filePath,
4755
});
4856

49-
const tsJestConfig = getTSJestConfig(jestConfig.globals);
50-
logOnce('tsJestConfig: ', tsJestConfig);
51-
5257
const postHook = getPostProcessHook(
5358
compilerOptions,
5459
jestConfig,

0 commit comments

Comments
 (0)