Skip to content

Commit

Permalink
fix(message-utils): convert URLs to paths (#12277)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Feb 2, 2022
1 parent 6259ce1 commit 60a788e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- `[matcher-utils]` Correct diff for expected asymmetric matchers ([#12264](https://github.com/facebook/jest/pull/12264))
- `[expect]` Add a fix for `.toHaveProperty('')` ([#12251](https://github.com/facebook/jest/pull/12251))
- `[jest-environment-node]` Add `atob` and `btoa` ([#12269](https://github.com/facebook/jest/pull/12269))
- `[jest-message-util]` Fix `.getTopFrame()` (and `toMatchInlineSnapshot()`) with `mjs` files ([#12277](https://github.com/facebook/jest/pull/12277))

### Chore & Maintenance

Expand Down
22 changes: 21 additions & 1 deletion packages/jest-message-util/src/__tests__/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import {readFileSync} from 'graceful-fs';
import slash = require('slash');
import tempy = require('tempy');
import {formatExecError, formatResultsErrors, formatStackTrace} from '..';
import {
formatExecError,
formatResultsErrors,
formatStackTrace,
getTopFrame,
} from '..';

const rootDir = tempy.directory();

Expand Down Expand Up @@ -365,3 +370,18 @@ describe('formatStackTrace', () => {
expect(message).toMatchSnapshot();
});
});

it('getTopFrame should return a path for mjs files', () => {
let stack: Array<string>;
let expectedFile: string;
if (process.platform === 'win32') {
stack = [' at stack (file:///C:/Users/user/project/inline.mjs:1:1)'];
expectedFile = 'C:/Users/user/project/inline.mjs';
} else {
stack = [' at stack (file:///Users/user/project/inline.mjs:1:1)'];
expectedFile = '/Users/user/project/inline.mjs';
}
const frame = getTopFrame(stack);

expect(frame.file).toBe(expectedFile);
});
4 changes: 4 additions & 0 deletions packages/jest-message-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

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

if (parsedFrame && parsedFrame.file) {
if (parsedFrame.file.startsWith('file://')) {
parsedFrame.file = slash(fileURLToPath(parsedFrame.file));
}
return parsedFrame as Frame;
}
}
Expand Down

0 comments on commit 60a788e

Please sign in to comment.