Skip to content

Commit 1cd5216

Browse files
committed
fix(test): TestRunner avoids using jest mock's require
1 parent fa54eac commit 1cd5216

4 files changed

Lines changed: 49 additions & 5 deletions

File tree

test/configCases/externals/externals-array/webpack.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
const webpack = require("../../../../");
22

3-
/** @type {import("../../../../").Configuration[]} */
3+
/** @type {import("../../../../types").Configuration[]} */
44
module.exports = [
55
{
66
output: {
7-
libraryTarget: "commonjs2"
7+
libraryTarget: "global"
88
},
99
externals: {
10-
external: ["webpack", "version"]
10+
external: ["process", "version"]
1111
},
1212
plugins: [
1313
new webpack.DefinePlugin({
14-
EXPECTED: JSON.stringify(webpack.version)
14+
EXPECTED: JSON.stringify(process.version)
1515
})
1616
]
1717
},
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function getMajorVersion(versionStr) {
2+
const match = versionStr.match(/^v?(\d+)\./);
3+
if (match) {
4+
return parseInt(match[1], 10);
5+
}
6+
return null;
7+
}
8+
9+
it("should not fail on optional externals", function() {
10+
if (getMajorVersion(NODE_VERSION) <= 12) {
11+
const external = require("external");
12+
// The behavior of jest mock's require is different from that of node require, so it works fine here.
13+
expect(external).toBe(EXPECTED);
14+
} else {
15+
try {
16+
require("external");
17+
} catch (e) {
18+
// Since there is no webpack in node_modules, node require will report an error here.
19+
expect(e.message).toContain("Cannot find module 'webpack'");
20+
}
21+
}
22+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const webpack = require("../../../../");
2+
3+
/** @type {import("../../../../types").Configuration} */
4+
module.exports = {
5+
output: {
6+
libraryTarget: "commonjs2"
7+
},
8+
externals: {
9+
external: ["webpack", "version"]
10+
},
11+
plugins: [
12+
new webpack.DefinePlugin({
13+
NODE_VERSION: JSON.stringify(process.version),
14+
EXPECTED: JSON.stringify(webpack.version)
15+
})
16+
]
17+
};

test/runner/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require("fs");
2+
const { Module } = require("module");
23
const path = require("path");
34
const { fileURLToPath, pathToFileURL } = require("url");
45
const vm = require("vm");
@@ -292,7 +293,11 @@ class TestRunner {
292293
}
293294
const moduleInfo = this._resolveModule(currentDirectory, module);
294295
if (!moduleInfo) {
295-
return require(module.startsWith("node:") ? module.slice(5) : module);
296+
// node v12.2.0+ has Module.createRequire
297+
const rawRequire = Module.createRequire
298+
? Module.createRequire(currentDirectory)
299+
: require;
300+
return rawRequire(module.startsWith("node:") ? module.slice(5) : module);
296301
}
297302
const { modulePath } = moduleInfo;
298303
if (

0 commit comments

Comments
 (0)