-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy path29253.test.ts
More file actions
173 lines (149 loc) · 6.24 KB
/
29253.test.ts
File metadata and controls
173 lines (149 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// https://github.com/oven-sh/bun/issues/29253
//
// `new Module(id, parent)` produced an instance whose prototype
// did not expose `Module.prototype.load(filename)`, so packages
// that construct a module by hand and then call `.load()` on it
// (the same pattern Node's internal cjs loader uses) threw:
//
// TypeError: targetModule.load is not a function
//
// `requizzle` — a dependency of `jsdoc` — does exactly this
// inside its `exports.load` helper, so `bun run .../jsdoc.js`
// crashed before jsdoc got a chance to run.
//
// The fix adds `Module.prototype.load` as a real method on the
// prototype shared by instances created via `new Module(...)`
// and unifies `require("module").prototype` with that same
// prototype, so patching one is reflected in the other (Node
// semantics).
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
import Module from "node:module";
test("Module.prototype.load is a function (#29253)", () => {
// The one the ticket is about: the stub on the instance prototype.
expect(typeof Module.prototype.load).toBe("function");
// An instance created via `new Module(...)` must inherit `.load`.
const m = new Module("/tmp/does-not-matter-29253.js", null);
expect(typeof m.load).toBe("function");
});
test("Module.prototype is the instance prototype (#29253)", () => {
// Node guarantees these are the same object — so patching
// `Module.prototype.foo` is visible on every instance. Several
// libraries (next.js, requizzle, etc.) rely on this.
const m = new Module("/tmp/does-not-matter-29253-proto.js", null);
expect(Object.getPrototypeOf(m)).toBe(Module.prototype);
});
test("new Module().load(filename) reads and evaluates the file (#29253)", async () => {
// Spawn a separate Bun so the test doesn't pollute its own
// require cache or Module.wrap state.
using dir = tempDir("issue-29253-load", {
"target.js": `
module.exports = { answer: 42, filename: __filename, dirname: __dirname };
`,
"driver.js": `
const Module = require("node:module");
const path = require("node:path");
const target = path.resolve(__dirname, "target.js");
const m = new Module(target, module);
m.load(target);
// After load(): the file has been read, wrapped, and
// executed. The module's exports must be the object the
// file assigned to module.exports, and the bookkeeping
// fields must be populated the way Node does.
console.log(JSON.stringify({
loaded: m.loaded,
filename: m.filename,
exports: m.exports,
}));
`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "driver.js"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).not.toContain("TypeError");
expect(stderr).not.toContain("Error");
expect(exitCode).toBe(0);
const result = JSON.parse(stdout.trim());
expect(result.loaded).toBe(true);
expect(result.filename).toMatch(/target\.js$/);
expect(result.exports.answer).toBe(42);
expect(result.exports.filename).toBe(result.filename);
});
test("Module.prototype.load honors an overridden Module.wrapper (#29253)", async () => {
// `load()` must compile the file through the CURRENT module
// wrapper (`Module.wrapper[0] + source + Module.wrapper[1]`)
// — not a hard-coded one. Mutating the wrapper array is how
// Bun exposes Node's wrapper-override hook.
using dir = tempDir("issue-29253-wrap", {
"target.js": `module.exports = { wrappedVar: typeof __swizzled };`,
"driver.js": `
const Module = require("node:module");
const path = require("node:path");
const originalWrapper0 = Module.wrapper[0];
// Inject a local 'const __swizzled = 1;' at the top of
// the module scope; if the wrapper is honored, the module
// sees typeof __swizzled === "number".
Module.wrapper[0] = originalWrapper0 + "const __swizzled = 1;\\n";
try {
const target = path.resolve(__dirname, "target.js");
const m = new Module(target, module);
m.load(target);
console.log(m.exports.wrappedVar);
} finally {
Module.wrapper[0] = originalWrapper0;
}
`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "driver.js"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).not.toContain("TypeError");
expect(stderr).not.toContain("ReferenceError");
expect(exitCode).toBe(0);
expect(stdout.trim()).toBe("number");
});
test("new Module().load populates filename/paths/loaded (#29253)", async () => {
// Node's `Module.prototype.load` writes `filename`, `paths`,
// and `loaded` before returning. `requizzle` and any other
// package that reads those fields after `.load()` depends on
// this, even if it doesn't touch the wrapper.
using dir = tempDir("issue-29253-fields", {
"leaf.js": `module.exports = 'ok';`,
"driver.js": `
const Module = require("node:module");
const path = require("node:path");
const target = path.resolve(__dirname, "leaf.js");
const m = new Module(target, module);
// Pre-load state: loaded=false, no filename.
if (m.loaded !== false) throw new Error("pre-load 'loaded' should be false, got " + m.loaded);
m.load(target);
if (m.loaded !== true) throw new Error("post-load 'loaded' should be true");
if (m.filename !== target) throw new Error("filename mismatch: " + m.filename);
if (!Array.isArray(m.paths)) throw new Error("paths should be an array");
if (m.exports !== 'ok') throw new Error("exports mismatch: " + m.exports);
console.log("ok");
`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "driver.js"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).not.toContain("TypeError");
expect(stderr).not.toContain("Error:");
expect(exitCode).toBe(0);
expect(stdout.trim()).toBe("ok");
});