-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.mjs
More file actions
65 lines (45 loc) · 1.43 KB
/
tests.mjs
File metadata and controls
65 lines (45 loc) · 1.43 KB
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
import {describe, test, expect} from "vitest";
import esm, {check, required} from ".";
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const cjs = require(".");
describe("CJS", () => {
test("check() returns a boolean value", () => {
expect(typeof cjs.check()).toBe("boolean");
});
test("required() should throw if not elevated and not throw if elevated", () => {
if (cjs.check())
expect(() => cjs.required()).not.toThrow();
else
expect(() => cjs.required()).toThrow(cjs.defaultMessage);
});
test("message can be customized", () => {
const custom = "Custom elevation message";
try {
if (!cjs.check())
expect(() => cjs.required(custom)).toThrow(custom);
} finally {
}
});
test("default message can be customized", () => {
const original = cjs.defaultMessage;
const custom = "Custom elevation message";
try {
cjs.defaultMessage = custom;
if (!cjs.check())
expect(() => cjs.required()).toThrow(custom);
} finally {
cjs.defaultMessage = original;
}
});
});
describe("ESM", () => {
test("check and required should be in default export", () => {
expect(esm.check).toBe(check);
expect(esm.required).toBe(required);
});
test("check and required should be the CommonJS functions", () => {
expect(esm.check).toBe(cjs.check);
expect(esm.required).toBe(cjs.required);
});
});