mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
1528c5ccdf
(This only affects our own internal repo; it's not a public API.) I think most of us agree this is a less confusing name. It's possible someone will confuse it with `console.log`. If that becomes a problem we can warn in dev or something.
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
const JestReact = require('jest-react');
|
|
|
|
// TODO: Move to ReactInternalTestUtils
|
|
|
|
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: () => error.message,
|
|
};
|
|
}
|
|
return {pass: true};
|
|
}
|
|
|
|
function assertYieldsWereCleared(Scheduler) {
|
|
const actualYields = Scheduler.unstable_clearLog();
|
|
if (actualYields.length !== 0) {
|
|
const error = Error(
|
|
'The event log is not empty. Call assertLog(...) first.'
|
|
);
|
|
Error.captureStackTrace(error, assertYieldsWereCleared);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
function toMatchRenderedOutput(ReactNoop, expectedJSX) {
|
|
if (typeof ReactNoop.getChildrenAsJSX === 'function') {
|
|
const Scheduler = ReactNoop._Scheduler;
|
|
assertYieldsWereCleared(Scheduler);
|
|
return captureAssertion(() => {
|
|
expect(ReactNoop.getChildrenAsJSX()).toEqual(expectedJSX);
|
|
});
|
|
}
|
|
return JestReact.unstable_toMatchRenderedOutput(ReactNoop, expectedJSX);
|
|
}
|
|
|
|
module.exports = {
|
|
toMatchRenderedOutput,
|
|
};
|