2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-06-04 19:09:29 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file no-window-prefix no-window
|
2024-07-09 13:07:56 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertRejects,
|
|
|
|
assertThrows,
|
2024-08-26 17:58:28 -04:00
|
|
|
DENO_FUTURE,
|
2024-07-09 13:07:56 -04:00
|
|
|
} from "./test_util.ts";
|
2019-01-16 17:57:15 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function globalThisExists() {
|
2019-01-16 17:57:15 -05:00
|
|
|
assert(globalThis != null);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function noInternalGlobals() {
|
2020-09-04 07:52:19 -04:00
|
|
|
// globalThis.__bootstrap should not be there.
|
|
|
|
for (const key of Object.keys(globalThis)) {
|
|
|
|
assert(!key.startsWith("_"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-08-26 17:58:28 -04:00
|
|
|
Deno.test({ ignore: DENO_FUTURE }, function windowExists() {
|
2019-01-16 17:57:15 -05:00
|
|
|
assert(window != null);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function selfExists() {
|
2020-02-26 05:49:38 -05:00
|
|
|
assert(self != null);
|
|
|
|
});
|
|
|
|
|
2024-08-26 17:58:28 -04:00
|
|
|
Deno.test({ ignore: DENO_FUTURE }, function windowWindowExists() {
|
2019-01-16 17:57:15 -05:00
|
|
|
assert(window.window === window);
|
|
|
|
});
|
|
|
|
|
2024-08-26 17:58:28 -04:00
|
|
|
Deno.test({ ignore: DENO_FUTURE }, function windowSelfExists() {
|
2020-02-26 05:49:38 -05:00
|
|
|
assert(window.self === window);
|
|
|
|
});
|
|
|
|
|
2024-08-26 17:58:28 -04:00
|
|
|
Deno.test({ ignore: DENO_FUTURE }, function globalThisEqualsWindow() {
|
2019-01-16 17:57:15 -05:00
|
|
|
assert(globalThis === window);
|
|
|
|
});
|
2019-02-05 08:12:58 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function globalThisEqualsSelf() {
|
2020-02-26 05:49:38 -05:00
|
|
|
assert(globalThis === self);
|
|
|
|
});
|
|
|
|
|
2024-08-26 17:58:28 -04:00
|
|
|
Deno.test({ ignore: DENO_FUTURE }, function globalThisInstanceofWindow() {
|
2020-10-11 18:04:43 -04:00
|
|
|
assert(globalThis instanceof Window);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function globalThisConstructorLength() {
|
2020-11-14 07:10:23 -05:00
|
|
|
assert(globalThis.constructor.length === 0);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function globalThisInstanceofEventTarget() {
|
2020-10-11 18:04:43 -04:00
|
|
|
assert(globalThis instanceof EventTarget);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function navigatorInstanceofNavigator() {
|
2021-03-08 07:27:49 -05:00
|
|
|
// TODO(nayeemrmn): Add `Navigator` to deno_lint globals.
|
|
|
|
// deno-lint-ignore no-undef
|
|
|
|
assert(navigator instanceof Navigator);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function DenoNamespaceExists() {
|
2019-02-12 10:08:56 -05:00
|
|
|
assert(Deno != null);
|
|
|
|
});
|
|
|
|
|
2024-08-26 17:58:28 -04:00
|
|
|
Deno.test({ ignore: DENO_FUTURE }, function DenoNamespaceEqualsWindowDeno() {
|
2019-02-12 10:08:56 -05:00
|
|
|
assert(Deno === window.Deno);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function DenoNamespaceIsNotFrozen() {
|
2021-06-21 17:17:35 -04:00
|
|
|
assert(!Object.isFrozen(Deno));
|
2019-02-12 10:08:56 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function webAssemblyExists() {
|
2019-02-05 08:12:58 -05:00
|
|
|
assert(typeof WebAssembly.compile === "function");
|
|
|
|
});
|
2019-04-19 20:39:54 -04:00
|
|
|
|
2023-01-24 12:54:10 -05:00
|
|
|
// @ts-ignore This is not publicly typed namespace, but it's there for sure.
|
|
|
|
const core = Deno[Deno.internal].core;
|
2020-06-02 00:24:44 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function DenoNamespaceConfigurable() {
|
2021-06-21 17:17:35 -04:00
|
|
|
const desc = Object.getOwnPropertyDescriptor(globalThis, "Deno");
|
|
|
|
assert(desc);
|
|
|
|
assert(desc.configurable);
|
|
|
|
assert(!desc.writable);
|
|
|
|
});
|
2019-04-19 20:39:54 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function DenoCoreNamespaceIsImmutable() {
|
2023-01-24 12:54:10 -05:00
|
|
|
const { print } = core;
|
2019-04-19 20:39:54 -04:00
|
|
|
try {
|
2023-01-24 12:54:10 -05:00
|
|
|
core.print = 1;
|
2020-06-19 05:05:37 -04:00
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
2023-01-24 12:54:10 -05:00
|
|
|
assert(print === core.print);
|
2019-04-19 20:39:54 -04:00
|
|
|
try {
|
2023-01-24 12:54:10 -05:00
|
|
|
delete core.print;
|
2020-06-19 05:05:37 -04:00
|
|
|
} catch {
|
|
|
|
// pass
|
|
|
|
}
|
2023-01-24 12:54:10 -05:00
|
|
|
assert(print === core.print);
|
2019-04-19 20:39:54 -04:00
|
|
|
});
|
2019-08-31 15:16:30 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function windowQueueMicrotask() {
|
2019-08-31 15:16:30 -04:00
|
|
|
let resolve1: () => void | undefined;
|
|
|
|
let resolve2: () => void | undefined;
|
|
|
|
let microtaskDone = false;
|
2021-08-05 07:08:58 -04:00
|
|
|
const p1 = new Promise<void>((res) => {
|
|
|
|
resolve1 = () => {
|
2019-11-13 13:42:34 -05:00
|
|
|
microtaskDone = true;
|
|
|
|
res();
|
|
|
|
};
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
const p2 = new Promise<void>((res) => {
|
|
|
|
resolve2 = () => {
|
2019-11-13 13:42:34 -05:00
|
|
|
assert(microtaskDone);
|
|
|
|
res();
|
|
|
|
};
|
|
|
|
});
|
2024-08-26 17:58:28 -04:00
|
|
|
if (DENO_FUTURE) {
|
|
|
|
globalThis.queueMicrotask(resolve1!);
|
|
|
|
} else {
|
|
|
|
window.queueMicrotask(resolve1!);
|
|
|
|
}
|
2019-08-31 15:16:30 -04:00
|
|
|
setTimeout(resolve2!, 0);
|
|
|
|
await p1;
|
|
|
|
await p2;
|
|
|
|
});
|
2021-10-21 02:47:14 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function webApiGlobalThis() {
|
2021-10-21 02:47:14 -04:00
|
|
|
assert(globalThis.FormData !== null);
|
|
|
|
assert(globalThis.TextEncoder !== null);
|
|
|
|
assert(globalThis.TextEncoderStream !== null);
|
|
|
|
assert(globalThis.TextDecoder !== null);
|
|
|
|
assert(globalThis.TextDecoderStream !== null);
|
|
|
|
assert(globalThis.CountQueuingStrategy !== null);
|
|
|
|
assert(globalThis.ByteLengthQueuingStrategy !== null);
|
|
|
|
});
|
2023-10-08 18:12:59 -04:00
|
|
|
|
|
|
|
Deno.test(function windowNameIsDefined() {
|
|
|
|
assertEquals(typeof globalThis.name, "string");
|
|
|
|
assertEquals(name, "");
|
2024-08-26 17:58:28 -04:00
|
|
|
if (!DENO_FUTURE) {
|
|
|
|
assertEquals(window.name, name);
|
|
|
|
}
|
2023-10-08 18:12:59 -04:00
|
|
|
name = "foobar";
|
2024-08-26 17:58:28 -04:00
|
|
|
if (!DENO_FUTURE) {
|
|
|
|
assertEquals(window.name, "foobar");
|
|
|
|
}
|
2023-10-08 18:12:59 -04:00
|
|
|
assertEquals(name, "foobar");
|
|
|
|
name = "";
|
2024-08-26 17:58:28 -04:00
|
|
|
if (!DENO_FUTURE) {
|
|
|
|
assertEquals(window.name, "");
|
|
|
|
}
|
2023-10-08 18:12:59 -04:00
|
|
|
assertEquals(name, "");
|
|
|
|
});
|
2023-11-01 15:25:09 -04:00
|
|
|
|
|
|
|
Deno.test(async function promiseWithResolvers() {
|
|
|
|
{
|
|
|
|
const { promise, resolve } = Promise.withResolvers();
|
|
|
|
resolve(true);
|
|
|
|
assert(await promise);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const { promise, reject } = Promise.withResolvers();
|
|
|
|
reject(new Error("boom!"));
|
|
|
|
await assertRejects(() => promise, Error, "boom!");
|
|
|
|
}
|
|
|
|
});
|
2023-11-01 18:25:18 -04:00
|
|
|
|
|
|
|
Deno.test(async function arrayFromAsync() {
|
|
|
|
// Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fromAsync#examples
|
|
|
|
// Thank you.
|
|
|
|
const asyncIterable = (async function* () {
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 10 * i));
|
|
|
|
yield i;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
const a = await Array.fromAsync(asyncIterable);
|
|
|
|
assertEquals(a, [0, 1, 2, 3, 4]);
|
|
|
|
|
|
|
|
const b = await Array.fromAsync(new Map([[1, 2], [3, 4]]));
|
|
|
|
assertEquals(b, [[1, 2], [3, 4]]);
|
|
|
|
});
|
2023-11-01 19:18:41 -04:00
|
|
|
|
|
|
|
// Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy#examples
|
|
|
|
Deno.test(function objectGroupBy() {
|
|
|
|
const inventory = [
|
|
|
|
{ name: "asparagus", type: "vegetables", quantity: 5 },
|
|
|
|
{ name: "bananas", type: "fruit", quantity: 0 },
|
|
|
|
{ name: "goat", type: "meat", quantity: 23 },
|
|
|
|
{ name: "cherries", type: "fruit", quantity: 5 },
|
|
|
|
{ name: "fish", type: "meat", quantity: 22 },
|
|
|
|
];
|
|
|
|
const result = Object.groupBy(inventory, ({ type }) => type);
|
|
|
|
assertEquals(result, {
|
|
|
|
vegetables: [
|
|
|
|
{ name: "asparagus", type: "vegetables", quantity: 5 },
|
|
|
|
],
|
|
|
|
fruit: [
|
|
|
|
{ name: "bananas", type: "fruit", quantity: 0 },
|
|
|
|
{ name: "cherries", type: "fruit", quantity: 5 },
|
|
|
|
],
|
|
|
|
meat: [
|
|
|
|
{ name: "goat", type: "meat", quantity: 23 },
|
|
|
|
{ name: "fish", type: "meat", quantity: 22 },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-12-29 18:41:40 -05:00
|
|
|
Deno.test(function objectGroupByEmpty() {
|
|
|
|
const empty: string[] = [];
|
|
|
|
const result = Object.groupBy(empty, () => "abc");
|
|
|
|
assertEquals(result.abc, undefined);
|
|
|
|
});
|
|
|
|
|
2023-11-01 19:18:41 -04:00
|
|
|
// Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/groupBy#examples
|
|
|
|
Deno.test(function mapGroupBy() {
|
|
|
|
const inventory = [
|
|
|
|
{ name: "asparagus", type: "vegetables", quantity: 9 },
|
|
|
|
{ name: "bananas", type: "fruit", quantity: 5 },
|
|
|
|
{ name: "goat", type: "meat", quantity: 23 },
|
|
|
|
{ name: "cherries", type: "fruit", quantity: 12 },
|
|
|
|
{ name: "fish", type: "meat", quantity: 22 },
|
|
|
|
];
|
|
|
|
const restock = { restock: true };
|
|
|
|
const sufficient = { restock: false };
|
|
|
|
const result = Map.groupBy(
|
|
|
|
inventory,
|
|
|
|
({ quantity }) => quantity < 6 ? restock : sufficient,
|
|
|
|
);
|
|
|
|
assertEquals(result.get(restock), [{
|
|
|
|
name: "bananas",
|
|
|
|
type: "fruit",
|
|
|
|
quantity: 5,
|
|
|
|
}]);
|
|
|
|
});
|
2024-07-09 13:07:56 -04:00
|
|
|
|
|
|
|
Deno.test(function nodeGlobalsRaise() {
|
|
|
|
assertThrows(() => {
|
|
|
|
// @ts-ignore yes that's the point
|
|
|
|
Buffer;
|
|
|
|
}, ReferenceError);
|
|
|
|
});
|