Skip to content

Commit 9919444

Browse files
committed
refactor(src): ensure filepath inference works in esm context
1 parent 57dd54f commit 9919444

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/plugin-tester.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type {
3333
import type { Class } from 'type-fest';
3434

3535
const parseErrorStackRegExp =
36-
/at (?<fn>\S+) (?:.*? )?\(?(?<path>(?:\/|file:|\w:\\).*?)(?:\)|$)/i;
36+
/at (?:(?<fn>\S+) )?(?:.*? )?\(?(?<path>(?:\/|file:|\w:\\).*?)(?:\)|$)/i;
3737

3838
const parseScriptFilepathRegExp =
3939
/(\/|\\)babel-plugin-tester(\/|\\)(dist|src)(\/|\\)(index|plugin-tester)\.(j|t)s$/;
@@ -118,8 +118,8 @@ function pluginTester(options: PluginTesterOptions = {}) {
118118
const globalContextExpectFnHasToMatchSnapshot = (() => {
119119
try {
120120
return globalContextHasExpectFn
121-
? typeof expect(undefined)?.toMatchSnapshot == 'function'
122-
: false;
121+
? typeof expect(undefined)?.toMatchSnapshot == 'function'
122+
: false;
123123
} catch {
124124
/* istanbul ignore next */
125125
return false;
@@ -328,7 +328,7 @@ function pluginTester(options: PluginTesterOptions = {}) {
328328
process.cwd()
329329
);
330330

331-
debug2('plugin name inference succeeded: %O', name);
331+
debug2('plugin name inference result: %O', name);
332332
return name;
333333
} catch {
334334
debug2('plugin name inference failed');
@@ -358,11 +358,16 @@ function pluginTester(options: PluginTesterOptions = {}) {
358358
const { fn: functionName, path: filePath } =
359359
line.match(parseErrorStackRegExp)?.groups || {};
360360

361-
return functionName && filePath
361+
return filePath
362362
? {
363363
functionName,
364364
// ? Paranoid just in case the script name/path has colons
365-
filePath: filePath.split(':').slice(0, -2).join(':')
365+
filePath: filePath
366+
.split('file://')
367+
.at(-1)!
368+
.split(':')
369+
.slice(0, -2)
370+
.join(':')
366371
}
367372
: undefined;
368373
})

0 commit comments

Comments
 (0)