mirror of
https://github.com/zebrajr/react.git
synced 2025-12-07 12:20:38 +01:00
Instead of createElement. We should have done this when we initially released jsx-runtime but better late than never. The general principle is that our tests should be written using the most up-to-date idioms that we recommend for users, except when explicitly testing an edge case or legacy behavior, like for backwards compatibility. Most of the diff is related to tweaking test output and isn't very interesting. I did have to workaround an issue related to component stacks. The component stack logic depends on shared state that lives in the React module. The problem is that most of our tests reset the React module state and re-require a fresh instance of React, React DOM, etc. However, the JSX runtime is not re-required because it's injected by the compiler as a static import. This means its copy of the shared state is no longer the same as the one used by React, causing any warning logged by the JSX runtime to not include a component stack. (This same issue also breaks string refs, but since we're removing those soon I'm not so concerned about that.) The solution I went with for now is to mock the JSX runtime with a proxy that re-requires the module on every function invocation. I don't love this but it will have to do for now. What we should really do is migrate our tests away from manually resetting the module state and use import syntax instead.
98 lines
3.5 KiB
JavaScript
98 lines
3.5 KiB
JavaScript
'use strict';
|
|
|
|
const semver = require('semver');
|
|
const {ReactVersion} = require('../../../ReactVersions');
|
|
|
|
const {
|
|
DARK_MODE_DIMMED_WARNING_COLOR,
|
|
DARK_MODE_DIMMED_ERROR_COLOR,
|
|
DARK_MODE_DIMMED_LOG_COLOR,
|
|
LIGHT_MODE_DIMMED_WARNING_COLOR,
|
|
LIGHT_MODE_DIMMED_ERROR_COLOR,
|
|
LIGHT_MODE_DIMMED_LOG_COLOR,
|
|
} = require('react-devtools-extensions/utils');
|
|
|
|
// DevTools stores preferences between sessions in localStorage
|
|
if (!global.hasOwnProperty('localStorage')) {
|
|
global.localStorage = require('local-storage-fallback').default;
|
|
}
|
|
|
|
// Mimic the global we set with Webpack's DefinePlugin
|
|
global.__DEV__ = process.env.NODE_ENV !== 'production';
|
|
global.__TEST__ = true;
|
|
|
|
global.process.env.DARK_MODE_DIMMED_WARNING_COLOR =
|
|
DARK_MODE_DIMMED_WARNING_COLOR;
|
|
global.process.env.DARK_MODE_DIMMED_ERROR_COLOR = DARK_MODE_DIMMED_ERROR_COLOR;
|
|
global.process.env.DARK_MODE_DIMMED_LOG_COLOR = DARK_MODE_DIMMED_LOG_COLOR;
|
|
global.process.env.LIGHT_MODE_DIMMED_WARNING_COLOR =
|
|
LIGHT_MODE_DIMMED_WARNING_COLOR;
|
|
global.process.env.LIGHT_MODE_DIMMED_ERROR_COLOR =
|
|
LIGHT_MODE_DIMMED_ERROR_COLOR;
|
|
global.process.env.LIGHT_MODE_DIMMED_LOG_COLOR = LIGHT_MODE_DIMMED_LOG_COLOR;
|
|
|
|
global._test_react_version = (range, testName, callback) => {
|
|
const reactVersion = process.env.REACT_VERSION || ReactVersion;
|
|
const shouldPass = semver.satisfies(reactVersion, range);
|
|
|
|
if (shouldPass) {
|
|
test(testName, callback);
|
|
} else {
|
|
test.skip(testName, callback);
|
|
}
|
|
};
|
|
|
|
global._test_react_version_focus = (range, testName, callback) => {
|
|
const reactVersion = process.env.REACT_VERSION || ReactVersion;
|
|
const shouldPass = semver.satisfies(reactVersion, range);
|
|
|
|
if (shouldPass) {
|
|
// eslint-disable-next-line jest/no-focused-tests
|
|
test.only(testName, callback);
|
|
} else {
|
|
test.skip(testName, callback);
|
|
}
|
|
};
|
|
|
|
global._test_ignore_for_react_version = (testName, callback) => {
|
|
test.skip(testName, callback);
|
|
};
|
|
|
|
// Most of our tests call jest.resetModules in a beforeEach and the
|
|
// re-require all the React modules. However, the JSX runtime is injected by
|
|
// the compiler, so those bindings don't get updated. This causes warnings
|
|
// logged by the JSX runtime to not have a component stack, because component
|
|
// stack relies on the the secret internals object that lives on the React
|
|
// module, which because of the resetModules call is longer the same one.
|
|
//
|
|
// To workaround this issue, we use a proxy that re-requires the latest
|
|
// JSX Runtime from the require cache on every function invocation.
|
|
//
|
|
// Longer term we should migrate all our tests away from using require() and
|
|
// resetModules, and use import syntax instead so this kind of thing doesn't
|
|
// happen.
|
|
lazyRequireFunctionExports('react/jsx-dev-runtime');
|
|
|
|
// TODO: We shouldn't need to do this in the production runtime, but until
|
|
// we remove string refs they also depend on the shared state object. Remove
|
|
// once we remove string refs.
|
|
lazyRequireFunctionExports('react/jsx-runtime');
|
|
|
|
function lazyRequireFunctionExports(moduleName) {
|
|
jest.mock(moduleName, () => {
|
|
return new Proxy(jest.requireActual(moduleName), {
|
|
get(originalModule, prop) {
|
|
// If this export is a function, return a wrapper function that lazily
|
|
// requires the implementation from the current module cache.
|
|
if (typeof originalModule[prop] === 'function') {
|
|
return function () {
|
|
return jest.requireActual(moduleName)[prop].apply(this, arguments);
|
|
};
|
|
} else {
|
|
return originalModule[prop];
|
|
}
|
|
},
|
|
});
|
|
});
|
|
}
|