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