deno.land / std@0.166.0 / node / module_test.ts

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2018-2022 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"), Error, "Importing ESM module:", );});
Deno.test("requireErrorInEval", async function () { const cwd = path.dirname(path.fromFileUrl(import.meta.url));
const command = new Deno.Command(Deno.execPath(), { args: [ "run", "--unstable", "--allow-read", "./_module/cjs/test_cjs_import.js", ], cwd, }); const { stderr, stdout } = await command.output();
const decoder = new TextDecoder(); const outputError = decoder.decode(stderr);
assert(!stdout.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", ), );});
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");});
Deno.test("a module can have its own timers declarations", function () { require("./_module/cjs/cjs_declare_timers");});
Deno.test("require in a web worker", async () => { const code = `\ import { createRequire } from "${new URL("module.ts", import.meta.url)}"; const require = createRequire("${import.meta.url}"); const result = require("./_module/cjs/cjs_a.js"); if (result.helloA() != "A") { throw new Error("assertion failed in worker"); } postMessage(null); `; const worker = new Worker( `data:application/javascript;base64,${btoa(code)}`, { type: "module" }, ); await new Promise((resolve, reject) => { worker.addEventListener("message", resolve); worker.addEventListener("error", reject); }); worker.terminate();});
Deno.test("createRequire with http(s):// URL throws with correct error message", () => { assertThrows( () => createRequire("http://example.com/foo.js"), Error, "createRequire only supports 'file://' URLs for the 'filename' parameter. Received 'http://example.com/foo.js'", ); assertThrows( () => createRequire("https://example.com/foo.js"), Error, "createRequire only supports 'file://' URLs for the 'filename' parameter. Received 'https://example.com/foo.js'", );});
Deno.test("require Node-API module", { ignore: Deno.build.arch === "aarch64" && Deno.build.os === "darwin",}, () => { const require = createRequire(import.meta.url); if (Deno.build.os === "windows") { // TODO(kt3k): Add lib binary for windows from 1_hello_world example of // https://github.com/nodejs/node-addon-examples } if (Deno.build.os === "linux") { // TODO(kt3k): Add lib binary for linux from 1_hello_world example of // https://github.com/nodejs/node-addon-examples } if (Deno.build.os === "darwin") { const str = require("./testdata/libhello_darwin.node").hello(); assertEquals(str, "world"); }});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago