Skip to content

Commit 2dbf87c

Browse files
devversionjelbourn
authored andcommitted
build: don't run karma if typescript compilation fails (#3668)
1 parent 259ff5f commit 2dbf87c

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

tools/gulp/tasks/unit-test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,16 @@ gulp.task('test', [':test:deps'], () => {
7575
});
7676

7777
// Refreshes Karma's file list and schedules a test run.
78-
let runTests = () => {
79-
server.refreshFiles().then(() => server._injector.get('executor').schedule());
78+
// Tests will only run if TypeScript compilation was successful.
79+
let runTests = (err?: Error) => {
80+
if (!err) {
81+
server.refreshFiles().then(() => server._injector.get('executor').schedule());
82+
}
8083
};
8184

8285
// Boot up the test server and run the tests whenever a new browser connects.
8386
server.start();
84-
server.on('browser_register', runTests);
87+
server.on('browser_register', () => runTests());
8588

8689
// Watch for file changes, rebuild and run the tests.
8790
gulp.watch(patternRoot + '.ts', () => runSequence(':build:components:ts:spec', runTests));

tools/gulp/util/task_helpers.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ function _globify(maybeGlob: string, suffix = '**/*') {
3535

3636
/** Creates a task that runs the TypeScript compiler */
3737
export function tsBuildTask(tsConfigPath: string, extraOptions?: CompilerOptions) {
38-
return () => {
39-
compileProject(tsConfigPath, extraOptions);
40-
};
38+
return () => compileProject(tsConfigPath, extraOptions);
4139
}
4240

4341

tools/gulp/util/ts-compiler.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import * as chalk from 'chalk';
66
export function compileProject(project: string, options: ts.CompilerOptions) {
77
let parsed = parseProjectConfig(project, options);
88
let program = ts.createProgram(parsed.fileNames, parsed.options);
9+
let baseDir = program.getCurrentDirectory();
910

1011
// Report any invalid TypeScript options for the project.
11-
checkDiagnostics(program.getOptionsDiagnostics());
12+
reportDiagnostics(program.getOptionsDiagnostics(), baseDir);
1213

1314
let emitResult = program.emit();
1415

15-
checkDiagnostics(emitResult.diagnostics);
16+
reportDiagnostics(emitResult.diagnostics, baseDir);
1617
}
1718

1819
/** Parses a TypeScript project configuration. */
@@ -31,26 +32,26 @@ function parseProjectConfig(project: string, options: ts.CompilerOptions) {
3132
}
3233

3334
/** Formats the TypeScript diagnostics into a error string. */
34-
export function formatDiagnostics(diagnostics: ts.Diagnostic[]): string {
35+
export function formatDiagnostics(diagnostics: ts.Diagnostic[], baseDir: string): string {
3536
return diagnostics.map(diagnostic => {
36-
let res = ts.DiagnosticCategory[diagnostic.category];
37+
let res = `• ${chalk.red(`TS${diagnostic.code}`)} - `;
3738

3839
if (diagnostic.file) {
3940
let {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
41+
let filePath = path.relative(baseDir, diagnostic.file.fileName);
4042

41-
res += ` at ${diagnostic.file.fileName}(${line + 1},${character + 1}):`;
43+
res += `${filePath}(${line + 1},${character + 1}): `;
4244
}
43-
res += ` ${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`;
45+
res += `${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`;
4446

4547
return res;
4648
}).join('\n');
4749
}
4850

49-
/** Checks diagnostics and throws errors if present. */
50-
export function checkDiagnostics(diagnostics: ts.Diagnostic[]) {
51+
/** Checks and reports diagnostics if present. */
52+
export function reportDiagnostics(diagnostics: ts.Diagnostic[], baseDir?: string) {
5153
if (diagnostics && diagnostics.length && diagnostics[0]) {
52-
console.error(formatDiagnostics(diagnostics));
53-
console.error(chalk.red('TypeScript compilation failed. Exiting process.'));
54-
process.exit(1);
54+
console.error(formatDiagnostics(diagnostics, baseDir));
55+
throw new Error('TypeScript compilation failed.');
5556
}
5657
}

0 commit comments

Comments
 (0)