Run react-dom SSR import test in jsdom-less environment (#13157)
Dan Abramov committed
Jul 6, 2018 at 16:43 UTC
f762b3abb1d951b366f3c8a2354aeedb7fbc32ff
3 files changed
+40
-41
packages/react-dom/src/__tests__/ReactDOM-test.js
-22
@@ -446,26 +446,4 @@ describe('ReactDOM', () => {
446
global.requestAnimationFrame = previousRAF;
447
}
448
});
449
-
450
- // We're just testing importing, not using it.
451
- // It is important because even isomorphic components may import it.
452
- it('can import findDOMNode in Node environment', () => {
453
- const previousRAF = global.requestAnimationFrame;
454
- const previousRIC = global.requestIdleCallback;
455
- const prevWindow = global.window;
456
- try {
457
- global.requestAnimationFrame = undefined;
458
- global.requestIdleCallback = undefined;
459
- // Simulate the Node environment:
460
- delete global.window;
461
- jest.resetModules();
462
- expect(() => {
463
- require('react-dom');
464
- }).not.toThrow();
465
- } finally {
466
- global.requestAnimationFrame = previousRAF;
467
- global.requestIdleCallback = previousRIC;
468
- global.window = prevWindow;
469
- }
470
- });
449
});
packages/react-dom/src/__tests__/ReactServerRendering-test.js
+21
@@ -643,4 +643,25 @@ describe('ReactDOMServer', () => {
643
ReactDOMServer.renderToString(<ClassWithRenderNotExtended />);
644
}).toThrow(TypeError);
645
});
646
+
647
+ // We're just testing importing, not using it.
648
+ // It is important because even isomorphic components may import it.
649
+ it('can import react-dom in Node environment', () => {
650
+ if (
651
+ typeof requestAnimationFrame !== 'undefined' ||
652
+ global.hasOwnProperty('requestAnimationFrame') ||
653
+ typeof requestIdleCallback !== 'undefined' ||
654
+ global.hasOwnProperty('requestIdleCallback') ||
655
+ typeof window !== 'undefined' ||
656
+ global.hasOwnProperty('window')
657
+ ) {
658
+ // Don't remove this. This test is specifically checking
659
+ // what happens when they *don't* exist. It's useless otherwise.
660
+ throw new Error('Expected this test to run in a Node environment.');
661
+ }
662
+ jest.resetModules();
663
+ expect(() => {
664
+ require('react-dom');
665
+ }).not.toThrow();
666
+ });
667
});
scripts/jest/setupEnvironment.js
+19
-19
@@ -7,24 +7,6 @@ if (NODE_ENV !== 'development' && NODE_ENV !== 'production') {
7
global.__DEV__ = NODE_ENV === 'development';
8
global.__PROFILE__ = NODE_ENV === 'development';
9
10
-global.requestAnimationFrame = function(callback) {
11
- setTimeout(callback);
12
-};
13
-
14
-global.requestIdleCallback = function(callback) {
15
- return setTimeout(() => {
16
- callback({
17
- timeRemaining() {
18
- return Infinity;
19
- },
20
- });
21
- });
22
-};
23
-
24
-global.cancelIdleCallback = function(callbackID) {
25
- clearTimeout(callbackID);
26
-};
27
-
10
// By default React console.error()'s any errors, caught or uncaught.
11
// However it is annoying to assert that a warning fired each time
12
// we assert that there is an exception in our tests. This lets us
@@ -34,7 +16,25 @@ global.cancelIdleCallback = function(callbackID) {
16
Error.prototype.suppressReactErrorLogging = true;
17
18
if (typeof window !== 'undefined') {
37
- // Same as above.
19
+ global.requestAnimationFrame = function(callback) {
20
+ setTimeout(callback);
21
+ };
22
+
23
+ global.requestIdleCallback = function(callback) {
24
+ return setTimeout(() => {
25
+ callback({
26
+ timeRemaining() {
27
+ return Infinity;
28
+ },
29
+ });
30
+ });
31
+ };
32
+
33
+ global.cancelIdleCallback = function(callbackID) {
34
+ clearTimeout(callbackID);
35
+ };
36
+
37
+ // Same as we did with Error.prototype above.
38
DOMException.prototype.suppressReactErrorLogging = true;
39
40
// Also prevent JSDOM from logging intentionally thrown errors.