@samitouri / QOS-React / commits / 2c59713f71

Remove invokeGuardedCallbackProd

We weren't using it anywhere except for the test suite. Instead, we can change the environment flag and run the tests in both environments.

Andrew Clark committed Feb 24, 2017 at 16:19 UTC 2c59713f71c495a34679d61fdae949cbfb6a4f9c
3 files changed +62 -91
scripts/fiber/tests-passing.txt
+14 -12
@@ -1667,18 +1667,20 @@ src/renderers/shared/stack/reconciler/__tests__/Transaction-test.js
1667 * should allow nesting of transactions
1668
1669 src/renderers/shared/utils/__tests__/ReactErrorUtils-test.js
1670 -* should call the callback the passed arguments
1671 -* should call the callback with the provided context
1672 -* should return a caught error
1673 -* should return null if no error is thrown
1674 -* should rethrow caught errors
1675 -* should call the callback the passed arguments
1676 -* should call the callback with the provided context
1677 -* should return a caught error
1678 -* should return null if no error is thrown
1679 -* can nest with same debug name
1680 -* does not return nested errors
1681 -* should use invokeGuardedCallbackProd in production
1670 +* it should rethrow errors caught by invokeGuardedCallbackAndCatchFirstError (development)
1671 +* should call the callback the passed arguments (development)
1672 +* should call the callback with the provided context (development)
1673 +* should return a caught error (development)
1674 +* should return null if no error is thrown (development)
1675 +* can nest with same debug name (development)
1676 +* does not return nested errors (development)
1677 +* it should rethrow errors caught by invokeGuardedCallbackAndCatchFirstError (production)
1678 +* should call the callback the passed arguments (production)
1679 +* should call the callback with the provided context (production)
1680 +* should return a caught error (production)
1681 +* should return null if no error is thrown (production)
1682 +* can nest with same debug name (production)
1683 +* does not return nested errors (production)
1684
1685 src/renderers/shared/utils/__tests__/accumulateInto-test.js
1686 * throws if the second item is null
src/renderers/shared/utils/ReactErrorUtils.js
+23 -26
@@ -1,4 +1,4 @@
1 -/**
1 + /**
2 * Copyright 2013-present, Facebook, Inc.
3 * All rights reserved.
4 *
@@ -12,7 +12,7 @@
12
13 'use strict';
14
15 -var caughtError = null;
15 +let caughtError = null;
16
17 /**
18 * Call a function while guarding against errors that happens within it.
@@ -23,29 +23,26 @@ var caughtError = null;
23 * @param {*} context The context to use when calling the function
24 * @param {...*} args Arguments for function
25 */
26 -function invokeGuardedCallback<A, B, C, D, E, F, Context>(
27 - name: string | null,
28 - func: (A, B, C, D, E, F) => void,
29 - context: Context,
30 - a: A,
31 - b: B,
32 - c: C,
33 - d: D,
34 - e: E,
35 - f: F,
36 -): Error | null {
37 - const funcArgs = Array.prototype.slice.call(arguments, 3);
38 - try {
39 - func.apply(context, funcArgs);
40 - } catch (error) {
41 - return error;
42 - }
43 - return null;
44 -}
45 -
46 -var ReactErrorUtils = {
47 - invokeGuardedCallback,
48 - invokeGuardedCallbackProd: invokeGuardedCallback,
26 +const ReactErrorUtils = {
27 + invokeGuardedCallback: function<A, B, C, D, E, F, Context>(
28 + name: string | null,
29 + func: (A, B, C, D, E, F) => void,
30 + context: Context,
31 + a: A,
32 + b: B,
33 + c: C,
34 + d: D,
35 + e: E,
36 + f: F,
37 + ): Error | null {
38 + const funcArgs = Array.prototype.slice.call(arguments, 3);
39 + try {
40 + func.apply(context, funcArgs);
41 + } catch (error) {
42 + return error;
43 + }
44 + return null;
45 + },
46
47 /**
48 * Same as invokeGuardedCallback, but instead of returning an error, it stores
@@ -79,7 +76,7 @@ var ReactErrorUtils = {
76 */
77 rethrowCaughtError: function() {
78 if (caughtError) {
82 - var error = caughtError;
79 + const error = caughtError;
80 caughtError = null;
81 throw error;
82 }
src/renderers/shared/utils/__tests__/ReactErrorUtils-test.js
+25 -53
@@ -19,38 +19,30 @@ describe('ReactErrorUtils', () => {
19 ReactErrorUtils = require('ReactErrorUtils');
20 });
21
22 - describe('invokeGuardedCallbackProd', () => {
23 - it('should call the callback the passed arguments', () => {
24 - var callback = jest.fn();
25 - ReactErrorUtils.invokeGuardedCallbackProd('foo', callback, null, 'arg1', 'arg2');
26 - expect(callback).toBeCalledWith('arg1', 'arg2');
27 - });
28 -
29 - it('should call the callback with the provided context', () => {
30 - var context = { didCall: false };
31 - ReactErrorUtils.invokeGuardedCallbackProd('foo', function() {
32 - this.didCall = true;
33 - }, context);
34 - expect(context.didCall).toBe(true);
22 + // Run tests in both DEV and production
23 + describe('invokeGuardedCallback (development)', invokeGuardedCallbackTests.bind(null, 'development'));
24 + describe('invokeGuardedCallback (production)', () => {
25 + let oldProcess;
26 + beforeEach(() => {
27 + __DEV__ = false;
28 + oldProcess = process;
29 + global.process = {
30 + env: Object.assign({}, process.env, {NODE_ENV: 'production'}),
31 + };
32 + jest.resetModules();
33 + ReactErrorUtils = require('ReactErrorUtils');
34 });
35
37 - it('should return a caught error', () => {
38 - const error = new Error();
39 - const returnValue = ReactErrorUtils.invokeGuardedCallbackProd('foo', function() {
40 - throw error;
41 - }, null, 'arg1', 'arg2');
42 - expect(returnValue).toBe(error);
36 + afterEach(() => {
37 + __DEV__ = true;
38 + global.process = oldProcess;
39 });
40
45 - it('should return null if no error is thrown', () => {
46 - var callback = jest.fn();
47 - const returnValue = ReactErrorUtils.invokeGuardedCallbackProd('foo', callback, null);
48 - expect(returnValue).toBe(null);
49 - });
41 + invokeGuardedCallbackTests('production');
42 });
43
52 - describe('invokeGuardedCallbackAndCatchFirstError', () => {
53 - it('should rethrow caught errors', () => {
44 + function invokeGuardedCallbackTests(environment) {
45 + it(`it should rethrow errors caught by invokeGuardedCallbackAndCatchFirstError (${environment})`, () => {
46 var err = new Error('foo');
47 var callback = function() {
48 throw err;
@@ -58,16 +50,14 @@ describe('ReactErrorUtils', () => {
50 ReactErrorUtils.invokeGuardedCallbackAndCatchFirstError('foo', callback, null);
51 expect(() => ReactErrorUtils.rethrowCaughtError()).toThrow(err);
52 });
61 - });
53
63 - describe('invokeGuardedCallback', () => {
64 - it('should call the callback the passed arguments', () => {
54 + it(`should call the callback the passed arguments (${environment})`, () => {
55 var callback = jest.fn();
56 ReactErrorUtils.invokeGuardedCallback('foo', callback, null, 'arg1', 'arg2');
57 expect(callback).toBeCalledWith('arg1', 'arg2');
58 });
59
70 - it('should call the callback with the provided context', () => {
60 + it(`should call the callback with the provided context (${environment})`, () => {
61 var context = { didCall: false };
62 ReactErrorUtils.invokeGuardedCallback('foo', function() {
63 this.didCall = true;
@@ -75,7 +65,7 @@ describe('ReactErrorUtils', () => {
65 expect(context.didCall).toBe(true);
66 });
67
78 - it('should return a caught error', () => {
68 + it(`should return a caught error (${environment})`, () => {
69 const error = new Error();
70 const returnValue = ReactErrorUtils.invokeGuardedCallback('foo', function() {
71 throw error;
@@ -83,13 +73,13 @@ describe('ReactErrorUtils', () => {
73 expect(returnValue).toBe(error);
74 });
75
86 - it('should return null if no error is thrown', () => {
76 + it(`should return null if no error is thrown (${environment})`, () => {
77 var callback = jest.fn();
78 const returnValue = ReactErrorUtils.invokeGuardedCallback('foo', callback, null);
79 expect(returnValue).toBe(null);
80 });
81
92 - it('can nest with same debug name', () => {
82 + it(`can nest with same debug name (${environment})`, () => {
83 const err1 = new Error();
84 let err2;
85 const err3 = new Error();
@@ -104,7 +94,7 @@ describe('ReactErrorUtils', () => {
94 expect(err4).toBe(err3);
95 });
96
107 - it('does not return nested errors', () => {
97 + it(`does not return nested errors (${environment})`, () => {
98 const err1 = new Error();
99 let err2;
100 const err3 = ReactErrorUtils.invokeGuardedCallback('foo', function() {
@@ -116,23 +106,5 @@ describe('ReactErrorUtils', () => {
106 expect(err3).toBe(null); // Returns null because inner error was already captured
107 expect(err2).toBe(err1);
108 });
119 -
120 - it('should use invokeGuardedCallbackProd in production', () => {
121 - expect(ReactErrorUtils.invokeGuardedCallback).not.toEqual(
122 - ReactErrorUtils.invokeGuardedCallbackProd
123 - );
124 - __DEV__ = false;
125 - var oldProcess = process;
126 - global.process = {
127 - env: Object.assign({}, process.env, {NODE_ENV: 'production'}),
128 - };
129 - jest.resetModules();
130 - ReactErrorUtils = require('ReactErrorUtils');
131 - expect(ReactErrorUtils.invokeGuardedCallback).toEqual(
132 - ReactErrorUtils.invokeGuardedCallbackProd
133 - );
134 - __DEV__ = true;
135 - global.process = oldProcess;
136 - });
137 - });
109 + }
110 });