From e519a3d762e708a3f27165156e4de56ed82b87bd Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Mon, 6 Apr 2020 17:47:17 -0700 Subject: [PATCH] fix(testrunner): better capture Location for hooks (#1680) --- utils/testrunner/Location.js | 6 ++---- utils/testrunner/Matchers.js | 2 +- utils/testrunner/Test.js | 2 +- utils/testrunner/TestCollector.js | 4 ++-- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/utils/testrunner/Location.js b/utils/testrunner/Location.js index 2330f031f5..a18e3618a2 100644 --- a/utils/testrunner/Location.js +++ b/utils/testrunner/Location.js @@ -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; diff --git a/utils/testrunner/Matchers.js b/utils/testrunner/Matchers.js index e61b9317cc..ac1ee8b889 100644 --- a/utils/testrunner/Matchers.js +++ b/utils/testrunner/Matchers.js @@ -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); } } diff --git a/utils/testrunner/Test.js b/utils/testrunner/Test.js index 0a2754f9fd..90753b1e97 100644 --- a/utils/testrunner/Test.js +++ b/utils/testrunner/Test.js @@ -23,7 +23,7 @@ const TestExpectation = { }; function createHook(callback, name) { - const location = Location.getCallerLocation(__filename); + const location = Location.getCallerLocation(); return { name, body: callback, location }; } diff --git a/utils/testrunner/TestCollector.js b/utils/testrunner/TestCollector.js index fc6d695299..480c93f11a 100644 --- a/utils/testrunner/TestCollector.js +++ b/utils/testrunner/TestCollector.js @@ -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)