react/scripts/jest/setupHostConfigs.js
Sebastian Markbåge 1d25aa5787
[Fizz] New Server Rendering Infra (#14144)
* [Fizz] Add Flow/Jest/Rollup build infra

Add a new package for react-stream which allows for custom server renderer
outputs. I picked the name because it's a reasonable name but also
because the npm name is currently owned by a friend of the project.

The react-dom build has its own inlined server renderer under the
name `react-dom/fizz`.

There is also a noop renderer to be used for testing. At some point
we might add a public one to test-renderer but for now I don't want to have
to think about public API design for the tests.

* Add FormatConfig too

We need to separate the format (DOM, React Native, etc) from the host
running the server (Node, Browser, etc).

* Basic wiring between Node, Noop and DOM configs

The Node DOM API is pipeToNodeStream which accepts a writable stream.

* Merge host and format config in dynamic react-stream entry point

Simpler API this way but also avoids having to fork the wrapper config.

Fixes noop builds.

* Add setImmediate/Buffer globals to lint config

Used by the server renderer

* Properly include fizz.node.js

Also use forwarding to it from fizz.js in builds so that tests covers
this.

* Make react-stream private since we're not ready to publish

or even name it yet

* Rename Renderer -> Streamer

* Prefix react-dom/fizz with react-dom/unstable-fizz

* Add Fizz Browser host config

This lets Fizz render to WHATWG streams. E.g. for rendering in a
Service Worker.

I added react-dom/unstable-fizz.browser as the entry point for this.

Since we now have two configurations of DOM. I had to add another
inlinedHostConfigs configuration called `dom-browser`. The reconciler
treats this configuration the same as `dom`. For stream it checks
against the ReactFizzHostConfigBrowser instead of the Node one.

* Add Fizz Browser Fixture

This is for testing server rendering - on the client.

* Lower version number to detach it from react-reconciler version
2018-11-30 11:38:22 -08:00

114 lines
4.1 KiB
JavaScript

'use strict';
const inlinedHostConfigs = require('../shared/inlinedHostConfigs');
// When testing the custom renderer code path through `react-reconciler`,
// turn the export into a function, and use the argument as host config.
const shimHostConfigPath = 'react-reconciler/src/ReactFiberHostConfig';
jest.mock('react-reconciler', () => {
return config => {
jest.mock(shimHostConfigPath, () => config);
return require.requireActual('react-reconciler');
};
});
jest.mock('react-reconciler/persistent', () => {
return config => {
jest.mock(shimHostConfigPath, () => config);
return require.requireActual('react-reconciler/persistent');
};
});
const shimFizzHostConfigPath = 'react-stream/src/ReactFizzHostConfig';
const shimFizzFormatConfigPath = 'react-stream/src/ReactFizzFormatConfig';
jest.mock('react-stream', () => {
return config => {
jest.mock(shimFizzHostConfigPath, () => config);
jest.mock(shimFizzFormatConfigPath, () => config);
return require.requireActual('react-stream');
};
});
// But for inlined host configs (such as React DOM, Native, etc), we
// mock their named entry points to establish a host config mapping.
inlinedHostConfigs.forEach(rendererInfo => {
if (rendererInfo.shortName === 'custom') {
// There is no inline entry point for the custom renderers.
// Instead, it's handled by the generic `react-reconciler` entry point above.
return;
}
jest.mock(`react-reconciler/inline.${rendererInfo.shortName}`, () => {
let hasImportedShimmedConfig = false;
// We want the reconciler to pick up the host config for this renderer.
jest.mock(shimHostConfigPath, () => {
hasImportedShimmedConfig = true;
return require.requireActual(
`react-reconciler/src/forks/ReactFiberHostConfig.${
rendererInfo.shortName
}.js`
);
});
const renderer = require.requireActual('react-reconciler');
// If the shimmed config factory function above has not run,
// it means this test file loads more than one renderer
// but doesn't reset modules between them. This won't work.
if (!hasImportedShimmedConfig) {
throw new Error(
`Could not import the "${rendererInfo.shortName}" renderer ` +
`in this suite because another renderer has already been ` +
`loaded earlier. Call jest.resetModules() before importing any ` +
`of the following entry points:\n\n` +
rendererInfo.entryPoints.map(entry => ` * ${entry}`)
);
}
return renderer;
});
if (rendererInfo.isFizzSupported) {
jest.mock(`react-stream/inline.${rendererInfo.shortName}`, () => {
let hasImportedShimmedConfig = false;
// We want the renderer to pick up the host config for this renderer.
jest.mock(shimFizzHostConfigPath, () => {
hasImportedShimmedConfig = true;
return require.requireActual(
`react-stream/src/forks/ReactFizzHostConfig.${
rendererInfo.shortName
}.js`
);
});
jest.mock(shimFizzFormatConfigPath, () => {
hasImportedShimmedConfig = true;
return require.requireActual(
`react-stream/src/forks/ReactFizzFormatConfig.${
rendererInfo.shortName
}.js`
);
});
const renderer = require.requireActual('react-stream');
// If the shimmed config factory function above has not run,
// it means this test file loads more than one renderer
// but doesn't reset modules between them. This won't work.
if (!hasImportedShimmedConfig) {
throw new Error(
`Could not import the "${rendererInfo.shortName}" renderer ` +
`in this suite because another renderer has already been ` +
`loaded earlier. Call jest.resetModules() before importing any ` +
`of the following entry points:\n\n` +
rendererInfo.entryPoints.map(entry => ` * ${entry}`)
);
}
return renderer;
});
}
});
// Make it possible to import this module inside
// the React package itself.
jest.mock('shared/ReactSharedInternals', () =>
require.requireActual('react/src/ReactSharedInternals')
);