2020-04-20 14:30:52 -04:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
const { test } = Deno;
|
2020-03-19 10:42:07 -04:00
|
|
|
import { assertEquals, assert, assertStrContains } from "../testing/asserts.ts";
|
2019-11-19 16:44:59 -05:00
|
|
|
import { createRequire } from "./module.ts";
|
|
|
|
|
2020-04-20 14:30:52 -04:00
|
|
|
const require = createRequire(import.meta.url);
|
2019-11-19 16:44:59 -05:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("requireSuccess", function () {
|
2019-11-19 16:44:59 -05:00
|
|
|
// Relative to import.meta.url
|
2020-04-20 14:30:52 -04:00
|
|
|
const result = require("./tests/cjs/cjs_a.js");
|
2019-11-19 16:44:59 -05:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("requireCycle", function () {
|
2020-04-20 14:30:52 -04:00
|
|
|
const resultA = require("./tests/cjs/cjs_cycle_a");
|
|
|
|
const resultB = require("./tests/cjs/cjs_cycle_b");
|
2019-11-19 16:44:59 -05:00
|
|
|
assert(resultA);
|
|
|
|
assert(resultB);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("requireBuiltin", function () {
|
2020-04-20 14:30:52 -04:00
|
|
|
const fs = require("fs");
|
2019-11-19 16:44:59 -05:00
|
|
|
assert("readFileSync" in fs);
|
2020-04-20 14:30:52 -04:00
|
|
|
const { readFileSync, isNull, extname } = require("./tests/cjs/cjs_builtin");
|
2019-11-19 16:44:59 -05:00
|
|
|
assertEquals(
|
2020-03-16 07:40:36 -04:00
|
|
|
readFileSync("./node/_fs/testdata/hello.txt", { encoding: "utf8" }),
|
2019-11-19 16:44:59 -05:00
|
|
|
"hello world"
|
|
|
|
);
|
|
|
|
assert(isNull(null));
|
|
|
|
assertEquals(extname("index.html"), ".html");
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("requireIndexJS", function () {
|
2020-04-20 14:30:52 -04:00
|
|
|
const { isIndex } = require("./tests/cjs");
|
2019-11-19 16:44:59 -05:00
|
|
|
assert(isIndex);
|
|
|
|
});
|
2020-01-29 20:57:29 -05:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("requireNodeOs", function () {
|
2020-04-20 14:30:52 -04:00
|
|
|
const os = require("os");
|
2020-01-29 20:57:29 -05:00
|
|
|
assert(os.arch);
|
|
|
|
assert(typeof os.arch() == "string");
|
|
|
|
});
|
2020-03-19 10:42:07 -04:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("requireStack", function () {
|
2020-04-20 14:30:52 -04:00
|
|
|
const { hello } = require("./tests/cjs/cjs_throw");
|
2020-03-19 10:42:07 -04:00
|
|
|
try {
|
|
|
|
hello();
|
|
|
|
} catch (e) {
|
|
|
|
assertStrContains(e.stack, "/tests/cjs/cjs_throw.js");
|
|
|
|
}
|
|
|
|
});
|