/
global_test.ts
113 lines (103 loc) · 3.98 KB
/
global_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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import "./global.ts";
import {
assert,
assertEquals,
assertNotEquals,
assertStrictEquals,
} from "../testing/asserts.ts";
import { Buffer as BufferModule } from "./buffer.ts";
import processModule from "./process.ts";
import timers from "./timers.ts";
// Definitions for this are quite delicate
// This ensures modifications to the global namespace don't break on TypeScript
// TODO(bartlomieju):
// Deno lint marks globals defined by this module as undefined
// probably gonna change in the future
Deno.test("global is correctly defined", () => {
// deno-lint-ignore no-undef
assertStrictEquals(global.Buffer, BufferModule);
// deno-lint-ignore no-undef
assertStrictEquals(global.process, process);
});
Deno.test("Buffer is correctly defined", () => {
//Check that Buffer is defined as a type as well
type x = Buffer;
// deno-lint-ignore no-undef
assertStrictEquals(Buffer, BufferModule);
// deno-lint-ignore no-undef
assert(Buffer.from);
// deno-lint-ignore no-undef
assertStrictEquals(global.Buffer, BufferModule);
// deno-lint-ignore no-undef
assert(global.Buffer.from);
assertStrictEquals(globalThis.Buffer, BufferModule);
assert(globalThis.Buffer.from);
assertStrictEquals(window.Buffer, BufferModule);
assert(window.Buffer.from);
});
Deno.test("process is correctly defined", () => {
// deno-lint-ignore no-undef
assertStrictEquals(process, processModule);
// deno-lint-ignore no-undef
assert(process.arch);
// deno-lint-ignore no-undef
assertStrictEquals(global.process, processModule);
// deno-lint-ignore no-undef
assert(global.process.arch);
assertStrictEquals(globalThis.process, processModule);
assert(globalThis.process.arch);
assertStrictEquals(window.process, processModule);
assert(window.process.arch);
});
Deno.test("global timers are not Node.js timers", () => {
assertNotEquals<unknown>(setTimeout, timers.setTimeout);
assertNotEquals(clearTimeout, timers.clearTimeout);
assertNotEquals<unknown>(setInterval, timers.setInterval);
assertNotEquals(clearInterval, timers.clearInterval);
});
Deno.test("timers in `global` object are Node.js timers", () => {
assertStrictEquals(global.setTimeout, timers.setTimeout);
assertStrictEquals(global.clearTimeout, timers.clearTimeout);
assertStrictEquals(global.setInterval, timers.setInterval);
assertStrictEquals(global.clearInterval, timers.clearInterval);
});
Deno.test("setImmediate is correctly defined", () => {
// deno-lint-ignore no-undef
assertStrictEquals(setImmediate, timers.setImmediate);
// deno-lint-ignore no-undef
assertStrictEquals(global.setImmediate, timers.setImmediate);
assertStrictEquals(globalThis.setImmediate, timers.setImmediate);
assertStrictEquals(window.setImmediate, timers.setImmediate);
});
Deno.test("clearImmediate is correctly defined", () => {
// deno-lint-ignore no-undef
assertStrictEquals(clearImmediate, timers.clearImmediate);
// deno-lint-ignore no-undef
assertStrictEquals(global.clearImmediate, timers.clearImmediate);
assertStrictEquals(globalThis.clearImmediate, timers.clearImmediate);
assertStrictEquals(window.clearImmediate, timers.clearImmediate);
});
// https://github.com/denoland/deno_std/issues/2097
Deno.test("global.ts evaluates synchronously", async () => {
const tempPath = await Deno.makeTempFile({ suffix: ".ts" });
try {
await Deno.writeTextFile(
tempPath,
`\
import "data:application/javascript,import '${
new URL("global.ts", import.meta.url).href
}'; console.log(globalThis.async ? 'async' : 'sync')";
import "data:application/javascript,globalThis.async = true";`,
);
const { status, stdout } = await Deno.spawn(Deno.execPath(), {
args: ["run", "--no-check", tempPath],
stdin: "null",
stderr: "null",
});
assertEquals(status.code, 0);
assertEquals(new TextDecoder().decode(stdout).trim(), "sync");
} finally {
await Deno.remove(tempPath).catch(() => {});
}
});