common-functionality.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. testRunner.runTests([
  7. {
  8. name: "Helpers should not show up in stack traces",
  9. body() {
  10. for (const builtin of [Array.prototype.forEach, Array.prototype.filter, Array.prototype.flatMap]) {
  11. assert.isTrue(typeof(builtin.name) === "string" && builtin.name.length > 0, `Test requires builtin.name to be set for ${builtin.toString()}`);
  12. try {
  13. builtin.call([1, 2, 3], function callback() { throw new Error("error in callback") });
  14. assert.isTrue(false, `Exception swallowed from callback for ${builtin.name}`);
  15. } catch (e) {
  16. const frames = e.stack.split("\n");
  17. assert.isTrue(/error in callback/.test(frames[0]), `Invalid first frame "${frames[0]}" for ${builtin.name}`);
  18. assert.isTrue(/at callback \(/.test(frames[1]), `Invalid second frame "${frames[1]}" for ${builtin.name}`);
  19. assert.isTrue(new RegExp(`at Array.prototype.${builtin.name} \\(native code\\)`, "i").test(frames[2]), `Invalid third frame "${frames[2]}" for ${builtin.name}`);
  20. assert.isTrue(/at body \(/.test(frames[3]), `Invalid fourth frame "${frames[3]}" for ${builtin.name}`);
  21. }
  22. }
  23. }
  24. },
  25. {
  26. name: "(Existing) JsBuiltIns shouldn't be constructable",
  27. body() {
  28. for (const builtin of [
  29. Array.prototype.values,
  30. Array.prototype.entries,
  31. Array.prototype.keys,
  32. Array.prototype.indexOf,
  33. Array.prototype.forEach,
  34. Array.prototype.filter,
  35. Array.prototype.flat,
  36. Array.prototype.flatMap,
  37. Object.fromEntries,
  38. ]) {
  39. assert.isTrue(typeof(builtin.name) === "string" && builtin.name.length > 0, `Test requires builtin.name to be set for ${builtin.toString()}`);
  40. assert.throws(() => new builtin(), TypeError, `${builtin.name} should not be constructable (using new)`, "Function is not a constructor");
  41. assert.throws(() => Reflect.construct(builtin, []), TypeError, `${builtin.name} should not be constructable (using Reflect.construct target)`, "'target' is not a constructor");
  42. assert.throws(() => Reflect.construct(function(){}, [], builtin), TypeError, `${builtin.name} should not be constructable (using Reflect.construct newTarget)`, "'newTarget' is not a constructor");
  43. }
  44. }
  45. },
  46. ], { verbose: false });