Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(message-utils): convert URLs to paths #12277

Merged
merged 7 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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