-
Notifications
You must be signed in to change notification settings - Fork 576
/
module_test.ts
199 lines (172 loc) · 5.3 KB
/
module_test.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertStringIncludes,
assertThrows,
} from "../testing/asserts.ts";
import * as path from "../path/mod.ts";
import Module, { createRequire } from "./module.ts";
import nodeMods from "./module_all.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
const testdataDir = path.resolve(moduleDir, path.join("_fs", "testdata"));
const require = createRequire(import.meta.url);
Deno.test("requireSuccess", function () {
// Relative to import.meta.url
const result = require("./_module/cjs/cjs_a.js");
assert("helloA" in result);
assert("helloB" in result);
assert("C" in result);
assert("leftPad" in result);
assertEquals(result.helloA(), "A");
assertEquals(result.helloB(), "B");
assertEquals(result.C, "C");
assertEquals(result.leftPad("pad", 4), " pad");
});
Deno.test("requireCycle", function () {
const resultA = require("./_module/cjs/cjs_cycle_a");
const resultB = require("./_module/cjs/cjs_cycle_b");
assert(resultA);
assert(resultB);
});
Deno.test("requireBuiltin", function () {
const fs = require("fs");
assert("readFileSync" in fs);
const { readFileSync, isNull, extname } = require(
"./_module/cjs/cjs_builtin",
);
const testData = path.relative(
Deno.cwd(),
path.join(testdataDir, "hello.txt"),
);
assertEquals(
readFileSync(testData, { encoding: "utf8" }),
"hello world",
);
assert(isNull(null));
assertEquals(extname("index.html"), ".html");
});
Deno.test("requireIndexJS", function () {
const { isIndex } = require("./_module/cjs");
assert(isIndex);
});
Deno.test("requireWithNodePrefix", function () {
const osWithPrefix = require("node:os");
const os = require("os");
assert(osWithPrefix === os);
assert(osWithPrefix.arch);
assert(typeof osWithPrefix.arch() == "string");
});
Deno.test("requireNodeOs", function () {
const os = require("os");
assert(os.arch);
assert(typeof os.arch() == "string");
});
Deno.test("requireStack", function () {
const { hello } = require("./_module/cjs/cjs_throw");
try {
hello();
} catch (e) {
assert(e instanceof Error);
assert(e.stack);
assertStringIncludes(e.stack, "/_module/cjs/cjs_throw.js");
}
});
Deno.test("requireFileInSymlinkDir", () => {
const { C } = require("./_module/cjs/dir");
assertEquals(C, "C");
});
Deno.test("requireModuleWithConditionalExports", () => {
const { red, blue } = require("./_module/cjs/cjs_conditional_exports");
assert(typeof red === "function");
assert(typeof blue === "function");
});
const SUPPORTED_NODE_MODULES = Object.keys(nodeMods);
Deno.test("requireNodeJsNativeModules", () => {
// Checks these exist and don't throw.
for (const name of SUPPORTED_NODE_MODULES) {
require(name);
}
});
Deno.test("native modules are extensible", () => {
const randomKey = "random-key";
const randomValue = "random-value";
for (const name of SUPPORTED_NODE_MODULES) {
const mod = require(name);
Object.defineProperty(mod, randomKey, {
value: randomValue,
configurable: true,
});
assertEquals(mod[randomKey], randomValue);
delete mod[randomKey];
assertEquals(mod[randomKey], undefined);
}
});
Deno.test("Require file with shebang", () => {
require("./testdata/shebang.js");
});
Deno.test("EventEmitter is exported correctly", () => {
const EventEmitter = require("events");
assertEquals(EventEmitter, EventEmitter.EventEmitter);
});
Deno.test("Require .mjs", () => {
assertThrows(
() => require("./testdata/inspect.mjs"),
undefined,
"Importing ESM module",
);
});
Deno.test("requireErrorInEval", async function () {
const cwd = path.dirname(path.fromFileUrl(import.meta.url));
const p = Deno.run({
cmd: [
Deno.execPath(),
"run",
"--unstable",
"--allow-read",
"./_module/cjs/test_cjs_import.js",
],
cwd,
stdout: "piped",
stderr: "piped",
});
const decoder = new TextDecoder();
const output = await p.output();
const outputError = decoder.decode(await p.stderrOutput());
assert(!output.length);
assert(
outputError.includes(
'To load an ES module, set "type": "module" in the package.json or use the .mjs extension.',
),
);
assert(
outputError.includes(
"SyntaxError: Cannot use import statement outside a module",
),
);
p.close();
});
Deno.test("requireCjsWithDynamicImport", function () {
require("./_module/cjs/cjs_with_dynamic_import");
});
Deno.test("requireWithImportsExports", function () {
require("./_module/cjs/cjs_imports_exports");
});
Deno.test("module has proper members", function () {
const module = require("module");
assert(module._cache);
assert(module._extensions);
assert(typeof module._findPath == "function");
assert(typeof module._initPaths == "function");
assert(typeof module._load == "function");
assert(typeof module._nodeModulePaths == "function");
assert(module._pathCache);
assert(typeof module._preloadModules == "function");
assert(typeof module._resolveFilename == "function");
assert(typeof module._resolveLookupPaths == "function");
assert(module.builtinModules);
assert(typeof module.createRequire == "function");
assert(module.globalPaths);
assert(module.Module === Module);
assert(typeof module.wrap == "function");
});