forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.ts
125 lines (110 loc) · 3.14 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
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertStringIncludes,
} from "../testing/asserts.ts";
import * as path from "../path/mod.ts";
import { createRequire } from "./module.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("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) {
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");
});
Deno.test("requireNodeJsNativeModules", () => {
// Checks these exist and don't throw.
require("assert");
require("buffer");
require("crypto");
require("events");
require("fs");
require("os");
require("path");
require("querystring");
require("stream");
require("string_decoder");
require("timers");
require("tty");
require("url");
require("util");
// TODO(kt3k): add these modules when implemented
// require("child_process");
// require("cluster");
// require("console");
// require("dgram");
// require("dns");
// require("http");
// require("http2");
// require("https");
// require("net");
// require("perf_hooks");
// require("readline");
// require("repl");
// require("sys");
// require("tls");
// require("vm");
// require("worker_threads");
// require("zlib");
});
Deno.test("Require file with shebang", () => {
require("./testdata/shebang.js");
});