Skip to content

Commit

Permalink
feat: add option to run TypeScript diagnostics
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alan-agius4 committed Feb 3, 2018
1 parent 17c2862 commit 13b77d9
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 215 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,19 @@ By default Jest ignores everything in `node_modules`. This setting prevents Jest
}
```
### TS compiler && error reporting
- ts-jest only returns syntax errors from [tsc](https://github.com/Microsoft/TypeScript/issues/4864#issuecomment-141567247)
- Non syntactic errors do not show up in [jest](https://github.com/facebook/jest/issues/2168)
- If you only want to run jest if tsc does not output any errors, a workaround is `tsc --noEmit -p . && jest`
If you want to enable Syntactic & Semantic TypeScript error reporting you can enable this through `enableTsDiagnostics` flag;

```json
{
"jest": {
"globals": {
"ts-jest": {
"enableTsDiagnostics": true
}
}
}
}
```

### Known Limitations for hoisting
If the `jest.mock()` calls is placed after actual code, (e.g. after functions or classes) and `skipBabel` is not set,
Expand Down
1 change: 1 addition & 0 deletions src/jest-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ export interface TsJestConfig {
babelConfig?: BabelTransformOpts;
tsConfigFile?: string;
enableInternalCache?: boolean;
enableTsDiagnostics?: boolean;
}
11 changes: 8 additions & 3 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
cacheFile,
getTSConfig,
getTSJestConfig,
runTsDiagnostics,
injectSourcemapHook,
} from './utils';

Expand Down Expand Up @@ -41,14 +42,18 @@ export function process(
return src;
}

const tsJestConfig = getTSJestConfig(jestConfig.globals);
logOnce('tsJestConfig: ', tsJestConfig);

if (tsJestConfig.enableTsDiagnostics) {
runTsDiagnostics(filePath, compilerOptions);
}

const tsTranspiled = tsc.transpileModule(src, {
compilerOptions,
fileName: filePath,
});

const tsJestConfig = getTSJestConfig(jestConfig.globals);
logOnce('tsJestConfig: ', tsJestConfig);

const postHook = getPostProcessHook(
compilerOptions,
jestConfig,
Expand Down
Loading

0 comments on commit 13b77d9

Please sign in to comment.