Run react-dom SSR import test in jsdom-less environment (#13157)

This commit is contained in:
Dan Abramov
2018-07-06 16:43:43 +01:00
committed by GitHub
parent 6f6b560a64
commit f762b3abb1
3 changed files with 40 additions and 41 deletions

View File

@@ -446,26 +446,4 @@ describe('ReactDOM', () => {
global.requestAnimationFrame = previousRAF;
}
});
// We're just testing importing, not using it.
// It is important because even isomorphic components may import it.
it('can import findDOMNode in Node environment', () => {
const previousRAF = global.requestAnimationFrame;
const previousRIC = global.requestIdleCallback;
const prevWindow = global.window;
try {
global.requestAnimationFrame = undefined;
global.requestIdleCallback = undefined;
// Simulate the Node environment:
delete global.window;
jest.resetModules();
expect(() => {
require('react-dom');
}).not.toThrow();
} finally {
global.requestAnimationFrame = previousRAF;
global.requestIdleCallback = previousRIC;
global.window = prevWindow;
}
});
});

View File

@@ -643,4 +643,25 @@ describe('ReactDOMServer', () => {
ReactDOMServer.renderToString(<ClassWithRenderNotExtended />);
}).toThrow(TypeError);
});
// We're just testing importing, not using it.
// It is important because even isomorphic components may import it.
it('can import react-dom in Node environment', () => {
if (
typeof requestAnimationFrame !== 'undefined' ||
global.hasOwnProperty('requestAnimationFrame') ||
typeof requestIdleCallback !== 'undefined' ||
global.hasOwnProperty('requestIdleCallback') ||
typeof window !== 'undefined' ||
global.hasOwnProperty('window')
) {
// Don't remove this. This test is specifically checking
// what happens when they *don't* exist. It's useless otherwise.
throw new Error('Expected this test to run in a Node environment.');
}
jest.resetModules();
expect(() => {
require('react-dom');
}).not.toThrow();
});
});

View File

@@ -7,24 +7,6 @@ if (NODE_ENV !== 'development' && NODE_ENV !== 'production') {
global.__DEV__ = NODE_ENV === 'development';
global.__PROFILE__ = NODE_ENV === 'development';
global.requestAnimationFrame = function(callback) {
setTimeout(callback);
};
global.requestIdleCallback = function(callback) {
return setTimeout(() => {
callback({
timeRemaining() {
return Infinity;
},
});
});
};
global.cancelIdleCallback = function(callbackID) {
clearTimeout(callbackID);
};
// By default React console.error()'s any errors, caught or uncaught.
// However it is annoying to assert that a warning fired each time
// we assert that there is an exception in our tests. This lets us
@@ -34,7 +16,25 @@ global.cancelIdleCallback = function(callbackID) {
Error.prototype.suppressReactErrorLogging = true;
if (typeof window !== 'undefined') {
// Same as above.
global.requestAnimationFrame = function(callback) {
setTimeout(callback);
};
global.requestIdleCallback = function(callback) {
return setTimeout(() => {
callback({
timeRemaining() {
return Infinity;
},
});
});
};
global.cancelIdleCallback = function(callbackID) {
clearTimeout(callbackID);
};
// Same as we did with Error.prototype above.
DOMException.prototype.suppressReactErrorLogging = true;
// Also prevent JSDOM from logging intentionally thrown errors.