diff --git a/CHANGELOG.md b/CHANGELOG.md index 69bdc048a79e..b1dbc41bd099 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/jest-message-util/src/__tests__/messages.test.ts b/packages/jest-message-util/src/__tests__/messages.test.ts index cb4d7e91f959..a2ab092378c4 100644 --- a/packages/jest-message-util/src/__tests__/messages.test.ts +++ b/packages/jest-message-util/src/__tests__/messages.test.ts @@ -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(); @@ -365,3 +370,18 @@ describe('formatStackTrace', () => { expect(message).toMatchSnapshot(); }); }); + +it('getTopFrame should return a path for mjs files', () => { + let stack: Array; + 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); +}); diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index f02a7f0fefeb..1aa0899e5bbf 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -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'; @@ -273,6 +274,9 @@ export const getTopFrame = (lines: Array): 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; } }