summaryrefslogtreecommitdiff
path: root/cli/tests/unit/globals_test.ts
blob: 53fafc5b686195fe054b9e1b02a408604d771859 (plain)
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
// Copyright 2018-2020 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 globalThisInstanceofEventTarget(): void {
  assert(globalThis instanceof EventTarget);
});

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");
});

/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-explicit-any,no-var */
declare global {
  // deno-lint-ignore no-namespace
  namespace Deno {
    // deno-lint-ignore no-explicit-any
    var core: any;
  }
}
/* eslint-enable */

unitTest(function DenoNamespaceImmutable(): void {
  const denoCopy = window.Deno;
  try {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (Deno as any) = 1;
  } catch {
    // pass
  }
  assert(denoCopy === Deno);
  try {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (window as any).Deno = 1;
  } catch {
    // pass
  }
  assert(denoCopy === Deno);
  try {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    delete (window as any).Deno;
  } catch {
    // pass
  }
  assert(denoCopy === Deno);

  const { readFile } = Deno;
  try {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (Deno as any).readFile = 1;
  } catch {
    // pass
  }
  assert(readFile === Deno.readFile);
  try {
    // eslint-disable-next-line @typescript-eslint/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((res): void => {
    resolve1 = (): void => {
      microtaskDone = true;
      res();
    };
  });
  const p2 = new Promise((res): void => {
    resolve2 = (): void => {
      assert(microtaskDone);
      res();
    };
  });
  window.queueMicrotask(resolve1!);
  setTimeout(resolve2!, 0);
  await p1;
  await p2;
});