2020-04-20 14:30:52 -04:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
2020-06-05 23:43:00 -04:00
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assert,
|
|
|
|
assertStringContains,
|
|
|
|
} from "../testing/asserts.ts";
|
2020-09-08 11:37:58 -04:00
|
|
|
|
|
|
|
import * as path from "../path/mod.ts";
|
2019-11-19 16:44:59 -05:00
|
|
|
import { createRequire } from "./module.ts";
|
|
|
|
|
2020-09-08 11:37:58 -04:00
|
|
|
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = path.resolve(moduleDir, path.join("_fs", "testdata"));
|
|
|
|
|
2020-04-20 14:30:52 -04:00
|
|
|
const require = createRequire(import.meta.url);
|
2019-11-19 16:44:59 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.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-06-12 15:23:38 -04:00
|
|
|
Deno.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-06-12 15:23:38 -04:00
|
|
|
Deno.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");
|
2020-09-08 11:37:58 -04:00
|
|
|
|
|
|
|
const testData = path.relative(
|
|
|
|
Deno.cwd(),
|
|
|
|
path.join(testdataDir, "hello.txt"),
|
|
|
|
);
|
2019-11-19 16:44:59 -05:00
|
|
|
assertEquals(
|
2020-09-08 11:37:58 -04:00
|
|
|
readFileSync(testData, { encoding: "utf8" }),
|
2020-07-14 15:24:17 -04:00
|
|
|
"hello world",
|
2019-11-19 16:44:59 -05:00
|
|
|
);
|
|
|
|
assert(isNull(null));
|
|
|
|
assertEquals(extname("index.html"), ".html");
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.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-06-12 15:23:38 -04:00
|
|
|
Deno.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-06-12 15:23:38 -04:00
|
|
|
Deno.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) {
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStringContains(e.stack, "/tests/cjs/cjs_throw.js");
|
2020-03-19 10:42:07 -04:00
|
|
|
}
|
|
|
|
});
|