test(javascript): execute the registered Webpack export fixture (#6951)

This commit is contained in:
gensokyo-dev
2026-10-24 13:27:20 +08:00
committed by GitHub
parent b5c42dfc2a
commit d87e73f2db
@@ -5,6 +5,7 @@ import { createRequire } from "node:module";
import { join, relative, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { promisify } from "node:util";
import { runInNewContext } from "node:vm";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
@@ -74,15 +75,6 @@ afterAll(async () => {
});
const WEBPACK_FACTORY_BODY = `
const key = "dynamic";
const __webpack_require__ = {
d(target, definitions) {
for (const name of Object.keys(definitions))
Object.defineProperty(target, name, {
enumerable: true,
get: definitions[name],
});
},
};
exports[key] = 1;
exports["dot.key"] = 2;
exports[""] = 3;
@@ -1106,14 +1098,28 @@ describe("Webpack factory CommonJS export keys", () => {
}]);`;
await writeFile(join(root, "bundle.js"), bundle);
// Execute the same factory body as CommonJS, outside the analyzer, to
// verify Node's alias behavior and the exact runtime object keys.
// Execute the exact bundle and invoke its registered factory with a loader.
// This also rejects invalid factory syntax before trusting the export oracle.
const actual = runInNewContext(`
globalThis.webpackChunkStaticExports = [];
${bundle}
const module = { exports: {} };
const loader = {
d(target, definitions) {
for (const name of Object.keys(definitions))
Object.defineProperty(target, name, {
enumerable: true,
get: definitions[name],
});
},
};
const [, factories] = globalThis.webpackChunkStaticExports[0];
factories[1](module, module.exports, loader);
module.exports;
`) as Record<string, unknown>;
const oracleRoot = await createTestTempDirectory(
"rea-webpack-export-oracle-",
);
const oraclePath = join(oracleRoot, "oracle.cjs");
await writeFile(oraclePath, WEBPACK_FACTORY_BODY);
const actual = requireFixture(oraclePath) as Record<string, unknown>;
expect(Reflect.ownKeys(actual).sort()).toEqual([
"",
"dot.key",