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 6fb5556
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### Fixes

- `[jest-message-util]` Fixes `.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
12 changes: 11 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 @@ -79,6 +79,10 @@ const babelStack =
\u001b[90m 23 | \u001b[39m } \u001b[36melse\u001b[39m \u001b[36mif\u001b[39m (\u001b[36mtypeof\u001b[39m render \u001b[33m!==\u001b[39m \u001b[32m'function'\u001b[39m) {\u001b[0m
`;

const mjsStackTrace =
` ` +
`at stack (file:///Users/user/project/inline.mjs:1:1)`;

beforeEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -365,3 +369,9 @@ describe('formatStackTrace', () => {
expect(message).toMatchSnapshot();
});
});

it('getTopFrame should return a path for mjs files', () => {
const frame = getTopFrame(mjsStackTrace.split(/\n/));

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 6fb5556

Please sign in to comment.