Skip to content

Commit

Permalink
Merge pull request avajs#659 from jamestalmage/use-clean-yaml-object
Browse files Browse the repository at this point in the history
switch from serialize-error to clean-yaml-object
  • Loading branch information
jamestalmage committed Mar 19, 2016
2 parents e4ef5f2 + cd5767e commit 13077e2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 23 deletions.
15 changes: 14 additions & 1 deletion api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var resolveCwd = require('resolve-cwd');
var uniqueTempDir = require('unique-temp-dir');
var findCacheDir = require('find-cache-dir');
var slash = require('slash');
var isObj = require('is-obj');
var AvaError = require('./lib/ava-error');
var fork = require('./lib/fork');
var formatter = require('./lib/enhance-assert').formatter();
Expand Down Expand Up @@ -81,10 +82,22 @@ Api.prototype._handleOutput = function (channel, data) {
this.emit(channel, data);
};

function normalizeError(err) {
if (!isObj(err)) {
err = {
message: err,
stack: err
};
}

return err;
}

Api.prototype._handleRejections = function (data) {
this.rejectionCount += data.rejections.length;

data.rejections.forEach(function (err) {
err = normalizeError(err);
err.type = 'rejection';
err.file = data.file;
this.emit('error', err);
Expand All @@ -94,7 +107,7 @@ Api.prototype._handleRejections = function (data) {

Api.prototype._handleExceptions = function (data) {
this.exceptionCount++;
var err = data.exception;
var err = normalizeError(data.exception);
err.type = 'exception';
err.file = data.file;
this.emit('error', err);
Expand Down
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
var path = require('path');
var chalk = require('chalk');
var serializeError = require('serialize-error');
var beautifyStack = require('./lib/beautify-stack');
var serializeError = require('./lib/serialize-error');
var globals = require('./lib/globals');
var Runner = require('./lib/runner');
var send = require('./lib/send');
Expand Down Expand Up @@ -49,9 +48,6 @@ function test(props) {

if (hasError) {
props.error = serializeError(props.error);
if (props.error.stack) {
props.error.stack = beautifyStack(props.error.stack);
}
} else {
props.error = null;
}
Expand Down
20 changes: 20 additions & 0 deletions lib/serialize-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
var cleanYamlObject = require('clean-yaml-object');
var beautifyStack = require('./beautify-stack');

function filter(propertyName, isRoot, source, target) {
if (!isRoot) {
return true;
}

if (propertyName === 'stack') {
target.stack = beautifyStack(source.stack);
return false;
}

return true;
}

module.exports = function (error) {
return cleanYamlObject(error, filter);
};
15 changes: 3 additions & 12 deletions lib/test-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ sourceMapSupport.install({
});

var loudRejection = require('loud-rejection/api')(process);
var serializeError = require('serialize-error');
var beautifyStack = require('./beautify-stack');
var serializeError = require('./serialize-error');
var send = require('./send');
var installPrecompiler = require('require-precompiled');
var cacheDir = opts.cacheDir;
Expand Down Expand Up @@ -94,11 +93,7 @@ Object.keys(require.extensions).forEach(function (ext) {
require(testPath);

process.on('uncaughtException', function (exception) {
var serialized = serializeError(exception);
if (serialized.stack) {
serialized.stack = beautifyStack(serialized.stack);
}
send('uncaughtException', {exception: serialized});
send('uncaughtException', {exception: serializeError(exception)});
});

// if ava was not required, show an error
Expand Down Expand Up @@ -140,11 +135,7 @@ process.on('ava-teardown', function () {
}

rejections = rejections.map(function (rejection) {
var error = serializeError(rejection.reason);
if (error.stack) {
error.stack = beautifyStack(error.stack);
}
return error;
return serializeError(rejection.reason);
});

send('unhandledRejections', {rejections: rejections});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"bluebird": "^3.0.0",
"caching-transform": "^1.0.0",
"chalk": "^1.0.0",
"clean-yaml-object": "^0.1.0",
"cli-cursor": "^1.0.2",
"cli-spinners": "^0.1.2",
"cli-truncate": "^0.2.0",
Expand All @@ -104,6 +105,7 @@
"ignore-by-default": "^1.0.0",
"is-ci": "^1.0.7",
"is-generator-fn": "^1.0.0",
"is-obj": "^1.0.0",
"is-observable": "^0.1.0",
"is-promise": "^2.1.0",
"last-line-stream": "^1.0.0",
Expand All @@ -124,7 +126,6 @@
"pretty-ms": "^2.0.0",
"require-precompiled": "^0.1.0",
"resolve-cwd": "^1.0.0",
"serialize-error": "^1.1.0",
"set-immediate-shim": "^1.0.1",
"slash": "^1.0.0",
"source-map-support": "^0.4.0",
Expand Down
6 changes: 3 additions & 3 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ test('disallow invalid babel config shortcuts', function (t) {
});
});

test('throwing a named function will report the to the console', function (t) {
test('throwing a named function will report the function to the console', function (t) {
execCli('fixture/throw-named-function.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /\[Function: fooFn]/);
t.match(stderr, /function fooFn\(\) \{\}/);
// TODO(jamestalmage)
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
Expand All @@ -102,7 +102,7 @@ test('babel require hook only applies to the test file', function (t) {
test('throwing a anonymous function will report the function to the console', function (t) {
execCli('fixture/throw-anonymous-function.js', function (err, stdout, stderr) {
t.ok(err);
t.match(stderr, /\[Function: anonymous]/);
t.match(stderr, /function \(\) \{\}/);
// TODO(jamestalmage)
// t.ok(/1 uncaught exception[^s]/.test(stdout));
t.end();
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/throw-anonymous-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import test from '../../';

test('throw an uncaught exception', () => {
setImmediate(() => {
throw () => {};
throw function () {};
});
});

0 comments on commit 13077e2

Please sign in to comment.