Removed unnecessary errorWithStack helper

This commit is contained in:
Steve Gravrock
2025-06-22 12:49:26 -07:00
parent 2d07b3e6d7
commit 21db6ec0e3
6 changed files with 155 additions and 229 deletions
+4 -11
View File
@@ -721,16 +721,9 @@ getJasmineRequireObj().util = function(j$) {
return Object.prototype.hasOwnProperty.call(obj, key); return Object.prototype.hasOwnProperty.call(obj, key);
}; };
util.errorWithStack = function errorWithStack() {
// Don't throw and catch. That makes it harder for users to debug their
// code with exception breakpoints, and it's unnecessary since all
// supported environments populate new Error().stack
return new Error();
};
function callerFile() { function callerFile() {
const trace = new j$.StackTrace(util.errorWithStack()); const trace = new j$.StackTrace(new Error());
return trace.frames[2].file; return trace.frames[1].file;
} }
util.jasmineFile = (function() { util.jasmineFile = (function() {
@@ -3724,7 +3717,7 @@ getJasmineRequireObj().Deprecator = function(j$) {
Deprecator.prototype.stackTrace_ = function() { Deprecator.prototype.stackTrace_ = function() {
const formatter = new j$.ExceptionFormatter(); const formatter = new j$.ExceptionFormatter();
return formatter.stack(j$.util.errorWithStack()).replace(/^Error\n/m, ''); return formatter.stack(new Error()).replace(/^Error\n/m, '');
}; };
Deprecator.prototype.report_ = function(runnable, deprecation, options) { Deprecator.prototype.report_ = function(runnable, deprecation, options) {
@@ -4034,7 +4027,7 @@ getJasmineRequireObj().Expectation = function(j$) {
return function() { return function() {
// Capture the call stack here, before we go async, so that it will contain // Capture the call stack here, before we go async, so that it will contain
// frames that are relevant to the user instead of just parts of Jasmine. // frames that are relevant to the user instead of just parts of Jasmine.
const errorForStack = j$.util.errorWithStack(); const errorForStack = new Error();
return this.expector return this.expector
.compare(name, matcherFactory, arguments) .compare(name, matcherFactory, arguments)
+146 -206
View File
@@ -277,23 +277,18 @@ describe('AsyncExpectation', function() {
it('reports a passing result to the spec when the comparison passes', function() { it('reports a passing result to the spec when the comparison passes', function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ pass: true }); return Promise.resolve({ pass: true });
} }
}; };
} }
}, };
matchersUtil = { const matchersUtil = {
buildFailureMessage: jasmine.createSpy('buildFailureMessage') buildFailureMessage: jasmine.createSpy('buildFailureMessage')
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
customAsyncMatchers: matchers, customAsyncMatchers: matchers,
@@ -310,7 +305,7 @@ describe('AsyncExpectation', function() {
error: undefined, error: undefined,
expected: 'hello', expected: 'hello',
actual: 'an actual', actual: 'an actual',
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
@@ -329,13 +324,8 @@ describe('AsyncExpectation', function() {
buildFailureMessage: function() { buildFailureMessage: function() {
return ''; return '';
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
customAsyncMatchers: matchers, customAsyncMatchers: matchers,
@@ -352,30 +342,25 @@ describe('AsyncExpectation', function() {
actual: 'an actual', actual: 'an actual',
message: '', message: '',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
it('reports a failing result and a custom fail message to the spec when the comparison fails', function() { it('reports a failing result and a custom fail message to the spec when the comparison fails', function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ return Promise.resolve({
pass: false, pass: false,
message: 'I am a custom message' message: 'I am a custom message'
}); });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
actual: 'an actual', actual: 'an actual',
@@ -391,32 +376,27 @@ describe('AsyncExpectation', function() {
actual: 'an actual', actual: 'an actual',
message: 'I am a custom message', message: 'I am a custom message',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
it('reports a failing result with a custom fail message function to the spec when the comparison fails', function() { it('reports a failing result with a custom fail message function to the spec when the comparison fails', function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ return Promise.resolve({
pass: false, pass: false,
message: function() { message: function() {
return 'I am a custom message'; return 'I am a custom message';
} }
}); });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
customAsyncMatchers: matchers, customAsyncMatchers: matchers,
@@ -432,28 +412,23 @@ describe('AsyncExpectation', function() {
actual: 'an actual', actual: 'an actual',
message: 'I am a custom message', message: 'I am a custom message',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
it('reports a passing result to the spec when the comparison fails for a negative expectation', function() { it('reports a passing result to the spec when the comparison fails for a negative expectation', function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ pass: false }); return Promise.resolve({ pass: false });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
actual = 'an actual', const actual = 'an actual';
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
customAsyncMatchers: matchers, customAsyncMatchers: matchers,
@@ -469,7 +444,7 @@ describe('AsyncExpectation', function() {
error: undefined, error: undefined,
expected: 'hello', expected: 'hello',
actual: actual, actual: actual,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
@@ -488,14 +463,9 @@ describe('AsyncExpectation', function() {
buildFailureMessage: function() { buildFailureMessage: function() {
return 'default message'; return 'default message';
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
actual = 'an actual', const actual = 'an actual';
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
customAsyncMatchers: matchers, customAsyncMatchers: matchers,
@@ -512,31 +482,26 @@ describe('AsyncExpectation', function() {
actual: actual, actual: actual,
message: 'default message', message: 'default message',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
it('reports a failing result and a custom fail message to the spec when the comparison passes for a negative expectation', function() { it('reports a failing result and a custom fail message to the spec when the comparison passes for a negative expectation', function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ return Promise.resolve({
pass: true, pass: true,
message: 'I am a custom message' message: 'I am a custom message'
}); });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
actual = 'an actual', const actual = 'an actual';
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
customAsyncMatchers: matchers, customAsyncMatchers: matchers,
@@ -552,31 +517,26 @@ describe('AsyncExpectation', function() {
actual: actual, actual: actual,
message: 'I am a custom message', message: 'I am a custom message',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
it("reports a passing result to the spec when the 'not' comparison passes, given a negativeCompare", function() { it("reports a passing result to the spec when the 'not' comparison passes, given a negativeCompare", function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ pass: true }); return Promise.resolve({ pass: true });
}, },
negativeCompare: function() { negativeCompare: function() {
return Promise.resolve({ pass: true }); return Promise.resolve({ pass: true });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
actual = 'an actual', const actual = 'an actual';
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
customAsyncMatchers: matchers, customAsyncMatchers: matchers,
@@ -592,34 +552,29 @@ describe('AsyncExpectation', function() {
actual: actual, actual: actual,
message: '', message: '',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
it("reports a failing result and a custom fail message to the spec when the 'not' comparison fails, given a negativeCompare", function() { it("reports a failing result and a custom fail message to the spec when the 'not' comparison fails, given a negativeCompare", function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ pass: true }); return Promise.resolve({ pass: true });
}, },
negativeCompare: function() { negativeCompare: function() {
return Promise.resolve({ return Promise.resolve({
pass: false, pass: false,
message: "I'm a custom message" message: "I'm a custom message"
}); });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
actual = 'an actual', const actual = 'an actual';
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
customAsyncMatchers: matchers, customAsyncMatchers: matchers,
@@ -635,7 +590,7 @@ describe('AsyncExpectation', function() {
actual: actual, actual: actual,
message: "I'm a custom message", message: "I'm a custom message",
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
@@ -643,24 +598,19 @@ describe('AsyncExpectation', function() {
it('reports errorWithStack when a custom error message is returned', function() { it('reports errorWithStack when a custom error message is returned', function() {
const customError = new Error('I am a custom error'); const customError = new Error('I am a custom error');
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ return Promise.resolve({
pass: false, pass: false,
message: 'I am a custom message', message: 'I am a custom message',
error: customError error: customError
}); });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
actual: 'an actual', actual: 'an actual',
@@ -676,30 +626,25 @@ describe('AsyncExpectation', function() {
actual: 'an actual', actual: 'an actual',
message: 'I am a custom message', message: 'I am a custom message',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
it("reports a custom message to the spec when a 'not' comparison fails", function() { it("reports a custom message to the spec when a 'not' comparison fails", function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ return Promise.resolve({
pass: true, pass: true,
message: 'I am a custom message' message: 'I am a custom message'
}); });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
const expectation = jasmineUnderTest.Expectation.asyncFactory({ const expectation = jasmineUnderTest.Expectation.asyncFactory({
actual: 'an actual', actual: 'an actual',
@@ -715,32 +660,27 @@ describe('AsyncExpectation', function() {
actual: 'an actual', actual: 'an actual',
message: 'I am a custom message', message: 'I am a custom message',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
it("reports a custom message func to the spec when a 'not' comparison fails", function() { it("reports a custom message func to the spec when a 'not' comparison fails", function() {
const matchers = { const matchers = {
toFoo: function() { toFoo: function() {
return { return {
compare: function() { compare: function() {
return Promise.resolve({ return Promise.resolve({
pass: true, pass: true,
message: function() { message: function() {
return 'I am a custom message'; return 'I am a custom message';
} }
}); });
} }
}; };
} }
}, };
addExpectationResult = jasmine.createSpy('addExpectationResult'), const addExpectationResult = jasmine.createSpy('addExpectationResult');
errorWithStack = new Error('errorWithStack');
spyOn(jasmineUnderTest.util, 'errorWithStack').and.returnValue(
errorWithStack
);
let expectation = jasmineUnderTest.Expectation.asyncFactory({ let expectation = jasmineUnderTest.Expectation.asyncFactory({
actual: 'an actual', actual: 'an actual',
@@ -756,7 +696,7 @@ describe('AsyncExpectation', function() {
actual: 'an actual', actual: 'an actual',
message: 'I am a custom message', message: 'I am a custom message',
error: undefined, error: undefined,
errorForStack: errorWithStack errorForStack: jasmine.any(Error)
}); });
}); });
}); });
+1 -1
View File
@@ -1524,7 +1524,7 @@ describe('Env integration', function() {
env = new jasmineUnderTest.Env({ env = new jasmineUnderTest.Env({
global: { global: {
setTimeout: function(cb, t) { setTimeout: function(cb, t) {
const stack = jasmine.util.errorWithStack().stack; const stack = new Error().stack;
if (stack.indexOf('ClearStack') >= 0) { if (stack.indexOf('ClearStack') >= 0) {
return realSetTimeout(cb, t); return realSetTimeout(cb, t);
} else { } else {
+1 -1
View File
@@ -65,7 +65,7 @@ getJasmineRequireObj().Deprecator = function(j$) {
Deprecator.prototype.stackTrace_ = function() { Deprecator.prototype.stackTrace_ = function() {
const formatter = new j$.ExceptionFormatter(); const formatter = new j$.ExceptionFormatter();
return formatter.stack(j$.util.errorWithStack()).replace(/^Error\n/m, ''); return formatter.stack(new Error()).replace(/^Error\n/m, '');
}; };
Deprecator.prototype.report_ = function(runnable, deprecation, options) { Deprecator.prototype.report_ = function(runnable, deprecation, options) {
+1 -1
View File
@@ -148,7 +148,7 @@ getJasmineRequireObj().Expectation = function(j$) {
return function() { return function() {
// Capture the call stack here, before we go async, so that it will contain // Capture the call stack here, before we go async, so that it will contain
// frames that are relevant to the user instead of just parts of Jasmine. // frames that are relevant to the user instead of just parts of Jasmine.
const errorForStack = j$.util.errorWithStack(); const errorForStack = new Error();
return this.expector return this.expector
.compare(name, matcherFactory, arguments) .compare(name, matcherFactory, arguments)
+2 -9
View File
@@ -48,16 +48,9 @@ getJasmineRequireObj().util = function(j$) {
return Object.prototype.hasOwnProperty.call(obj, key); return Object.prototype.hasOwnProperty.call(obj, key);
}; };
util.errorWithStack = function errorWithStack() {
// Don't throw and catch. That makes it harder for users to debug their
// code with exception breakpoints, and it's unnecessary since all
// supported environments populate new Error().stack
return new Error();
};
function callerFile() { function callerFile() {
const trace = new j$.StackTrace(util.errorWithStack()); const trace = new j$.StackTrace(new Error());
return trace.frames[2].file; return trace.frames[1].file;
} }
util.jasmineFile = (function() { util.jasmineFile = (function() {