forked from react/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupHostConfigs.js
More file actions
189 lines (169 loc) · 7.25 KB
/
Copy pathsetupHostConfigs.js
File metadata and controls
189 lines (169 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'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 shimServerHostConfigPath = 'react-server/src/ReactServerHostConfig';
const shimServerFormatConfigPath = 'react-server/src/ReactServerFormatConfig';
jest.mock('react-server', () => {
return config => {
jest.mock(shimServerHostConfigPath, () => config);
jest.mock(shimServerFormatConfigPath, () => config);
return require.requireActual('react-server');
};
});
jest.mock('react-server/flight', () => {
return config => {
jest.mock(shimServerHostConfigPath, () => config);
jest.mock(shimServerFormatConfigPath, () => config);
return require.requireActual('react-server/flight');
};
});
const shimFlightClientHostConfigPath =
'react-flight/src/ReactFlightClientHostConfig';
jest.mock('react-flight', () => {
return config => {
jest.mock(shimFlightClientHostConfigPath, () => config);
return require.requireActual('react-flight');
};
});
// 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.isServerSupported) {
jest.mock(`react-server/inline.${rendererInfo.shortName}`, () => {
let hasImportedShimmedConfig = false;
// We want the renderer to pick up the host config for this renderer.
jest.mock(shimServerHostConfigPath, () => {
hasImportedShimmedConfig = true;
return require.requireActual(
`react-server/src/forks/ReactServerHostConfig.${rendererInfo.shortName}.js`
);
});
jest.mock(shimServerFormatConfigPath, () => {
hasImportedShimmedConfig = true;
return require.requireActual(
`react-server/src/forks/ReactServerFormatConfig.${rendererInfo.shortName}.js`
);
});
const renderer = require.requireActual('react-server');
// 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;
});
jest.mock(`react-server/flight.inline.${rendererInfo.shortName}`, () => {
let hasImportedShimmedConfig = false;
// We want the renderer to pick up the host config for this renderer.
jest.mock(shimServerHostConfigPath, () => {
hasImportedShimmedConfig = true;
return require.requireActual(
`react-server/src/forks/ReactServerHostConfig.${rendererInfo.shortName}.js`
);
});
jest.mock(shimServerFormatConfigPath, () => {
hasImportedShimmedConfig = true;
return require.requireActual(
`react-server/src/forks/ReactServerFormatConfig.${rendererInfo.shortName}.js`
);
});
const renderer = require.requireActual('react-server/flight');
// 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;
});
jest.mock(`react-flight/inline.${rendererInfo.shortName}`, () => {
let hasImportedShimmedConfig = false;
// We want the renderer to pick up the host config for this renderer.
jest.mock(shimFlightClientHostConfigPath, () => {
hasImportedShimmedConfig = true;
return require.requireActual(
`react-flight/src/forks/ReactFlightClientHostConfig.${rendererInfo.shortName}.js`
);
});
const renderer = require.requireActual('react-flight');
// 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')
);
jest.mock('scheduler', () => require.requireActual('scheduler/unstable_mock'));
jest.mock('scheduler/src/SchedulerHostConfig', () =>
require.requireActual('scheduler/src/forks/SchedulerHostConfig.mock.js')
);