Skip to content

Commit 97e8ddb

Browse files
committed
Use require() to implement script src in tests
We currently use rollup to make an adhoc bundle from the file system when we're testing an import of an external file. This doesn't follow all the interception rules that we use in jest and in our actual builds. This switches to just using jest require() to load these. This means that they effectively have to load into the global document so this only works with global document tests which is all we have now anyway.
1 parent 5e5342b commit 97e8ddb

File tree

4 files changed

+24
-71
lines changed

4 files changed

+24
-71
lines changed

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ describe('ReactDOMFizzServer', () => {
6464
});
6565
streamingContainer = null;
6666
global.window = jsdom.window;
67-
global.document = jsdom.window.document;
67+
global.document = global.window.document;
68+
global.navigator = global.window.navigator;
69+
global.Node = global.window.Node;
70+
global.addEventListener = global.window.addEventListener;
71+
global.MutationObserver = global.window.MutationObserver;
6872
container = document.getElementById('container');
6973

7074
Scheduler = require('scheduler');

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ describe('ReactDOMFloat', () => {
5959
});
6060
streamingContainer = null;
6161
global.window = jsdom.window;
62-
global.document = jsdom.window.document;
62+
global.document = global.window.document;
63+
global.navigator = global.window.navigator;
64+
global.Node = global.window.Node;
65+
global.addEventListener = global.window.addEventListener;
66+
global.MutationObserver = global.window.MutationObserver;
6367
container = document.getElementById('container');
6468

6569
React = require('react');
@@ -95,7 +99,7 @@ describe('ReactDOMFloat', () => {
9599
renderOptions = {};
96100
if (gate(flags => flags.enableFizzExternalRuntime)) {
97101
renderOptions.unstable_externalRuntimeSrc =
98-
'react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js';
102+
'react-dom/unstable_server-external-runtime';
99103
}
100104
});
101105

packages/react-dom/src/test-utils/FizzTestUtils.js

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,6 @@
88
*/
99
'use strict';
1010

11-
import * as tmp from 'tmp';
12-
import * as fs from 'fs';
13-
import replace from '@rollup/plugin-replace';
14-
import resolve from '@rollup/plugin-node-resolve';
15-
import {rollup} from 'rollup';
16-
import path from 'path';
17-
18-
const rollupCache: Map<string, string | null> = new Map();
19-
20-
// Utility function to read and bundle a standalone browser script
21-
async function getRollupResult(scriptSrc: string): Promise<string | null> {
22-
const cachedResult = rollupCache.get(scriptSrc);
23-
if (cachedResult !== undefined) {
24-
return cachedResult;
25-
}
26-
let tmpFile;
27-
try {
28-
tmpFile = tmp.fileSync();
29-
const rollupConfig = {
30-
input: require.resolve(scriptSrc),
31-
onwarn: console.warn,
32-
plugins: [
33-
replace({__DEV__: 'true', preventAssignment: true}),
34-
resolve({
35-
rootDir: path.join(__dirname, '..', '..', '..'),
36-
}),
37-
],
38-
output: {
39-
externalLiveBindings: false,
40-
freeze: false,
41-
interop: false,
42-
esModule: false,
43-
},
44-
};
45-
const outputConfig = {
46-
file: tmpFile.name,
47-
format: 'iife',
48-
};
49-
const bundle = await rollup(rollupConfig);
50-
await bundle.write(outputConfig);
51-
const bundleBuffer = Buffer.alloc(4096);
52-
let bundleStr = '';
53-
while (true) {
54-
// $FlowFixMe[incompatible-call]
55-
const bytes = fs.readSync(tmpFile.fd, bundleBuffer);
56-
if (bytes <= 0) {
57-
break;
58-
}
59-
bundleStr += bundleBuffer.slice(0, bytes).toString();
60-
}
61-
rollupCache.set(scriptSrc, bundleStr);
62-
return bundleStr;
63-
} catch (e) {
64-
rollupCache.set(scriptSrc, null);
65-
return null;
66-
} finally {
67-
if (tmpFile) {
68-
tmpFile.removeCallback();
69-
}
70-
}
71-
}
72-
7311
async function insertNodesAndExecuteScripts(
7412
source: Document | Element,
7513
target: Node,
@@ -150,12 +88,17 @@ async function executeScript(script: Element) {
15088
const parent = script.parentNode;
15189
const scriptSrc = script.getAttribute('src');
15290
if (scriptSrc) {
153-
const rollupOutput = await getRollupResult(scriptSrc);
154-
if (rollupOutput) {
155-
const transientScript = ownerDocument.createElement('script');
156-
transientScript.textContent = rollupOutput;
157-
parent.appendChild(transientScript);
158-
parent.removeChild(transientScript);
91+
if (document !== ownerDocument) {
92+
throw new Error(
93+
'You must set the current document to the global document to use script src in tests',
94+
);
95+
}
96+
// $FlowFixMe
97+
try {
98+
require(scriptSrc);
99+
} catch (x) {
100+
const event = new window.ErrorEvent('error', {error: x});
101+
window.dispatchEvent(event);
159102
}
160103
} else {
161104
const newScript = ownerDocument.createElement('script');

scripts/jest/ReactDOMServerIntegrationEnvironment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ReactDOMServerIntegrationEnvironment extends NodeEnvironment {
1616
this.global.document = this.global.window.document;
1717
this.global.navigator = this.global.window.navigator;
1818
this.global.Node = this.global.window.Node;
19+
this.global.addEventListener = this.global.window.addEventListener;
20+
this.global.MutationObserver = this.global.window.MutationObserver;
1921
}
2022

2123
async setup() {

0 commit comments

Comments
 (0)