Browse Source

Remove stackStartFunction from AssertionError

At best it saves one stack level, but AVA code is already filtered out
of the stacks, so this is really quite unnecessary.
master
Mark Wubben 8 years ago
committed by Sindre Sorhus
parent
commit
59f2745640
  1. 47
      lib/assert.js
  2. 25
      lib/test.js
  3. 3
      test/serialize-error.js

47
lib/assert.js

@ -26,8 +26,6 @@ class AssertionError extends Error {
if (opts.stack) {
this.stack = opts.stack;
} else {
Error.captureStackTrace(this, opts.stackStartFunction);
}
}
}
@ -52,8 +50,7 @@ function wrapAssertions(callbacks) {
fail(message) {
fail(this, new AssertionError({
assertion: 'fail',
message: message || 'Test failed via t.fail()',
stackStartFunction: assertions.fail
message: message || 'Test failed via t.fail()'
}));
},
@ -66,8 +63,7 @@ function wrapAssertions(callbacks) {
assertion: 'is',
expected,
message,
operator: '===',
stackStartFunction: assertions.is
operator: '==='
}));
}
},
@ -79,8 +75,7 @@ function wrapAssertions(callbacks) {
assertion: 'not',
expected,
message,
operator: '!==',
stackStartFunction: assertions.not
operator: '!=='
}));
} else {
pass(this);
@ -95,8 +90,7 @@ function wrapAssertions(callbacks) {
actual,
assertion: 'deepEqual',
expected,
message,
stackStartFunction: assertions.deepEqual
message
}));
}
},
@ -107,8 +101,7 @@ function wrapAssertions(callbacks) {
actual,
assertion: 'notDeepEqual',
expected,
message,
stackStartFunction: assertions.notDeepEqual
message
}));
} else {
pass(this);
@ -153,8 +146,7 @@ function wrapAssertions(callbacks) {
} catch (err) {
throw new AssertionError({
assertion: 'throws',
message,
stackStartFunction: assertions.throws
message
});
}
};
@ -195,8 +187,7 @@ function wrapAssertions(callbacks) {
throw new AssertionError({
actual: err.actual,
assertion: 'notThrows',
message,
stackStartFunction: assertions.notThrows
message
});
}
};
@ -223,8 +214,7 @@ function wrapAssertions(callbacks) {
fail(this, new AssertionError({
actual,
assertion: 'ifError',
message,
stackStartFunction: assertions.ifError
message
}));
} else {
pass(this);
@ -240,8 +230,7 @@ function wrapAssertions(callbacks) {
actual,
assertion: 'snapshot',
expected: result.expected,
message: result.message,
stackStartFunction: assertions.snapshot
message: result.message
}));
}
}
@ -255,8 +244,7 @@ function wrapAssertions(callbacks) {
assertion: 'truthy',
expected: true,
message,
operator: '==',
stackStartFunction: enhancedAssertions.truthy
operator: '=='
});
}
},
@ -268,8 +256,7 @@ function wrapAssertions(callbacks) {
assertion: 'falsy',
expected: false,
message,
operator: '==',
stackStartFunction: enhancedAssertions.falsy
operator: '=='
});
}
},
@ -281,8 +268,7 @@ function wrapAssertions(callbacks) {
assertion: 'true',
expected: true,
message,
operator: '===',
stackStartFunction: enhancedAssertions.true
operator: '==='
});
}
},
@ -294,8 +280,7 @@ function wrapAssertions(callbacks) {
assertion: 'false',
expected: false,
message,
operator: '===',
stackStartFunction: enhancedAssertions.false
operator: '==='
});
}
},
@ -306,8 +291,7 @@ function wrapAssertions(callbacks) {
actual,
assertion: 'regex',
expected,
message,
stackStartFunction: enhancedAssertions.regex
message
});
}
},
@ -318,8 +302,7 @@ function wrapAssertions(callbacks) {
actual,
assertion: 'notRegex',
expected,
message,
stackStartFunction: enhancedAssertions.notRegex
message
});
}
}

25
lib/test.js

@ -18,21 +18,27 @@ class SkipApi {
}
}
const captureStack = start => {
const limitBefore = Error.stackTraceLimit;
Error.stackTraceLimit = 1;
const obj = {};
Error.captureStackTrace(obj, start);
Error.stackTraceLimit = limitBefore;
return obj.stack;
};
class ExecutionContext {
constructor(test) {
this._test = test;
this.skip = new SkipApi(test);
}
plan(ct) {
const limitBefore = Error.stackTraceLimit;
Error.stackTraceLimit = 1;
const obj = {};
Error.captureStackTrace(obj, this.plan);
Error.stackTraceLimit = limitBefore;
this._test.plan(ct, obj.stack);
this._test.plan(ct, captureStack(this.plan));
}
get end() {
return this._test.end;
const end = this._test.end;
const endFn = err => end(err, captureStack(endFn));
return endFn;
}
get title() {
return this._test.title;
@ -261,12 +267,13 @@ class Test {
throw new Error('t.end is not supported in this context. To use t.end as a callback, you must use "callback mode" via `test.cb(testName, fn)`');
}
_end(err) {
_end(err, stack) {
if (err) {
if (!(err instanceof assert.AssertionError)) {
err = new assert.AssertionError({
actual: err,
message: 'Callback called with an error'
message: 'Callback called with an error',
stack
});
}

3
test/serialize-error.js

@ -154,6 +154,7 @@ test('serialize statements of assertion errors', t => {
test('serialize actual and expected props of assertion errors', t => {
const err = new avaAssert.AssertionError({
stackStartFunction: null,
assertion: 'is',
actual: 1,
expected: 'a'
@ -168,7 +169,7 @@ test('serialize actual and expected props of assertion errors', t => {
});
test('only serialize actual and expected props of assertion errors if error was created with one', t => {
const err = new avaAssert.AssertionError({});
const err = new avaAssert.AssertionError({stackStartFunction: null});
const serializedErr = serialize(err);
t.is(serializedErr.actual, undefined);

Loading…
Cancel
Save