Skip to content

Commit 60a788e

Browse files
authored
fix(message-utils): convert URLs to paths (#12277)
1 parent 6259ce1 commit 60a788e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `[matcher-utils]` Correct diff for expected asymmetric matchers ([#12264](https://github.com/facebook/jest/pull/12264))
88
- `[expect]` Add a fix for `.toHaveProperty('')` ([#12251](https://github.com/facebook/jest/pull/12251))
99
- `[jest-environment-node]` Add `atob` and `btoa` ([#12269](https://github.com/facebook/jest/pull/12269))
10+
- `[jest-message-util]` Fix `.getTopFrame()` (and `toMatchInlineSnapshot()`) with `mjs` files ([#12277](https://github.com/facebook/jest/pull/12277))
1011

1112
### Chore & Maintenance
1213

packages/jest-message-util/src/__tests__/messages.test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
import {readFileSync} from 'graceful-fs';
1010
import slash = require('slash');
1111
import tempy = require('tempy');
12-
import {formatExecError, formatResultsErrors, formatStackTrace} from '..';
12+
import {
13+
formatExecError,
14+
formatResultsErrors,
15+
formatStackTrace,
16+
getTopFrame,
17+
} from '..';
1318

1419
const rootDir = tempy.directory();
1520

@@ -365,3 +370,18 @@ describe('formatStackTrace', () => {
365370
expect(message).toMatchSnapshot();
366371
});
367372
});
373+
374+
it('getTopFrame should return a path for mjs files', () => {
375+
let stack: Array<string>;
376+
let expectedFile: string;
377+
if (process.platform === 'win32') {
378+
stack = [' at stack (file:///C:/Users/user/project/inline.mjs:1:1)'];
379+
expectedFile = 'C:/Users/user/project/inline.mjs';
380+
} else {
381+
stack = [' at stack (file:///Users/user/project/inline.mjs:1:1)'];
382+
expectedFile = '/Users/user/project/inline.mjs';
383+
}
384+
const frame = getTopFrame(stack);
385+
386+
expect(frame.file).toBe(expectedFile);
387+
});

packages/jest-message-util/src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import * as path from 'path';
9+
import {fileURLToPath} from 'url';
910
import {codeFrameColumns} from '@babel/code-frame';
1011
import chalk = require('chalk');
1112
import * as fs from 'graceful-fs';
@@ -273,6 +274,9 @@ export const getTopFrame = (lines: Array<string>): Frame | null => {
273274
const parsedFrame = stackUtils.parseLine(line.trim());
274275

275276
if (parsedFrame && parsedFrame.file) {
277+
if (parsedFrame.file.startsWith('file://')) {
278+
parsedFrame.file = slash(fileURLToPath(parsedFrame.file));
279+
}
276280
return parsedFrame as Frame;
277281
}
278282
}

0 commit comments

Comments
 (0)