Skip to content

Commit cc9d64c

Browse files
nhunzakersophiebits
authored andcommitted
Use standard method of testing for production
1 parent e0c864d commit cc9d64c

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/renderers/shared/stack/reconciler/__tests__/refs-test.js

+29-20
Original file line numberDiff line numberDiff line change
@@ -319,43 +319,52 @@ describe('creating element with ref in constructor', () => {
319319
'for the full message or use the non-minified dev environment for full errors and additional ' +
320320
'helpful warnings.';
321321

322-
it('throws an error when __DEV__ = true', () => {
323-
ReactTestUtils = require('ReactTestUtils');
322+
describe('when in development', () => {
323+
it('throws an error', () => {
324+
ReactTestUtils = require('ReactTestUtils');
324325

325-
var originalDev = __DEV__;
326-
__DEV__ = true;
327-
328-
try {
329326
expect(function() {
330327
ReactTestUtils.renderIntoDocument(<RefTest />);
331328
}).toThrowError(
332329
ReactDOMFeatureFlags.useFiber ? fiberDevErrorMessage : devErrorMessage,
333330
);
334-
} finally {
335-
__DEV__ = originalDev;
336-
}
331+
});
337332
});
338333

339-
it('throws an error when __DEV__ = false', () => {
340-
ReactTestUtils = require('ReactTestUtils');
334+
describe('when in production', () => {
335+
var oldProcess;
341336

342-
var originalDev = __DEV__;
343-
var originalEnv = process.env.NODE_ENV;
337+
beforeEach(() => {
338+
__DEV__ = false;
344339

345-
__DEV__ = false;
346-
process.env.NODE_ENV = 'production';
340+
// Mutating process.env.NODE_ENV would cause our babel plugins to do the
341+
// wrong thing. If you change this, make sure to test with jest --no-cache.
342+
oldProcess = process;
343+
global.process = {
344+
...process,
345+
env: {...process.env, NODE_ENV: 'production'},
346+
};
347+
348+
jest.resetModules();
349+
React = require('React');
350+
ReactTestUtils = require('ReactTestUtils');
351+
ReactDOMFeatureFlags = require('ReactDOMFeatureFlags');
352+
reactComponentExpect = require('reactComponentExpect');
353+
});
347354

348-
try {
355+
afterEach(() => {
356+
__DEV__ = true;
357+
global.process = oldProcess;
358+
});
359+
360+
it('throws an error', () => {
349361
expect(function() {
350362
ReactTestUtils.renderIntoDocument(<RefTest />);
351363
}).toThrowError(
352364
ReactDOMFeatureFlags.useFiber
353365
? fiberProdErrorMessage
354366
: prodErrorMessage,
355367
);
356-
} finally {
357-
__DEV__ = originalDev;
358-
process.env.NODE_ENV = originalEnv;
359-
}
368+
});
360369
});
361370
});

0 commit comments

Comments
 (0)