jest-react
Advanced tools
Comparing version
@@ -11,345 +11,104 @@ /** | ||
'use strict'; | ||
if (process.env.NODE_ENV !== "production") { | ||
(function() { | ||
'use strict'; | ||
var React = require('react'); | ||
var Scheduler = require('scheduler/unstable_mock'); | ||
var assign = Object.assign; | ||
// ATTENTION | ||
// When adding new symbols to this file, | ||
// Please consider also adding to 'react-devtools-shared/src/backend/ReactSymbols' | ||
// The Symbol used to tag the ReactElement-like types. | ||
var REACT_ELEMENT_TYPE = Symbol.for('react.element'); | ||
var REACT_FRAGMENT_TYPE = Symbol.for('react.fragment'); | ||
var isArrayImpl = Array.isArray; // eslint-disable-next-line no-redeclare | ||
function isArray(a) { | ||
return isArrayImpl(a); | ||
} | ||
var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; | ||
function error(format) { | ||
{ | ||
{ | ||
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
} | ||
printWarning('error', format, args); | ||
} | ||
} | ||
} | ||
function printWarning(level, format, args) { | ||
// When changing this logic, you might want to also | ||
// update consoleWithStackDev.www.js as well. | ||
{ | ||
var ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame; | ||
var stack = ReactDebugCurrentFrame.getStackAddendum(); | ||
if (stack !== '') { | ||
format += '%s'; | ||
args = args.concat([stack]); | ||
} // eslint-disable-next-line react-internal/safe-string-coercion | ||
var argsWithFormat = args.map(function (item) { | ||
return String(item); | ||
}); // Careful: RN currently depends on this prefix | ||
argsWithFormat.unshift('Warning: ' + format); // We intentionally don't use spread (or .apply) directly because it | ||
// breaks IE9: https://github.com/facebook/react/issues/13610 | ||
// eslint-disable-next-line react-internal/no-production-logging | ||
Function.prototype.apply.call(console[level], console, argsWithFormat); | ||
} | ||
} | ||
var didWarnAboutMessageChannel = false; | ||
var enqueueTaskImpl = null; | ||
function enqueueTask(task) { | ||
if (enqueueTaskImpl === null) { | ||
try { | ||
// read require off the module object to get around the bundlers. | ||
// we don't want them to detect a require and bundle a Node polyfill. | ||
var requireString = ('require' + Math.random()).slice(0, 7); | ||
var nodeRequire = module && module[requireString]; // assuming we're in node, let's try to get node's | ||
// version of setImmediate, bypassing fake timers if any. | ||
enqueueTaskImpl = nodeRequire.call(module, 'timers').setImmediate; | ||
} catch (_err) { | ||
// we're in a browser | ||
// we can't use regular timers because they may still be faked | ||
// so we try MessageChannel+postMessage instead | ||
enqueueTaskImpl = function (callback) { | ||
{ | ||
if (didWarnAboutMessageChannel === false) { | ||
didWarnAboutMessageChannel = true; | ||
if (typeof MessageChannel === 'undefined') { | ||
error('This browser does not have a MessageChannel implementation, ' + 'so enqueuing tasks via await act(async () => ...) will fail. ' + 'Please file an issue at https://github.com/facebook/react/issues ' + 'if you encounter this warning.'); | ||
} | ||
} | ||
} | ||
var channel = new MessageChannel(); | ||
channel.port1.onmessage = callback; | ||
channel.port2.postMessage(undefined); | ||
}; | ||
} | ||
} | ||
return enqueueTaskImpl(task); | ||
} | ||
var actingUpdatesScopeDepth = 0; | ||
function act(scope) { | ||
if (Scheduler.unstable_flushUntilNextPaint === undefined) { | ||
throw Error('This version of `act` requires a special mock build of Scheduler.'); | ||
} // $FlowFixMe: _isMockFunction doesn't exist on function | ||
if (setTimeout._isMockFunction !== true) { | ||
throw Error("This version of `act` requires Jest's timer mocks " + '(i.e. jest.useFakeTimers).'); | ||
} | ||
var previousIsActEnvironment = global.IS_REACT_ACT_ENVIRONMENT; | ||
var previousActingUpdatesScopeDepth = actingUpdatesScopeDepth; | ||
actingUpdatesScopeDepth++; | ||
if ( actingUpdatesScopeDepth === 1) { | ||
// Because this is not the "real" `act`, we set this to `false` so React | ||
// knows not to fire `act` warnings. | ||
global.IS_REACT_ACT_ENVIRONMENT = false; | ||
} | ||
var unwind = function () { | ||
if ( actingUpdatesScopeDepth === 1) { | ||
global.IS_REACT_ACT_ENVIRONMENT = previousIsActEnvironment; | ||
} | ||
actingUpdatesScopeDepth--; | ||
{ | ||
if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) { | ||
// if it's _less than_ previousActingUpdatesScopeDepth, then we can | ||
// assume the 'other' one has warned | ||
error('You seem to have overlapping act() calls, this is not supported. ' + 'Be sure to await previous act() calls before making a new one. '); | ||
} | ||
} | ||
}; // TODO: This would be way simpler if 1) we required a promise to be | ||
// returned and 2) we could use async/await. Since it's only our used in | ||
// our test suite, we should be able to. | ||
try { | ||
var result = scope(); | ||
if (typeof result === 'object' && result !== null && // $FlowFixMe[method-unbinding] | ||
typeof result.then === 'function') { | ||
var thenableResult = result; | ||
return { | ||
then: function (resolve, reject) { | ||
thenableResult.then(function (returnValue) { | ||
flushActWork(function () { | ||
unwind(); | ||
resolve(returnValue); | ||
}, function (error) { | ||
unwind(); | ||
reject(error); | ||
}); | ||
}, function (error) { | ||
unwind(); | ||
reject(error); | ||
}); | ||
} | ||
}; | ||
} else { | ||
var returnValue = result; | ||
"use strict"; | ||
"production" !== process.env.NODE_ENV && | ||
(function () { | ||
function captureAssertion(fn) { | ||
try { | ||
// TODO: Let's not support non-async scopes at all in our tests. Need to | ||
// migrate existing tests. | ||
var didFlushWork; | ||
do { | ||
didFlushWork = Scheduler.unstable_flushAllWithoutAsserting(); | ||
} while (didFlushWork); | ||
fn(); | ||
} catch (error) { | ||
return { | ||
then: function (resolve, reject) { | ||
resolve(returnValue); | ||
pass: !1, | ||
message: function () { | ||
return error.message; | ||
} | ||
}; | ||
} finally { | ||
unwind(); | ||
} | ||
return { pass: !0 }; | ||
} | ||
} catch (error) { | ||
unwind(); | ||
throw error; | ||
} | ||
} | ||
function flushActWork(resolve, reject) { | ||
if (Scheduler.unstable_hasPendingWork()) { | ||
try { | ||
Scheduler.unstable_flushUntilNextPaint(); | ||
} catch (error) { | ||
reject(error); | ||
} // If Scheduler yields while there's still work, it's so that we can | ||
// unblock the main thread (e.g. for paint or for microtasks). Yield to | ||
// the main thread and continue in a new task. | ||
enqueueTask(function () { | ||
return flushActWork(resolve, reject); | ||
}); | ||
return; | ||
} // Once the scheduler queue is empty, run all the timers. The purpose of this | ||
// is to force any pending fallbacks to commit. The public version of act does | ||
// this with dev-only React runtime logic, but since our internal act needs to | ||
// work production builds of React, we have to cheat. | ||
// $FlowFixMe: Flow doesn't know about global Jest object | ||
jest.runOnlyPendingTimers(); | ||
if (Scheduler.unstable_hasPendingWork()) { | ||
// Committing a fallback scheduled additional work. Continue flushing. | ||
flushActWork(resolve, reject); | ||
return; | ||
} | ||
resolve(); | ||
} | ||
function captureAssertion(fn) { | ||
// Trick to use a Jest matcher inside another Jest matcher. `fn` contains an | ||
// assertion; if it throws, we capture the error and return it, so the stack | ||
// trace presented to the user points to the original assertion in the | ||
// test file. | ||
try { | ||
fn(); | ||
} catch (error) { | ||
return { | ||
pass: false, | ||
message: function () { | ||
return error.message; | ||
} | ||
}; | ||
} | ||
return { | ||
pass: true | ||
}; | ||
} | ||
function assertYieldsWereCleared(root) { | ||
var Scheduler = root._Scheduler; | ||
var actualYields = Scheduler.unstable_clearYields(); | ||
if (actualYields.length !== 0) { | ||
throw new Error('Log of yielded values is not empty. ' + 'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.'); | ||
} | ||
} | ||
function unstable_toMatchRenderedOutput(root, expectedJSX) { | ||
assertYieldsWereCleared(root); | ||
var actualJSON = root.toJSON(); | ||
var actualJSX; | ||
if (actualJSON === null || typeof actualJSON === 'string') { | ||
actualJSX = actualJSON; | ||
} else if (isArray(actualJSON)) { | ||
if (actualJSON.length === 0) { | ||
actualJSX = null; | ||
} else if (actualJSON.length === 1) { | ||
actualJSX = jsonChildToJSXChild(actualJSON[0]); | ||
} else { | ||
var actualJSXChildren = jsonChildrenToJSXChildren(actualJSON); | ||
if (actualJSXChildren === null || typeof actualJSXChildren === 'string') { | ||
actualJSX = actualJSXChildren; | ||
} else { | ||
actualJSX = { | ||
$$typeof: REACT_ELEMENT_TYPE, | ||
type: REACT_FRAGMENT_TYPE, | ||
key: null, | ||
ref: null, | ||
props: { | ||
children: actualJSXChildren | ||
}, | ||
_owner: null, | ||
_store: {} | ||
}; | ||
} | ||
function assertYieldsWereCleared(root) { | ||
if (0 !== root._Scheduler.unstable_clearLog().length) | ||
throw ( | ||
((root = Error( | ||
"Log of yielded values is not empty. Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first." | ||
)), | ||
Error.captureStackTrace(root, assertYieldsWereCleared), | ||
root) | ||
); | ||
assertConsoleLogsCleared(); | ||
} | ||
} else { | ||
actualJSX = jsonChildToJSXChild(actualJSON); | ||
} | ||
return captureAssertion(function () { | ||
expect(actualJSX).toEqual(expectedJSX); | ||
}); | ||
} | ||
function jsonChildToJSXChild(jsonChild) { | ||
if (jsonChild === null || typeof jsonChild === 'string') { | ||
return jsonChild; | ||
} else { | ||
var jsxChildren = jsonChildrenToJSXChildren(jsonChild.children); | ||
return { | ||
$$typeof: REACT_ELEMENT_TYPE, | ||
type: jsonChild.type, | ||
key: null, | ||
ref: null, | ||
props: jsxChildren === null ? jsonChild.props : assign({}, jsonChild.props, { | ||
children: jsxChildren | ||
}), | ||
_owner: null, | ||
_store: {} | ||
}; | ||
} | ||
} | ||
function jsonChildrenToJSXChildren(jsonChildren) { | ||
if (jsonChildren !== null) { | ||
if (jsonChildren.length === 1) { | ||
return jsonChildToJSXChild(jsonChildren[0]); | ||
} else if (jsonChildren.length > 1) { | ||
var jsxChildren = []; | ||
var allJSXChildrenAreStrings = true; | ||
var jsxChildrenString = ''; | ||
for (var i = 0; i < jsonChildren.length; i++) { | ||
var jsxChild = jsonChildToJSXChild(jsonChildren[i]); | ||
jsxChildren.push(jsxChild); | ||
if (allJSXChildrenAreStrings) { | ||
if (typeof jsxChild === 'string') { | ||
jsxChildrenString += jsxChild; | ||
} else if (jsxChild !== null) { | ||
allJSXChildrenAreStrings = false; | ||
function createJSXElementForTestComparison(type, props) { | ||
type = { | ||
$$typeof: REACT_ELEMENT_TYPE, | ||
type: type, | ||
key: null, | ||
props: props, | ||
_owner: null, | ||
_store: {} | ||
}; | ||
Object.defineProperty(type, "ref", { enumerable: !1, value: null }); | ||
return type; | ||
} | ||
function jsonChildToJSXChild(jsonChild) { | ||
if (null === jsonChild || "string" === typeof jsonChild) return jsonChild; | ||
var jsxChildren = jsonChildrenToJSXChildren(jsonChild.children); | ||
return createJSXElementForTestComparison( | ||
jsonChild.type, | ||
null === jsxChildren | ||
? jsonChild.props | ||
: assign({}, jsonChild.props, { children: jsxChildren }) | ||
); | ||
} | ||
function jsonChildrenToJSXChildren(jsonChildren) { | ||
if (null !== jsonChildren) { | ||
if (1 === jsonChildren.length) | ||
return jsonChildToJSXChild(jsonChildren[0]); | ||
if (1 < jsonChildren.length) { | ||
for ( | ||
var jsxChildren = [], | ||
allJSXChildrenAreStrings = !0, | ||
jsxChildrenString = "", | ||
i = 0; | ||
i < jsonChildren.length; | ||
i++ | ||
) { | ||
var jsxChild = jsonChildToJSXChild(jsonChildren[i]); | ||
jsxChildren.push(jsxChild); | ||
allJSXChildrenAreStrings && | ||
("string" === typeof jsxChild | ||
? (jsxChildrenString += jsxChild) | ||
: null !== jsxChild && (allJSXChildrenAreStrings = !1)); | ||
} | ||
return allJSXChildrenAreStrings ? jsxChildrenString : jsxChildren; | ||
} | ||
} | ||
return allJSXChildrenAreStrings ? jsxChildrenString : jsxChildren; | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
exports.act = act; | ||
exports.unstable_toMatchRenderedOutput = unstable_toMatchRenderedOutput; | ||
var assign = Object.assign, | ||
REACT_ELEMENT_TYPE = Symbol.for("react.transitional.element"), | ||
REACT_FRAGMENT_TYPE = Symbol.for("react.fragment"), | ||
isArrayImpl = Array.isArray, | ||
assertConsoleLogsCleared = | ||
require("internal-test-utils/consoleMock").assertConsoleLogsCleared; | ||
exports.unstable_toMatchRenderedOutput = function (root, expectedJSX) { | ||
assertYieldsWereCleared(root); | ||
root = root.toJSON(); | ||
if (null === root || "string" === typeof root) var actualJSX = root; | ||
else | ||
isArrayImpl(root) | ||
? 0 === root.length | ||
? (actualJSX = null) | ||
: 1 === root.length | ||
? (actualJSX = jsonChildToJSXChild(root[0])) | ||
: ((root = jsonChildrenToJSXChildren(root)), | ||
(actualJSX = | ||
null === root || "string" === typeof root | ||
? root | ||
: createJSXElementForTestComparison(REACT_FRAGMENT_TYPE, { | ||
children: root | ||
}))) | ||
: (actualJSX = jsonChildToJSXChild(root)); | ||
return captureAssertion(function () { | ||
expect(actualJSX).toEqual(expectedJSX); | ||
}); | ||
}; | ||
})(); | ||
} |
'use strict'; | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./cjs/jest-react.production.min.js'); | ||
module.exports = require('./cjs/jest-react.production.js'); | ||
} else { | ||
module.exports = require('./cjs/jest-react.development.js'); | ||
} |
{ | ||
"name": "jest-react", | ||
"version": "0.0.0-experimental-79c582981-20221021", | ||
"version": "0.0.0-experimental-79d9aed7-20250620", | ||
"description": "Jest matchers and utilities for testing React components.", | ||
@@ -20,7 +20,7 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://reactjs.org/", | ||
"homepage": "https://react.dev/", | ||
"peerDependencies": { | ||
"jest": "^23.0.1 || ^24.0.0 || ^25.1.0", | ||
"react": "0.0.0-experimental-79c582981-20221021", | ||
"react-test-renderer": "0.0.0-experimental-79c582981-20221021" | ||
"jest": "^23.0.1 || ^24.0.0 || ^25.1.0 || ^26.0.0 || ^27.0.0 || ^28.0.0 || ^29.0.0", | ||
"react": "0.0.0-experimental-79d9aed7-20250620", | ||
"react-test-renderer": "0.0.0-experimental-79d9aed7-20250620" | ||
}, | ||
@@ -27,0 +27,0 @@ "files": [ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
1
-50%2
-33.33%9209
-39.47%223
-30.96%1
Infinity%