error-stack-accessors.js (1137B)
1 // Test the Error.prototype.stack getter/setter with various "fun" edge cases. 2 3 load(libdir + "asserts.js"); 4 5 // Stack should be accessible in subclasses. The accessor should walk up the 6 // prototype chain. 7 assertEq(typeof Object.create(Error()).stack, "string"); 8 assertEq(Object.create(Error.prototype).stack, ""); 9 10 // Stack should be overridable in subclasses. 11 { 12 let myError = Object.create(Error()); 13 myError.stack = 5; 14 assertEq(myError.stack, 5); 15 16 let myOtherError = Object.create(Error.prototype); 17 myOtherError.stack = 2; 18 assertEq(myOtherError.stack, 2); 19 } 20 21 // Should throw when there is no Error in the `this` object's prototype chain. 22 var obj = Object.create(null); 23 var desc = Object.getOwnPropertyDescriptor(Error.prototype, "stack"); 24 Object.defineProperty(obj, "stack", desc); 25 assertThrowsInstanceOf(() => obj.stack, TypeError); 26 27 // Should throw with non-object `this` values. 28 assertThrowsInstanceOf(desc.set, TypeError); 29 assertThrowsInstanceOf(desc.set.bind("string"), TypeError); 30 assertThrowsInstanceOf(desc.get, TypeError); 31 assertThrowsInstanceOf(desc.get.bind("string"), TypeError);