From 13f35433bca9fc08fc7c4ab001f39c982cbde051 Mon Sep 17 00:00:00 2001 From: Timothy Yung Date: Fri, 29 Mar 2024 13:19:54 -0700 Subject: [PATCH] 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 ``` --- .../src/__tests__/utils/TurbopackMock.js | 7 ++++--- .../src/__tests__/utils/WebpackMock.js | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/react-server-dom-turbopack/src/__tests__/utils/TurbopackMock.js b/packages/react-server-dom-turbopack/src/__tests__/utils/TurbopackMock.js index 7b10593220b60..35c1aae80a02b 100644 --- a/packages/react-server-dom-turbopack/src/__tests__/utils/TurbopackMock.js +++ b/packages/react-server-dom-turbopack/src/__tests__/utils/TurbopackMock.js @@ -55,7 +55,7 @@ exports.clientModuleError = function clientModuleError(moduleError) { chunks: [], name: '*', }; - const mod = {exports: {}}; + const mod = new Module(); nodeCompile.call(mod, '"use client"', idx); return mod.exports; }; @@ -107,7 +107,7 @@ exports.clientExports = function clientExports(moduleExports, chunkUrl) { name: 's', }; } - const mod = {exports: {}}; + const mod = new Module(); nodeCompile.call(mod, '"use client"', idx); return mod.exports; }; @@ -142,7 +142,8 @@ exports.serverExports = function serverExports(moduleExports) { name: 's', }; } - const mod = {exports: moduleExports}; + const mod = new Module(); + mod.exports = moduleExports; nodeCompile.call(mod, '"use server"', idx); return mod.exports; }; diff --git a/packages/react-server-dom-webpack/src/__tests__/utils/WebpackMock.js b/packages/react-server-dom-webpack/src/__tests__/utils/WebpackMock.js index 7f80eb10b84a8..7c7678860db6b 100644 --- a/packages/react-server-dom-webpack/src/__tests__/utils/WebpackMock.js +++ b/packages/react-server-dom-webpack/src/__tests__/utils/WebpackMock.js @@ -55,7 +55,7 @@ exports.clientModuleError = function clientModuleError(moduleError) { chunks: [], name: '*', }; - const mod = {exports: {}}; + const mod = new Module(); nodeCompile.call(mod, '"use client"', idx); return mod.exports; }; @@ -111,7 +111,7 @@ exports.clientExports = function clientExports( name: 's', }; } - const mod = {exports: {}}; + const mod = new Module(); nodeCompile.call(mod, '"use client"', idx); return mod.exports; }; @@ -146,7 +146,8 @@ exports.serverExports = function serverExports(moduleExports) { name: 's', }; } - const mod = {exports: moduleExports}; + const mod = new Module(); + mod.exports = moduleExports; nodeCompile.call(mod, '"use server"', idx); return mod.exports; };