forked from gov-suite/governed-service-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_test.ts
104 lines (99 loc) · 3.29 KB
/
import_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
import * as ta from "https://deno.land/[email protected]/testing/asserts.ts";
import * as expected from "./import_test-module-valid.ts";
import * as mod from "./import.ts";
Deno.test("erroneous import of unchecked import_test-module-valid.ts", async () => {
let reportedDiag: string | undefined;
let importErrorEncountered: Error | undefined;
let moduleUrlEncountered: string | undefined;
const e = await mod.importModuleDefault<expected.Expected>(
"import_test-module-valid.ts",
{
onImportError: (err: Error, moduleUrl: string): string | undefined => {
importErrorEncountered = err;
moduleUrlEncountered = moduleUrl;
return undefined;
},
reportDiagnostic: (diagnostic: string): void => {
reportedDiag = diagnostic;
},
},
);
ta.assert(e === undefined);
ta.assert(importErrorEncountered);
ta.assert(moduleUrlEncountered);
ta.assert(reportedDiag);
ta.assertStringIncludes(
reportedDiag,
`Unable to import module import_test-module-valid.ts: TypeError: relative import path "import_test-module-valid.ts" not prefixed with / or ./ or ../`,
);
});
Deno.test("valid import of unchecked import_test-module-valid.ts", async () => {
const e = await mod.importModuleDefault<expected.Expected>(
"./import_test-module-valid.ts",
);
ta.assert(e);
});
Deno.test("valid import of guarded import_test-module-valid.ts", async () => {
let isGuarded = false;
const e = await mod.importModuleDefault<expected.Expected>(
"./import_test-module-valid.ts",
{
typeGuard: expected.isExpected,
onSuccessfulImport: (
instance: expected.Expected,
guarded: boolean,
): expected.Expected => {
isGuarded = guarded;
return instance;
},
},
);
ta.assert(e);
ta.assert(isGuarded);
});
Deno.test("invalid import of missing default: import_test-module-no-default.ts", async () => {
let reportedDiag: string | undefined;
let noModuleDefaultEncountered = false;
const e = await mod.importModuleDefault<expected.Expected>(
"./import_test-module-no-default.ts",
{
typeGuard: expected.isExpected,
onNoModuleDefault: (): string | undefined => {
noModuleDefaultEncountered = true;
return undefined;
},
reportDiagnostic: (diagnostic: string): void => {
reportedDiag = diagnostic;
},
},
);
ta.assert(typeof e === "undefined");
ta.assert(noModuleDefaultEncountered);
ta.assertEquals(
reportedDiag,
`No module.default found in ./import_test-module-no-default.ts`,
);
});
Deno.test("invalid import of guard failure: import_test-module-invalid-type.ts", async () => {
let reportedDiag: string | undefined;
let guardFailureEncountered = false;
const e = await mod.importModuleDefault<expected.Expected>(
"./import_test-module-invalid-type.ts",
{
typeGuard: expected.isExpected,
onGuardFailure: (): string | undefined => {
guardFailureEncountered = true;
return undefined;
},
reportDiagnostic: (diagnostic: string): void => {
reportedDiag = diagnostic;
},
},
);
ta.assert(typeof e === "undefined");
ta.assert(guardFailureEncountered);
ta.assertEquals(
reportedDiag,
`module.default did not pass type guard: ./import_test-module-invalid-type.ts`,
);
});