main
js 48 lines 1.36 KB
Raw
1 'use strict';
2
3 const JestReact = require('jest-react');
4 const {assertConsoleLogsCleared} = require('internal-test-utils/consoleMock');
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 assertConsoleLogsCleared();
33 }
34
35 function toMatchRenderedOutput(ReactNoop, expectedJSX) {
36 if (typeof ReactNoop.getChildrenAsJSX === 'function') {
37 const Scheduler = ReactNoop._Scheduler;
38 assertYieldsWereCleared(Scheduler, toMatchRenderedOutput);
39 return captureAssertion(() => {
40 expect(ReactNoop.getChildrenAsJSX()).toEqual(expectedJSX);
41 });
42 }
43 return JestReact.unstable_toMatchRenderedOutput(ReactNoop, expectedJSX);
44 }
45
46 module.exports = {
47 toMatchRenderedOutput,
48 };