forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals_test.ts
156 lines (135 loc) · 3.39 KB
/
globals_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert, unitTest } from "./test_util.ts";
unitTest(function globalThisExists(): void {
assert(globalThis != null);
});
unitTest(function noInternalGlobals(): void {
// globalThis.__bootstrap should not be there.
for (const key of Object.keys(globalThis)) {
assert(!key.startsWith("_"));
}
});
unitTest(function windowExists(): void {
assert(window != null);
});
unitTest(function selfExists(): void {
assert(self != null);
});
unitTest(function windowWindowExists(): void {
assert(window.window === window);
});
unitTest(function windowSelfExists(): void {
assert(window.self === window);
});
unitTest(function globalThisEqualsWindow(): void {
assert(globalThis === window);
});
unitTest(function globalThisEqualsSelf(): void {
assert(globalThis === self);
});
unitTest(function globalThisInstanceofWindow(): void {
assert(globalThis instanceof Window);
});
unitTest(function globalThisConstructorLength(): void {
assert(globalThis.constructor.length === 0);
});
unitTest(function globalThisInstanceofEventTarget(): void {
assert(globalThis instanceof EventTarget);
});
unitTest(function navigatorInstanceofNavigator(): void {
// TODO(nayeemrmn): Add `Navigator` to deno_lint globals.
// deno-lint-ignore no-undef
assert(navigator instanceof Navigator);
});
unitTest(function DenoNamespaceExists(): void {
assert(Deno != null);
});
unitTest(function DenoNamespaceEqualsWindowDeno(): void {
assert(Deno === window.Deno);
});
unitTest(function DenoNamespaceIsFrozen(): void {
assert(Object.isFrozen(Deno));
});
unitTest(function webAssemblyExists(): void {
assert(typeof WebAssembly.compile === "function");
});
declare global {
// deno-lint-ignore no-namespace
namespace Deno {
// deno-lint-ignore no-explicit-any
var core: any;
}
}
unitTest(function DenoNamespaceImmutable(): void {
const denoCopy = window.Deno;
try {
// deno-lint-ignore no-explicit-any
(Deno as any) = 1;
} catch {
// pass
}
assert(denoCopy === Deno);
try {
// deno-lint-ignore no-explicit-any
(window as any).Deno = 1;
} catch {
// pass
}
assert(denoCopy === Deno);
try {
// deno-lint-ignore no-explicit-any
delete (window as any).Deno;
} catch {
// pass
}
assert(denoCopy === Deno);
const { readFile } = Deno;
try {
// deno-lint-ignore no-explicit-any
(Deno as any).readFile = 1;
} catch {
// pass
}
assert(readFile === Deno.readFile);
try {
// deno-lint-ignore no-explicit-any
delete (window as any).Deno.readFile;
} catch {
// pass
}
assert(readFile === Deno.readFile);
const { print } = Deno.core;
try {
Deno.core.print = 1;
} catch {
// pass
}
assert(print === Deno.core.print);
try {
delete Deno.core.print;
} catch {
// pass
}
assert(print === Deno.core.print);
});
unitTest(async function windowQueueMicrotask(): Promise<void> {
let resolve1: () => void | undefined;
let resolve2: () => void | undefined;
let microtaskDone = false;
const p1 = new Promise<void>((res): void => {
resolve1 = (): void => {
microtaskDone = true;
res();
};
});
const p2 = new Promise<void>((res): void => {
resolve2 = (): void => {
assert(microtaskDone);
res();
};
});
window.queueMicrotask(resolve1!);
setTimeout(resolve2!, 0);
await p1;
await p2;
});