Skip to content

Commit

Permalink
Pretty-print TypeScript build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
btkostner authored and novemberborn committed Jun 13, 2018
1 parent 199cc70 commit 5cce79a
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/reporters/mini.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ class MiniReporter {
}

writeErr(evt) {
if (evt.err.name === 'TSError' && evt.err.object && evt.err.object.diagnosticText) {
this.lineWriter.writeLine(colors.errorStack(trimOffNewlines(evt.err.object.diagnosticText)));
return;
}

if (evt.err.source) {
this.lineWriter.writeLine(colors.errorSource(`${evt.err.source.file}:${evt.err.source.line}`));
const excerpt = codeExcerpt(evt.err.source, {maxWidth: this.lineWriter.columns - 2});
Expand Down
5 changes: 5 additions & 0 deletions lib/reporters/verbose.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ class VerboseReporter {
}

writeErr(evt) {
if (evt.err.name === 'TSError' && evt.err.object && evt.err.object.diagnosticText) {
this.lineWriter.writeLine(colors.errorStack(trimOffNewlines(evt.err.object.diagnosticText)));
return;
}

if (evt.err.source) {
this.lineWriter.writeLine(colors.errorSource(`${evt.err.source.file}:${evt.err.source.line}`));
const excerpt = codeExcerpt(evt.err.source, {maxWidth: this.reportStream.columns - 2});
Expand Down
2 changes: 2 additions & 0 deletions test/fixture/report/typescript/build-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trigger.a.build.error();
lets.do.another();
1 change: 1 addition & 0 deletions test/fixture/report/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions test/fixture/report/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strict": true
}
}
20 changes: 16 additions & 4 deletions test/helper/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ exports.assert = (t, logFile, buffer, stripOptions) => {

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

const api = createApi({
const options = {
extensions: {
all: ['js'],
enhancementsOnly: [],
Expand All @@ -90,7 +91,7 @@ const run = (type, reporter) => {
failWithoutAssertions: false,
serial: type === 'failFast' || type === 'failFast2',
require: [],
cacheEnable: true,
cacheEnabled: true,
compileEnhancements: true,
match: [],
babelConfig: {testOptions: {}},
Expand All @@ -101,11 +102,21 @@ const run = (type, reporter) => {
updateSnapshots: false,
snapshotDir: false,
color: true
});
};
let pattern = '*.js';

if (type === 'typescript') {
options.extensions.all.push('ts');
options.extensions.enhancementsOnly.push('ts');
options.compileEnhancements = false;
options.require = ['ts-node/register'];
pattern = '*.ts';
}

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

const files = globby.sync('*.js', {cwd: projectDir}).sort();
const files = globby.sync(pattern, {cwd: projectDir}).sort();
if (type !== 'watch') {
return api.run(files).then(() => {
reporter.endRun();
Expand All @@ -129,3 +140,4 @@ exports.failFast = reporter => run('failFast', reporter);
exports.failFast2 = reporter => run('failFast2', reporter);
exports.only = reporter => run('only', reporter);
exports.watch = reporter => run('watch', reporter);
exports.typescript = reporter => run('typescript', reporter);
5 changes: 3 additions & 2 deletions test/reporters/mini.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const TTYStream = require('../helper/tty-stream');
const report = require('../helper/report');
const MiniReporter = require('../../lib/reporters/mini');

const run = type => t => {
const run = (type, sanitizers = []) => t => {
t.plan(1);

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

const tty = new TTYStream({
columns: 200,
sanitizers: [report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.unreliableProcessIO, report.sanitizers.version]
sanitizers: [...sanitizers, report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.unreliableProcessIO, report.sanitizers.version]
});
const reporter = new MiniReporter({
spinner: {
Expand All @@ -41,3 +41,4 @@ test('mini reporter - failFast run', run('failFast'));
test('mini reporter - second failFast run', run('failFast2'));
test('mini reporter - only run', run('only'));
test('mini reporter - watch mode run', run('watch'));
test('mini reporter - typescript', run('typescript', [report.sanitizers.lineEndings]));
14 changes: 14 additions & 0 deletions test/reporters/mini.typescript.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[?25l---tty-stream-chunk-separator

---tty-stream-chunk-separator
* ---tty-stream-chunk-separator
---tty-stream-chunk-separator
[?25h
1 uncaught exception

Uncaught exception in test/fixture/report/typescript/build-error.ts

build-error.ts(1,1): error TS2304: Cannot find name 'trigger'.
build-error.ts(2,1): error TS2304: Cannot find name 'lets'.

---tty-stream-chunk-separator
4 changes: 2 additions & 2 deletions test/reporters/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const TTYStream = require('../helper/tty-stream');
const report = require('../helper/report');
const TapReporter = require('../../lib/reporters/tap');

const run = type => t => {
const run = (type, sanitizers = []) => t => {
t.plan(1);

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

const tty = new TTYStream({
columns: 200,
sanitizers: [report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.unreliableProcessIO]
sanitizers: [...sanitizers, report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.unreliableProcessIO]
});
const reporter = new TapReporter({
reportStream: tty,
Expand Down
5 changes: 3 additions & 2 deletions test/reporters/verbose.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const TTYStream = require('../helper/tty-stream');
const report = require('../helper/report');
const VerboseReporter = require('../../lib/reporters/verbose');

const run = type => t => {
const run = (type, sanitizers = []) => t => {
t.plan(1);

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

const tty = new TTYStream({
columns: 200,
sanitizers: [report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.slow, report.sanitizers.unreliableProcessIO, report.sanitizers.version]
sanitizers: [...sanitizers, report.sanitizers.cwd, report.sanitizers.posix, report.sanitizers.slow, report.sanitizers.unreliableProcessIO, report.sanitizers.version]
});
const reporter = new VerboseReporter({
reportStream: tty,
Expand All @@ -36,3 +36,4 @@ test('verbose reporter - failFast run', run('failFast'));
test('verbose reporter - second failFast run', run('failFast2'));
test('verbose reporter - only run', run('only'));
test('verbose reporter - watch mode run', run('watch'));
test('verbose reporter - typescript', run('typescript', [report.sanitizers.lineEndings]));
14 changes: 14 additions & 0 deletions test/reporters/verbose.typescript.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

---tty-stream-chunk-separator
Uncaught exception in test/fixture/report/typescript/build-error.ts

build-error.ts(1,1): error TS2304: Cannot find name 'trigger'.
build-error.ts(2,1): error TS2304: Cannot find name 'lets'.

---tty-stream-chunk-separator
✖ test/fixture/report/typescript/build-error.ts exited with a non-zero exit code: 1
---tty-stream-chunk-separator

1 uncaught exception

---tty-stream-chunk-separator

0 comments on commit 5cce79a

Please sign in to comment.