Skip to content

Commit 13f3543

Browse files
authored
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 ```
1 parent 6cd6ba7 commit 13f3543

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

packages/react-server-dom-turbopack/src/__tests__/utils/TurbopackMock.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ exports.clientModuleError = function clientModuleError(moduleError) {
5555
chunks: [],
5656
name: '*',
5757
};
58-
const mod = {exports: {}};
58+
const mod = new Module();
5959
nodeCompile.call(mod, '"use client"', idx);
6060
return mod.exports;
6161
};
@@ -107,7 +107,7 @@ exports.clientExports = function clientExports(moduleExports, chunkUrl) {
107107
name: 's',
108108
};
109109
}
110-
const mod = {exports: {}};
110+
const mod = new Module();
111111
nodeCompile.call(mod, '"use client"', idx);
112112
return mod.exports;
113113
};
@@ -142,7 +142,8 @@ exports.serverExports = function serverExports(moduleExports) {
142142
name: 's',
143143
};
144144
}
145-
const mod = {exports: moduleExports};
145+
const mod = new Module();
146+
mod.exports = moduleExports;
146147
nodeCompile.call(mod, '"use server"', idx);
147148
return mod.exports;
148149
};

packages/react-server-dom-webpack/src/__tests__/utils/WebpackMock.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ exports.clientModuleError = function clientModuleError(moduleError) {
5555
chunks: [],
5656
name: '*',
5757
};
58-
const mod = {exports: {}};
58+
const mod = new Module();
5959
nodeCompile.call(mod, '"use client"', idx);
6060
return mod.exports;
6161
};
@@ -111,7 +111,7 @@ exports.clientExports = function clientExports(
111111
name: 's',
112112
};
113113
}
114-
const mod = {exports: {}};
114+
const mod = new Module();
115115
nodeCompile.call(mod, '"use client"', idx);
116116
return mod.exports;
117117
};
@@ -146,7 +146,8 @@ exports.serverExports = function serverExports(moduleExports) {
146146
name: 's',
147147
};
148148
}
149-
const mod = {exports: moduleExports};
149+
const mod = new Module();
150+
mod.exports = moduleExports;
150151
nodeCompile.call(mod, '"use server"', idx);
151152
return mod.exports;
152153
};

0 commit comments

Comments
 (0)