1. 'use strict';
    
  2. 
    
  3. const JestReact = require('jest-react');
    
  4. 
    
  5. // TODO: Move to ReactInternalTestUtils
    
  6. 
    
  7. function captureAssertion(fn) {
    
  8.   // Trick to use a Jest matcher inside another Jest matcher. `fn` contains an
    
  9.   // assertion; if it throws, we capture the error and return it, so the stack
    
  10.   // trace presented to the user points to the original assertion in the
    
  11.   // test file.
    
  12.   try {
    
  13.     fn();
    
  14.   } catch (error) {
    
  15.     return {
    
  16.       pass: false,
    
  17.       message: () => error.message,
    
  18.     };
    
  19.   }
    
  20.   return {pass: true};
    
  21. }
    
  22. 
    
  23. function assertYieldsWereCleared(Scheduler, caller) {
    
  24.   const actualYields = Scheduler.unstable_clearLog();
    
  25.   if (actualYields.length !== 0) {
    
  26.     const error = Error(
    
  27.       'The event log is not empty. Call assertLog(...) first.'
    
  28.     );
    
  29.     Error.captureStackTrace(error, caller);
    
  30.     throw error;
    
  31.   }
    
  32. }
    
  33. 
    
  34. function toMatchRenderedOutput(ReactNoop, expectedJSX) {
    
  35.   if (typeof ReactNoop.getChildrenAsJSX === 'function') {
    
  36.     const Scheduler = ReactNoop._Scheduler;
    
  37.     assertYieldsWereCleared(Scheduler, toMatchRenderedOutput);
    
  38.     return captureAssertion(() => {
    
  39.       expect(ReactNoop.getChildrenAsJSX()).toEqual(expectedJSX);
    
  40.     });
    
  41.   }
    
  42.   return JestReact.unstable_toMatchRenderedOutput(ReactNoop, expectedJSX);
    
  43. }
    
  44. 
    
  45. module.exports = {
    
  46.   toMatchRenderedOutput,
    
  47. };