Skip to content

Commit 5cce79a

Browse files
btkostnernovemberborn
authored andcommitted
Pretty-print TypeScript build errors
1 parent 199cc70 commit 5cce79a

File tree

11 files changed

+70
-10
lines changed

11 files changed

+70
-10
lines changed

lib/reporters/mini.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ class MiniReporter {
254254
}
255255

256256
writeErr(evt) {
257+
if (evt.err.name === 'TSError' && evt.err.object && evt.err.object.diagnosticText) {
258+
this.lineWriter.writeLine(colors.errorStack(trimOffNewlines(evt.err.object.diagnosticText)));
259+
return;
260+
}
261+
257262
if (evt.err.source) {
258263
this.lineWriter.writeLine(colors.errorSource(`${evt.err.source.file}:${evt.err.source.line}`));
259264
const excerpt = codeExcerpt(evt.err.source, {maxWidth: this.lineWriter.columns - 2});

lib/reporters/verbose.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ class VerboseReporter {
198198
}
199199

200200
writeErr(evt) {
201+
if (evt.err.name === 'TSError' && evt.err.object && evt.err.object.diagnosticText) {
202+
this.lineWriter.writeLine(colors.errorStack(trimOffNewlines(evt.err.object.diagnosticText)));
203+
return;
204+
}
205+
201206
if (evt.err.source) {
202207
this.lineWriter.writeLine(colors.errorSource(`${evt.err.source.file}:${evt.err.source.line}`));
203208
const excerpt = codeExcerpt(evt.err.source, {maxWidth: this.reportStream.columns - 2});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
trigger.a.build.error();
2+
lets.do.another();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true
4+
}
5+
}

test/helper/report.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ exports.assert = (t, logFile, buffer, stripOptions) => {
6363

6464
exports.sanitizers = {
6565
cwd: str => replaceString(str, process.cwd(), '~'),
66+
lineEndings: str => replaceString(str, '\r\n', '\n'),
6667
posix: str => replaceString(str, '\\', '/'),
6768
slow: str => str.replace(/(slow.+?)\(\d+m?s\)/g, '$1 (000ms)'),
6869
// At least in Appveyor with Node.js 6, IPC can overtake stdout/stderr. This
@@ -80,7 +81,7 @@ exports.sanitizers = {
8081
const run = (type, reporter) => {
8182
const projectDir = path.join(__dirname, '../fixture/report', type.toLowerCase());
8283

83-
const api = createApi({
84+
const options = {
8485
extensions: {
8586
all: ['js'],
8687
enhancementsOnly: [],
@@ -90,7 +91,7 @@ const run = (type, reporter) => {
9091
failWithoutAssertions: false,
9192
serial: type === 'failFast' || type === 'failFast2',
9293
require: [],
93-
cacheEnable: true,
94+
cacheEnabled: true,
9495
compileEnhancements: true,
9596
match: [],
9697
babelConfig: {testOptions: {}},
@@ -101,11 +102,21 @@ const run = (type, reporter) => {
101102
updateSnapshots: false,
102103
snapshotDir: false,
103104
color: true
104-
});
105+
};
106+
let pattern = '*.js';
107+
108+
if (type === 'typescript') {
109+
options.extensions.all.push('ts');
110+
options.extensions.enhancementsOnly.push('ts');
111+
options.compileEnhancements = false;
112+
options.require = ['ts-node/register'];
113+
pattern = '*.ts';
114+
}
105115

116+
const api = createApi(options);
106117
api.on('run', plan => reporter.startRun(plan));
107118

108-
const files = globby.sync('*.js', {cwd: projectDir}).sort();
119+
const files = globby.sync(pattern, {cwd: projectDir}).sort();
109120
if (type !== 'watch') {
110121
return api.run(files).then(() => {
111122
reporter.endRun();
@@ -129,3 +140,4 @@ exports.failFast = reporter => run('failFast', reporter);
129140
exports.failFast2 = reporter => run('failFast2', reporter);
130141
exports.only = reporter => run('only', reporter);
131142
exports.watch = reporter => run('watch', reporter);
143+
exports.typescript = reporter => run('typescript', reporter);

test/reporters/mini.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ const TTYStream = require('../helper/tty-stream');
88
const report = require('../helper/report');
99
const MiniReporter = require('../../lib/reporters/mini');
1010

11-
const run = type => t => {
11+
const run = (type, sanitizers = []) => t => {
1212
t.plan(1);
1313

1414
const logFile = path.join(__dirname, `mini.${type.toLowerCase()}.log`);
1515

1616
const tty = new TTYStream({
1717
columns: 200,
18-
sanitizers: [report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.unreliableProcessIO, report.sanitizers.version]
18+
sanitizers: [...sanitizers, report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.unreliableProcessIO, report.sanitizers.version]
1919
});
2020
const reporter = new MiniReporter({
2121
spinner: {
@@ -41,3 +41,4 @@ test('mini reporter - failFast run', run('failFast'));
4141
test('mini reporter - second failFast run', run('failFast2'));
4242
test('mini reporter - only run', run('only'));
4343
test('mini reporter - watch mode run', run('watch'));
44+
test('mini reporter - typescript', run('typescript', [report.sanitizers.lineEndings]));

test/reporters/mini.typescript.log

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[?25l---tty-stream-chunk-separator
2+
3+
---tty-stream-chunk-separator
4+
* ---tty-stream-chunk-separator
5+
---tty-stream-chunk-separator
6+
[?25h
7+
1 uncaught exception
8+
9+
Uncaught exception in test/fixture/report/typescript/build-error.ts
10+
11+
build-error.ts(1,1): error TS2304: Cannot find name 'trigger'.
12+
build-error.ts(2,1): error TS2304: Cannot find name 'lets'.
13+
14+
---tty-stream-chunk-separator

test/reporters/tap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ const TTYStream = require('../helper/tty-stream');
88
const report = require('../helper/report');
99
const TapReporter = require('../../lib/reporters/tap');
1010

11-
const run = type => t => {
11+
const run = (type, sanitizers = []) => t => {
1212
t.plan(1);
1313

1414
const logFile = path.join(__dirname, `tap.${type.toLowerCase()}.log`);
1515

1616
const tty = new TTYStream({
1717
columns: 200,
18-
sanitizers: [report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.unreliableProcessIO]
18+
sanitizers: [...sanitizers, report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.unreliableProcessIO]
1919
});
2020
const reporter = new TapReporter({
2121
reportStream: tty,

test/reporters/verbose.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ const TTYStream = require('../helper/tty-stream');
88
const report = require('../helper/report');
99
const VerboseReporter = require('../../lib/reporters/verbose');
1010

11-
const run = type => t => {
11+
const run = (type, sanitizers = []) => t => {
1212
t.plan(1);
1313

1414
const logFile = path.join(__dirname, `verbose.${type.toLowerCase()}.log`);
1515

1616
const tty = new TTYStream({
1717
columns: 200,
18-
sanitizers: [report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.slow, report.sanitizers.unreliableProcessIO, report.sanitizers.version]
18+
sanitizers: [...sanitizers, report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.slow, report.sanitizers.unreliableProcessIO, report.sanitizers.version]
1919
});
2020
const reporter = new VerboseReporter({
2121
reportStream: tty,
@@ -36,3 +36,4 @@ test('verbose reporter - failFast run', run('failFast'));
3636
test('verbose reporter - second failFast run', run('failFast2'));
3737
test('verbose reporter - only run', run('only'));
3838
test('verbose reporter - watch mode run', run('watch'));
39+
test('verbose reporter - typescript', run('typescript', [report.sanitizers.lineEndings]));

0 commit comments

Comments
 (0)