jest-message-util
Advanced tools
Comparing version 29.7.0 to 30.0.0-alpha.1
@@ -1,72 +0,37 @@ | ||
'use strict'; | ||
/*! | ||
* /** | ||
* * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* * | ||
* * This source code is licensed under the MIT license found in the | ||
* * LICENSE file in the root directory of this source tree. | ||
* * / | ||
*/ | ||
/******/ (() => { // webpackBootstrap | ||
/******/ "use strict"; | ||
var __webpack_exports__ = {}; | ||
// This entry need to be wrapped in an IIFE because it uses a non-standard name for the exports (exports). | ||
(() => { | ||
var exports = __webpack_exports__; | ||
Object.defineProperty(exports, '__esModule', { | ||
Object.defineProperty(exports, "__esModule", ({ | ||
value: true | ||
}); | ||
exports.separateMessageFromStack = | ||
exports.indentAllLines = | ||
exports.getTopFrame = | ||
exports.getStackTraceLines = | ||
exports.formatStackTrace = | ||
exports.formatResultsErrors = | ||
exports.formatPath = | ||
exports.formatExecError = | ||
void 0; | ||
var path = _interopRequireWildcard(require('path')); | ||
var _url = require('url'); | ||
var _util = require('util'); | ||
var _codeFrame = require('@babel/code-frame'); | ||
var _chalk = _interopRequireDefault(require('chalk')); | ||
var fs = _interopRequireWildcard(require('graceful-fs')); | ||
var _micromatch = _interopRequireDefault(require('micromatch')); | ||
var _slash = _interopRequireDefault(require('slash')); | ||
var _stackUtils = _interopRequireDefault(require('stack-utils')); | ||
var _prettyFormat = require('pretty-format'); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
function _getRequireWildcardCache(nodeInterop) { | ||
if (typeof WeakMap !== 'function') return null; | ||
var cacheBabelInterop = new WeakMap(); | ||
var cacheNodeInterop = new WeakMap(); | ||
return (_getRequireWildcardCache = function (nodeInterop) { | ||
return nodeInterop ? cacheNodeInterop : cacheBabelInterop; | ||
})(nodeInterop); | ||
} | ||
function _interopRequireWildcard(obj, nodeInterop) { | ||
if (!nodeInterop && obj && obj.__esModule) { | ||
return obj; | ||
} | ||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) { | ||
return {default: obj}; | ||
} | ||
var cache = _getRequireWildcardCache(nodeInterop); | ||
if (cache && cache.has(obj)) { | ||
return cache.get(obj); | ||
} | ||
var newObj = {}; | ||
var hasPropertyDescriptor = | ||
Object.defineProperty && Object.getOwnPropertyDescriptor; | ||
for (var key in obj) { | ||
if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = hasPropertyDescriptor | ||
? Object.getOwnPropertyDescriptor(obj, key) | ||
: null; | ||
if (desc && (desc.get || desc.set)) { | ||
Object.defineProperty(newObj, key, desc); | ||
} else { | ||
newObj[key] = obj[key]; | ||
} | ||
} | ||
} | ||
newObj.default = obj; | ||
if (cache) { | ||
cache.set(obj, newObj); | ||
} | ||
return newObj; | ||
} | ||
})); | ||
exports.separateMessageFromStack = exports.indentAllLines = exports.getTopFrame = exports.getStackTraceLines = exports.formatStackTrace = exports.formatResultsErrors = exports.formatPath = exports.formatExecError = void 0; | ||
var path = _interopRequireWildcard(require("path")); | ||
var _url = require("url"); | ||
var _util = require("util"); | ||
var _codeFrame = require("@babel/code-frame"); | ||
var _chalk = _interopRequireDefault(require("chalk")); | ||
var fs = _interopRequireWildcard(require("graceful-fs")); | ||
var _micromatch = _interopRequireDefault(require("micromatch")); | ||
var _slash = _interopRequireDefault(require("slash")); | ||
var _stackUtils = _interopRequireDefault(require("stack-utils")); | ||
var _prettyFormat = require("pretty-format"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } | ||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } | ||
var Symbol = globalThis['jest-symbol-do-not-touch'] || globalThis.Symbol; | ||
var Symbol = globalThis['jest-symbol-do-not-touch'] || globalThis.Symbol; | ||
var jestReadFile = | ||
globalThis[Symbol.for('jest-native-read-file')] || fs.readFileSync; | ||
var jestReadFile = globalThis[Symbol.for('jest-native-read-file')] || fs.readFileSync; | ||
/** | ||
@@ -93,6 +58,4 @@ * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// filter for noisy stack trace lines | ||
const JASMINE_IGNORE = | ||
/^\s+at(?:(?:.jasmine-)|\s+jasmine\.buildExpectationResult)/; | ||
const JEST_INTERNALS_IGNORE = | ||
/^\s+at.*?jest(-.*?)?(\/|\\)(build|node_modules|packages)(\/|\\)/; | ||
const JASMINE_IGNORE = /^\s+at(?:(?:.jasmine-)|\s+jasmine\.buildExpectationResult)/; | ||
const JEST_INTERNALS_IGNORE = /^\s+at.*?jest(-.*?)?(\/|\\)(build|node_modules|packages)(\/|\\)/; | ||
const ANONYMOUS_FN_IGNORE = /^\s+at <anonymous>.*$/; | ||
@@ -111,4 +74,3 @@ const ANONYMOUS_PROMISE_IGNORE = /^\s+at (new )?Promise \(<anonymous>\).*$/; | ||
const NOT_EMPTY_LINE_REGEXP = /^(?!$)/gm; | ||
const indentAllLines = lines => | ||
lines.replace(NOT_EMPTY_LINE_REGEXP, MESSAGE_INDENT); | ||
const indentAllLines = lines => lines.replace(NOT_EMPTY_LINE_REGEXP, MESSAGE_INDENT); | ||
exports.indentAllLines = indentAllLines; | ||
@@ -121,17 +83,12 @@ const trim = string => (string || '').trim(); | ||
// which will get misaligned. | ||
const trimPaths = string => | ||
string.match(STACK_PATH_REGEXP) ? trim(string) : string; | ||
const trimPaths = string => string.match(STACK_PATH_REGEXP) ? trim(string) : string; | ||
const getRenderedCallsite = (fileContent, line, column) => { | ||
let renderedCallsite = (0, _codeFrame.codeFrameColumns)( | ||
fileContent, | ||
{ | ||
start: { | ||
column, | ||
line | ||
} | ||
}, | ||
{ | ||
highlightCode: true | ||
let renderedCallsite = (0, _codeFrame.codeFrameColumns)(fileContent, { | ||
start: { | ||
column, | ||
line | ||
} | ||
); | ||
}, { | ||
highlightCode: true | ||
}); | ||
renderedCallsite = indentAllLines(renderedCallsite); | ||
@@ -143,7 +100,3 @@ renderedCallsite = `\n${renderedCallsite}\n`; | ||
function checkForCommonEnvironmentErrors(error) { | ||
if ( | ||
error.includes('ReferenceError: document is not defined') || | ||
error.includes('ReferenceError: window is not defined') || | ||
error.includes('ReferenceError: navigator is not defined') | ||
) { | ||
if (error.includes('ReferenceError: document is not defined') || error.includes('ReferenceError: window is not defined') || error.includes('ReferenceError: navigator is not defined')) { | ||
return warnAboutWrongTestEnvironment(error, 'jsdom'); | ||
@@ -156,9 +109,3 @@ } else if (error.includes('.unref is not a function')) { | ||
function warnAboutWrongTestEnvironment(error, env) { | ||
return ( | ||
_chalk.default.bold.red( | ||
`The error below may be caused by using the wrong test environment, see ${_chalk.default.dim.underline( | ||
'https://jestjs.io/docs/configuration#testenvironment-string' | ||
)}.\nConsider using the "${env}" test environment.\n\n` | ||
) + error | ||
); | ||
return _chalk.default.bold.red(`The error below may be caused by using the wrong test environment, see ${_chalk.default.dim.underline('https://jestjs.io/docs/configuration#testenvironment-string')}.\nConsider using the "${env}" test environment.\n\n`) + error; | ||
} | ||
@@ -169,10 +116,3 @@ | ||
// are executed. | ||
const formatExecError = ( | ||
error, | ||
config, | ||
options, | ||
testPath, | ||
reuseMessage, | ||
noTitle | ||
) => { | ||
const formatExecError = (error, config, options, testPath, reuseMessage, noTitle) => { | ||
if (!error || typeof error === 'number') { | ||
@@ -191,8 +131,5 @@ error = new Error(`Expected an Error, but "${String(error)}" was thrown`); | ||
message = error.message; | ||
stack = | ||
typeof error.stack === 'string' | ||
? error.stack | ||
: `thrown: ${(0, _prettyFormat.format)(error, { | ||
maxDepth: 3 | ||
})}`; | ||
stack = typeof error.stack === 'string' ? error.stack : `thrown: ${(0, _prettyFormat.format)(error, { | ||
maxDepth: 3 | ||
})}`; | ||
if ('cause' in error) { | ||
@@ -202,6 +139,3 @@ const prefix = '\n\nCause:\n'; | ||
cause += `${prefix}${error.cause}`; | ||
} else if ( | ||
_util.types.isNativeError(error.cause) || | ||
error.cause instanceof Error | ||
) { | ||
} else if (_util.types.isNativeError(error.cause) || error.cause instanceof Error) { | ||
/* `isNativeError` is used, because the error might come from another realm. | ||
@@ -212,10 +146,3 @@ `instanceof Error` is used because `isNativeError` does return `false` for some | ||
*/ | ||
const formatted = formatExecError( | ||
error.cause, | ||
config, | ||
options, | ||
testPath, | ||
reuseMessage, | ||
true | ||
); | ||
const formatted = formatExecError(error.cause, config, options, testPath, reuseMessage, true); | ||
cause += `${prefix}${formatted}`; | ||
@@ -226,12 +153,3 @@ } | ||
for (const subError of error.errors) { | ||
subErrors.push( | ||
formatExecError( | ||
subError, | ||
config, | ||
options, | ||
testPath, | ||
reuseMessage, | ||
true | ||
) | ||
); | ||
subErrors.push(formatExecError(subError, config, options, testPath, reuseMessage, true)); | ||
} | ||
@@ -251,10 +169,4 @@ } | ||
message = indentAllLines(message); | ||
stack = | ||
stack && !options.noStackTrace | ||
? `\n${formatStackTrace(stack, config, options, testPath)}` | ||
: ''; | ||
if ( | ||
typeof stack !== 'string' || | ||
(blankStringRegexp.test(message) && blankStringRegexp.test(stack)) | ||
) { | ||
stack = stack && !options.noStackTrace ? `\n${formatStackTrace(stack, config, options, testPath)}` : ''; | ||
if (typeof stack !== 'string' || blankStringRegexp.test(message) && blankStringRegexp.test(stack)) { | ||
// this can happen if an empty object is thrown. | ||
@@ -272,8 +184,3 @@ message = `thrown: ${(0, _prettyFormat.format)(error, { | ||
const title = noTitle ? '' : `${TITLE_INDENT + TITLE_BULLET}`; | ||
const subErrorStr = | ||
subErrors.length > 0 | ||
? indentAllLines( | ||
`\n\nErrors contained in AggregateError:\n${subErrors.join('\n')}` | ||
) | ||
: ''; | ||
const subErrorStr = subErrors.length > 0 ? indentAllLines(`\n\nErrors contained in AggregateError:\n${subErrors.join('\n')}`) : ''; | ||
return `${title + messageToUse + stack + cause + subErrorStr}\n`; | ||
@@ -327,8 +234,3 @@ }; | ||
// highlight paths from the current test file | ||
if ( | ||
(config.testMatch && | ||
config.testMatch.length && | ||
(0, _micromatch.default)([filePath], config.testMatch).length > 0) || | ||
filePath === relativeTestPath | ||
) { | ||
if (config.testMatch && config.testMatch.length > 0 && (0, _micromatch.default)([filePath], config.testMatch).length > 0 || filePath === relativeTestPath) { | ||
filePath = _chalk.default.reset.cyan(filePath); | ||
@@ -339,9 +241,6 @@ } | ||
exports.formatPath = formatPath; | ||
const getStackTraceLines = ( | ||
stack, | ||
options = { | ||
noCodeFrame: false, | ||
noStackTrace: false | ||
} | ||
) => removeInternalStackEntries(stack.split(/\n/), options); | ||
const getStackTraceLines = (stack, options = { | ||
noCodeFrame: false, | ||
noStackTrace: false | ||
}) => removeInternalStackEntries(stack.split(/\n/), options); | ||
exports.getStackTraceLines = getStackTraceLines; | ||
@@ -356,5 +255,3 @@ const getTopFrame = lines => { | ||
if (parsedFrame.file.startsWith('file://')) { | ||
parsedFrame.file = (0, _slash.default)( | ||
(0, _url.fileURLToPath)(parsedFrame.file) | ||
); | ||
parsedFrame.file = (0, _slash.default)((0, _url.fileURLToPath)(parsedFrame.file)); | ||
} | ||
@@ -370,9 +267,11 @@ return parsedFrame; | ||
let renderedCallsite = ''; | ||
const relativeTestPath = testPath | ||
? (0, _slash.default)(path.relative(config.rootDir, testPath)) | ||
: null; | ||
const relativeTestPath = testPath ? (0, _slash.default)(path.relative(config.rootDir, testPath)) : null; | ||
if (!options.noStackTrace && !options.noCodeFrame) { | ||
const topFrame = getTopFrame(lines); | ||
if (topFrame) { | ||
const {column, file: filename, line} = topFrame; | ||
const { | ||
column, | ||
file: filename, | ||
line | ||
} = topFrame; | ||
if (line && filename && path.isAbsolute(filename)) { | ||
@@ -391,22 +290,8 @@ let fileContent; | ||
} | ||
const stacktrace = lines | ||
.filter(Boolean) | ||
.map( | ||
line => | ||
STACK_INDENT + formatPath(trimPaths(line), config, relativeTestPath) | ||
) | ||
.join('\n'); | ||
return renderedCallsite | ||
? `${renderedCallsite}\n${stacktrace}` | ||
: `\n${stacktrace}`; | ||
const stacktrace = lines.filter(Boolean).map(line => STACK_INDENT + formatPath(trimPaths(line), config, relativeTestPath)).join('\n'); | ||
return renderedCallsite ? `${renderedCallsite}\n${stacktrace}` : `\n${stacktrace}`; | ||
}; | ||
exports.formatStackTrace = formatStackTrace; | ||
function isErrorOrStackWithCause(errorOrStack) { | ||
return ( | ||
typeof errorOrStack !== 'string' && | ||
'cause' in errorOrStack && | ||
(typeof errorOrStack.cause === 'string' || | ||
_util.types.isNativeError(errorOrStack.cause) || | ||
errorOrStack.cause instanceof Error) | ||
); | ||
return typeof errorOrStack !== 'string' && 'cause' in errorOrStack && (typeof errorOrStack.cause === 'string' || _util.types.isNativeError(errorOrStack.cause) || errorOrStack.cause instanceof Error); | ||
} | ||
@@ -416,10 +301,8 @@ function formatErrorStack(errorOrStack, config, options, testPath) { | ||
// thus we need to sanitize and clean it for proper display using separateMessageFromStack. | ||
const sourceStack = | ||
typeof errorOrStack === 'string' ? errorOrStack : errorOrStack.stack || ''; | ||
let {message, stack} = separateMessageFromStack(sourceStack); | ||
stack = options.noStackTrace | ||
? '' | ||
: `${STACK_TRACE_COLOR( | ||
formatStackTrace(stack, config, options, testPath) | ||
)}\n`; | ||
const sourceStack = typeof errorOrStack === 'string' ? errorOrStack : errorOrStack.stack || ''; | ||
let { | ||
message, | ||
stack | ||
} = separateMessageFromStack(sourceStack); | ||
stack = options.noStackTrace ? '' : `${STACK_TRACE_COLOR(formatStackTrace(stack, config, options, testPath))}\n`; | ||
message = checkForCommonEnvironmentErrors(message); | ||
@@ -429,8 +312,3 @@ message = indentAllLines(message); | ||
if (isErrorOrStackWithCause(errorOrStack)) { | ||
const nestedCause = formatErrorStack( | ||
errorOrStack.cause, | ||
config, | ||
options, | ||
testPath | ||
); | ||
const nestedCause = formatErrorStack(errorOrStack.cause, config, options, testPath); | ||
cause = `\n${MESSAGE_INDENT}Cause:\n${nestedCause}`; | ||
@@ -444,15 +322,7 @@ } | ||
} | ||
if ( | ||
_util.types.isNativeError(failureDetails) || | ||
failureDetails instanceof Error | ||
) { | ||
if (_util.types.isNativeError(failureDetails) || failureDetails instanceof Error) { | ||
return failureDetails; // receiving raw errors for jest-circus | ||
} | ||
if ( | ||
typeof failureDetails === 'object' && | ||
'error' in failureDetails && | ||
(_util.types.isNativeError(failureDetails.error) || | ||
failureDetails.error instanceof Error) | ||
) { | ||
if (typeof failureDetails === 'object' && 'error' in failureDetails && (_util.types.isNativeError(failureDetails.error) || failureDetails.error instanceof Error)) { | ||
return failureDetails.error; // receiving instances of FailedAssertion for jest-jasmine | ||
@@ -465,3 +335,3 @@ } | ||
const failedResults = testResults.reduce((errors, result) => { | ||
result.failureMessages.forEach((item, index) => { | ||
for (const [index, item] of result.failureMessages.entries()) { | ||
errors.push({ | ||
@@ -472,39 +342,23 @@ content: item, | ||
}); | ||
}); | ||
} | ||
return errors; | ||
}, []); | ||
if (!failedResults.length) { | ||
if (failedResults.length === 0) { | ||
return null; | ||
} | ||
return failedResults | ||
.map(({result, content, failureDetails}) => { | ||
const rootErrorOrStack = failureDetailsToErrorOrStack( | ||
failureDetails, | ||
content | ||
); | ||
const title = `${_chalk.default.bold.red( | ||
TITLE_INDENT + | ||
TITLE_BULLET + | ||
result.ancestorTitles.join(ANCESTRY_SEPARATOR) + | ||
(result.ancestorTitles.length ? ANCESTRY_SEPARATOR : '') + | ||
result.title | ||
)}\n`; | ||
return `${title}\n${formatErrorStack( | ||
rootErrorOrStack, | ||
config, | ||
options, | ||
testPath | ||
)}`; | ||
}) | ||
.join('\n'); | ||
return failedResults.map(({ | ||
result, | ||
content, | ||
failureDetails | ||
}) => { | ||
const rootErrorOrStack = failureDetailsToErrorOrStack(failureDetails, content); | ||
const title = `${_chalk.default.bold.red(TITLE_INDENT + TITLE_BULLET + result.ancestorTitles.join(ANCESTRY_SEPARATOR) + (result.ancestorTitles.length > 0 ? ANCESTRY_SEPARATOR : '') + result.title)}\n`; | ||
return `${title}\n${formatErrorStack(rootErrorOrStack, config, options, testPath)}`; | ||
}).join('\n'); | ||
}; | ||
exports.formatResultsErrors = formatResultsErrors; | ||
const errorRegexp = /^Error:?\s*$/; | ||
const removeBlankErrorLine = str => | ||
str | ||
.split('\n') | ||
// Lines saying just `Error:` are useless | ||
.filter(line => !errorRegexp.test(line)) | ||
.join('\n') | ||
.trimRight(); | ||
const removeBlankErrorLine = str => str.split('\n') | ||
// Lines saying just `Error:` are useless | ||
.filter(line => !errorRegexp.test(line)).join('\n').trimRight(); | ||
@@ -526,5 +380,3 @@ // jasmine and worker farm sometimes don't give us access to the actual | ||
// remove the prefix from the message because it is generally not useful. | ||
const messageMatch = content.match( | ||
/^(?:Error: )?([\s\S]*?(?=\n\s*at\s.*:\d*:\d*)|\s*.*)([\s\S]*)$/ | ||
); | ||
const messageMatch = content.match(/^(?:Error: )?([\s\S]*?(?=\n\s*at\s.*:\d*:\d*)|\s*.*)([\s\S]*)$/); | ||
if (!messageMatch) { | ||
@@ -542,1 +394,6 @@ // For typescript | ||
exports.separateMessageFromStack = separateMessageFromStack; | ||
})(); | ||
module.exports = __webpack_exports__; | ||
/******/ })() | ||
; |
{ | ||
"name": "jest-message-util", | ||
"version": "29.7.0", | ||
"version": "30.0.0-alpha.1", | ||
"repository": { | ||
@@ -10,3 +10,3 @@ "type": "git", | ||
"engines": { | ||
"node": "^14.15.0 || ^16.10.0 || >=18.0.0" | ||
"node": "^16.10.0 || ^18.12.0 || >=20.0.0" | ||
}, | ||
@@ -19,2 +19,4 @@ "license": "MIT", | ||
"types": "./build/index.d.ts", | ||
"require": "./build/index.js", | ||
"import": "./build/index.mjs", | ||
"default": "./build/index.js" | ||
@@ -26,3 +28,3 @@ }, | ||
"@babel/code-frame": "^7.12.13", | ||
"@jest/types": "^29.6.3", | ||
"@jest/types": "30.0.0-alpha.1", | ||
"@types/stack-utils": "^2.0.0", | ||
@@ -32,3 +34,3 @@ "chalk": "^4.0.0", | ||
"micromatch": "^4.0.4", | ||
"pretty-format": "^29.7.0", | ||
"pretty-format": "30.0.0-alpha.1", | ||
"slash": "^3.0.0", | ||
@@ -46,3 +48,3 @@ "stack-utils": "^2.0.3" | ||
}, | ||
"gitHead": "4e56991693da7cd4c3730dc3579a1dd1403ee630" | ||
"gitHead": "d005cb2505c041583e0c5636d006e08666a54b63" | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
20491
429
2
+ Added@babel/helper-validator-identifier@7.24.5(transitive)
+ Added@babel/highlight@7.24.5(transitive)
+ Added@jest/schemas@30.0.0-alpha.1(transitive)
+ Added@jest/types@30.0.0-alpha.1(transitive)
+ Added@sinclair/typebox@0.31.28(transitive)
+ Addedpretty-format@30.0.0-alpha.1(transitive)
+ Addedreact-is@18.3.1(transitive)
- Removed@babel/helper-validator-identifier@7.22.20(transitive)
- Removed@babel/highlight@7.24.2(transitive)
- Removed@jest/schemas@29.6.3(transitive)
- Removed@jest/types@29.6.3(transitive)
- Removed@sinclair/typebox@0.27.8(transitive)
- Removedpretty-format@29.7.0(transitive)
- Removedreact-is@18.2.0(transitive)
Updated@jest/types@30.0.0-alpha.1
Updatedpretty-format@30.0.0-alpha.1