Skip to content

Commit

Permalink
fix(testrunner): better capture Location for hooks (#1680)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Apr 7, 2020
1 parent f2b13c0 commit e519a3d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
6 changes: 2 additions & 4 deletions utils/testrunner/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ class Location {
return this._fileName + ':' + this._lineNumber + ':' + this._columnNumber;
}

// TODO: static getCallerLocationIn(glob) vs getCallerLocationIgnoring(glob).

static getCallerLocation(filename) {
static getCallerLocation(ignorePrefix = __dirname) {
const error = new Error();
const stackFrames = error.stack.split('\n').slice(1);
const location = new Location();
Expand All @@ -71,7 +69,7 @@ class Location {
if (!match)
return null;
const filePath = match[1];
if (filePath === __filename || filePath === filename)
if (filePath === __filename || filePath.startsWith(ignorePrefix))
continue;

location._filePath = filePath;
Expand Down
2 changes: 1 addition & 1 deletion utils/testrunner/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MatchError extends Error {
super(message);
this.name = this.constructor.name;
this.formatter = formatter;
this.location = Location.getCallerLocation(__filename);
this.location = Location.getCallerLocation();
Error.captureStackTrace(this, this.constructor);
}
}
Expand Down
2 changes: 1 addition & 1 deletion utils/testrunner/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const TestExpectation = {
};

function createHook(callback, name) {
const location = Location.getCallerLocation(__filename);
const location = Location.getCallerLocation();
return { name, body: callback, location };
}

Expand Down
4 changes: 2 additions & 2 deletions utils/testrunner/TestCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class TestCollector {
this._currentSuite = new Suite(null, '', new Location());

this._api.describe = specBuilder(this._suiteModifiers, this._suiteAttributes, (specs, name, suiteCallback, ...suiteArgs) => {
const location = Location.getCallerLocation(__filename);
const location = Location.getCallerLocation();
const suite = new Suite(this._currentSuite, name, location);
for (const { callback, args } of specs)
callback(suite, ...args);
Expand All @@ -146,7 +146,7 @@ class TestCollector {
this._currentSuite = suite.parentSuite();
});
this._api.it = specBuilder(this._testModifiers, this._testAttributes, (specs, name, testCallback) => {
const location = Location.getCallerLocation(__filename);
const location = Location.getCallerLocation();
const test = new Test(this._currentSuite, name, testCallback, location);
test.setTimeout(timeout);
for (const { callback, args } of specs)
Expand Down

0 comments on commit e519a3d

Please sign in to comment.