Skip to content

Commit

Permalink
Return correct file path for mjs files.
Browse files Browse the repository at this point in the history
Workaround for tapjs/stack-utils#60
  • Loading branch information
mshima committed Feb 2, 2022
1 parent 1614524 commit 28f2109
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### Fixes

- `[jest-message-util]` Fix `.getTopFrame()` (and `toMatchInlineSnapshot()`) with `mjs` files ([#12277](https://github.com/facebook/jest/pull/12277))
- `[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))

Expand Down
8 changes: 7 additions & 1 deletion packages/jest-message-util/src/__tests__/messages.test.ts
Expand Up @@ -9,7 +9,7 @@
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 +365,9 @@ describe('formatStackTrace', () => {
expect(message).toMatchSnapshot();
});
});

it('getTopFrame should return a path for mjs files', () => {
const frame = getTopFrame([' at stack (file:///Users/user/project/inline.mjs:1:1)']);

expect(frame.file).toBe('/Users/user/project/inline.mjs');
});
4 changes: 4 additions & 0 deletions packages/jest-message-util/src/index.ts
Expand Up @@ -14,6 +14,7 @@ import slash = require('slash');
import StackUtils = require('stack-utils');
import type {Config, TestResult} from '@jest/types';
import {format as prettyFormat} from 'pretty-format';
import {fileURLToPath} from 'url';
import type {Frame} from './types';

export type {Frame} from './types';
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 = fileURLToPath(parsedFrame.file);
}
return parsedFrame as Frame;
}
}
Expand Down

0 comments on commit 28f2109

Please sign in to comment.