Skip to content

Commit

Permalink
* Fix test
Browse files Browse the repository at this point in the history
* update error message slightly
* take into account if the default value is greater then the set Error.stackTraceLimit
  • Loading branch information
stefanpenner committed Jul 11, 2018
1 parent 5f6c68f commit 27e0381
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@
// Inspired by http://stackoverflow.com/questions/13227489

module.exports = function getCallerFile(_position) {
var position = _position ? _position : 2;

var stackTraceLimit = Error.stackTraceLimit-1;
var errorMessage='ErrorStackTraceLimit overflow. Can pass less then ' + stackTraceLimit + ' or increase property Error.stackTraceLimit';

if(_position > stackTraceLimit){
throw errorMessage;
if (position >= Error.stackTraceLimit) {
throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' + position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`');
}

var oldPrepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = function(err, stack) { return stack; };
var stack = new Error().stack;
Error.prepareStackTrace = oldPrepareStackTrace;

var position = _position ? _position : 2;

// stack[0] holds this file
// stack[1] holds where this function was called
Expand Down
16 changes: 11 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var bar = require('./fixtures/bar');
var ensurePosix = require('ensure-posix-path');

describe('getCallerFile', function() {
var originalStackTraceLimit = Error.stackTraceLimit;

afterEach(() => Error.stackTraceLimit = originalStackTraceLimit);

it('gets current caller file', function() {
expect(ensurePosix(getCallerFile())).to.eql(ensurePosix(__dirname + '/node_modules/mocha/lib/runnable.js'));
});
Expand All @@ -19,12 +23,14 @@ describe('getCallerFile', function() {
expect(ensurePosix(bar())).to.eql(ensurePosix(__dirname + '/fixtures/bar.js'));
});

xit('throws error if error stackTraceLimit overflow', function(){
expect(getCallerFile(12)).to.throw(Error);

it('throws error if error stackTraceLimit overflow', function() {
Error.stackTraceLimit = 5;
expect(() => getCallerFile(Error.stackTraceLimit + 1)).to.throw(TypeError);
});

xit('throws no errors if incrementing error stackTraceLimit ', function(){
Error.stackTraceLimit = 14;
expect(getCallerFile(12)).to.not.throw(Error);
it('throws no errors if incrementing error stackTraceLimit ', function() {
Error.stackTraceLimit = 5;
expect(() => getCallerFile(Error.stackTraceLimit - 1)).to.not.throw(TypeError);
});
});

0 comments on commit 27e0381

Please sign in to comment.