fix(testrunner): better capture Location for hooks (#1680)
This commit is contained in:
parent
f2b13c0e93
commit
e519a3d762
|
|
@ -49,9 +49,7 @@ class Location {
|
||||||
return this._fileName + ':' + this._lineNumber + ':' + this._columnNumber;
|
return this._fileName + ':' + this._lineNumber + ':' + this._columnNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: static getCallerLocationIn(glob) vs getCallerLocationIgnoring(glob).
|
static getCallerLocation(ignorePrefix = __dirname) {
|
||||||
|
|
||||||
static getCallerLocation(filename) {
|
|
||||||
const error = new Error();
|
const error = new Error();
|
||||||
const stackFrames = error.stack.split('\n').slice(1);
|
const stackFrames = error.stack.split('\n').slice(1);
|
||||||
const location = new Location();
|
const location = new Location();
|
||||||
|
|
@ -71,7 +69,7 @@ class Location {
|
||||||
if (!match)
|
if (!match)
|
||||||
return null;
|
return null;
|
||||||
const filePath = match[1];
|
const filePath = match[1];
|
||||||
if (filePath === __filename || filePath === filename)
|
if (filePath === __filename || filePath.startsWith(ignorePrefix))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
location._filePath = filePath;
|
location._filePath = filePath;
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ class MatchError extends Error {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = this.constructor.name;
|
this.name = this.constructor.name;
|
||||||
this.formatter = formatter;
|
this.formatter = formatter;
|
||||||
this.location = Location.getCallerLocation(__filename);
|
this.location = Location.getCallerLocation();
|
||||||
Error.captureStackTrace(this, this.constructor);
|
Error.captureStackTrace(this, this.constructor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ const TestExpectation = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function createHook(callback, name) {
|
function createHook(callback, name) {
|
||||||
const location = Location.getCallerLocation(__filename);
|
const location = Location.getCallerLocation();
|
||||||
return { name, body: callback, location };
|
return { name, body: callback, location };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ class TestCollector {
|
||||||
this._currentSuite = new Suite(null, '', new Location());
|
this._currentSuite = new Suite(null, '', new Location());
|
||||||
|
|
||||||
this._api.describe = specBuilder(this._suiteModifiers, this._suiteAttributes, (specs, name, suiteCallback, ...suiteArgs) => {
|
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);
|
const suite = new Suite(this._currentSuite, name, location);
|
||||||
for (const { callback, args } of specs)
|
for (const { callback, args } of specs)
|
||||||
callback(suite, ...args);
|
callback(suite, ...args);
|
||||||
|
|
@ -146,7 +146,7 @@ class TestCollector {
|
||||||
this._currentSuite = suite.parentSuite();
|
this._currentSuite = suite.parentSuite();
|
||||||
});
|
});
|
||||||
this._api.it = specBuilder(this._testModifiers, this._testAttributes, (specs, name, testCallback) => {
|
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);
|
const test = new Test(this._currentSuite, name, testCallback, location);
|
||||||
test.setTimeout(timeout);
|
test.setTimeout(timeout);
|
||||||
for (const { callback, args } of specs)
|
for (const { callback, args } of specs)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue