globalThis.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. var global = this;
  7. var bar = 5;
  8. const tests = [
  9. {
  10. name: "globalThis is the global",
  11. body() {
  12. assert.areEqual(global, globalThis, "globalThis should be the global object");
  13. }
  14. },
  15. {
  16. name: "globalThis has global properties",
  17. body() {
  18. assert.areEqual(Array, globalThis.Array, "globalThis should have all global properties");
  19. assert.areEqual(JSON, globalThis.JSON, "globalThis should have all global properties");
  20. assert.areEqual(Object, globalThis.Object, "globalThis should have all global properties");
  21. assert.areEqual(5, globalThis.bar, "globalThis should have all global properties");
  22. assert.areEqual(global, globalThis.global, "globalThis should have all global properties");
  23. assert.areEqual(global, globalThis.globalThis, "globalThis should have itself as a property");
  24. }
  25. },
  26. {
  27. name: "globalThis has correct attributes",
  28. body() {
  29. const descriptor = Object.getOwnPropertyDescriptor(globalThis, "globalThis");
  30. assert.isFalse(descriptor.enumerable, "globalThis should not be enumerable");
  31. assert.isTrue(descriptor.configurable, "globalThis should be configurable");
  32. assert.isTrue(descriptor.writable, "globalThis should be writable");
  33. assert.doesNotThrow(()=>{globalThis = 5;}, "Overwriting globalThis should not throw");
  34. assert.areEqual(5, global.globalThis, "Overwriting globalThis should succeed");
  35. assert.doesNotThrow(()=>{delete global.globalThis;}, "Deleting globalThis should not throw");
  36. assert.areEqual(undefined, global.globalThis, "Deleting globalThis should succeed");
  37. }
  38. },
  39. ];
  40. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });