fix(testrunner): better capture Location for hooks (#1680)

This commit is contained in:
Dmitry Gozman 2020-04-06 17:47:17 -07:00 committed by GitHub
parent f2b13c0e93
commit e519a3d762
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 6 additions and 8 deletions

View file

@ -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();
@ -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;

View file

@ -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);
}
}

View file

@ -23,7 +23,7 @@ const TestExpectation = {
};
function createHook(callback, name) {
const location = Location.getCallerLocation(__filename);
const location = Location.getCallerLocation();
return { name, body: callback, location };
}

View file

@ -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);
@ -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)