@samitouri / QOS-React-1 / commits / 13f35433bc

Mock modules in tests using `Module` (#28678)

## Summary Fixes a type validation error introduced in newer versions of Node.js when calling `Module.prototype._compile` in our unit tests. (I tried but have yet to pinpoint the precise change in Node.js that introduced this vaildation.) The specific error that currently occurs when running unit tests with Node.js v18.16.1: ``` TypeError: The "mod" argument must be an instance of Module. Received an instance of Object 80 | 81 | if (useServer) { > 82 | originalCompile.apply(this, arguments); | ^ 83 | 84 | const moduleId: string = (url.pathToFileURL(filename).href: any); 85 | ``` This fixes the type validation error by mocking modules using `new Module()` instead of plain objects. ## How did you test this change? Ran the unit tests successfully: ``` $ node --version v18.16.1 $ yarn test ```

Timothy Yung committed Mar 29, 2024 at 13:19 UTC 13f35433bca9fc08fc7c4ab001f39c982cbde051
2 files changed +8 -6
packages/react-server-dom-turbopack/src/__tests__/utils/TurbopackMock.js
+4 -3
@@ -55,7 +55,7 @@ exports.clientModuleError = function clientModuleError(moduleError) {
55 chunks: [],
56 name: '*',
57 };
58 - const mod = {exports: {}};
58 + const mod = new Module();
59 nodeCompile.call(mod, '"use client"', idx);
60 return mod.exports;
61 };
@@ -107,7 +107,7 @@ exports.clientExports = function clientExports(moduleExports, chunkUrl) {
107 name: 's',
108 };
109 }
110 - const mod = {exports: {}};
110 + const mod = new Module();
111 nodeCompile.call(mod, '"use client"', idx);
112 return mod.exports;
113 };
@@ -142,7 +142,8 @@ exports.serverExports = function serverExports(moduleExports) {
142 name: 's',
143 };
144 }
145 - const mod = {exports: moduleExports};
145 + const mod = new Module();
146 + mod.exports = moduleExports;
147 nodeCompile.call(mod, '"use server"', idx);
148 return mod.exports;
149 };
packages/react-server-dom-webpack/src/__tests__/utils/WebpackMock.js
+4 -3
@@ -55,7 +55,7 @@ exports.clientModuleError = function clientModuleError(moduleError) {
55 chunks: [],
56 name: '*',
57 };
58 - const mod = {exports: {}};
58 + const mod = new Module();
59 nodeCompile.call(mod, '"use client"', idx);
60 return mod.exports;
61 };
@@ -111,7 +111,7 @@ exports.clientExports = function clientExports(
111 name: 's',
112 };
113 }
114 - const mod = {exports: {}};
114 + const mod = new Module();
115 nodeCompile.call(mod, '"use client"', idx);
116 return mod.exports;
117 };
@@ -146,7 +146,8 @@ exports.serverExports = function serverExports(moduleExports) {
146 name: 's',
147 };
148 }
149 - const mod = {exports: moduleExports};
149 + const mod = new Module();
150 + mod.exports = moduleExports;
151 nodeCompile.call(mod, '"use server"', idx);
152 return mod.exports;
153 };