test_safeErrorString.js (1600B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test DevToolsUtils.safeErrorString 7 8 function run_test() { 9 test_with_error(); 10 test_with_tricky_error(); 11 test_with_string(); 12 test_with_thrower(); 13 test_with_psychotic(); 14 } 15 16 function test_with_error() { 17 const s = DevToolsUtils.safeErrorString(new Error("foo bar")); 18 // Got the message. 19 Assert.ok(s.includes("foo bar")); 20 // Got the stack. 21 Assert.ok(s.includes("test_with_error")); 22 Assert.ok(s.includes("test_safeErrorString.js")); 23 // Got the lineNumber and columnNumber. 24 Assert.ok(s.includes("Line")); 25 Assert.ok(s.includes("column")); 26 } 27 28 function test_with_tricky_error() { 29 const e = new Error("batman"); 30 e.stack = { toString: Object.create(null) }; 31 const s = DevToolsUtils.safeErrorString(e); 32 // Still got the message, despite a bad stack property. 33 Assert.ok(s.includes("batman")); 34 } 35 36 function test_with_string() { 37 const s = DevToolsUtils.safeErrorString("not really an error"); 38 // Still get the message. 39 Assert.ok(s.includes("not really an error")); 40 } 41 42 function test_with_thrower() { 43 const s = DevToolsUtils.safeErrorString({ 44 toString: () => { 45 throw new Error("Muahahaha"); 46 }, 47 }); 48 // Still don't fail, get string back. 49 Assert.equal(typeof s, "string"); 50 } 51 52 function test_with_psychotic() { 53 const s = DevToolsUtils.safeErrorString({ 54 toString: () => Object.create(null), 55 }); 56 // Still get a string out, and no exceptions thrown 57 Assert.equal(typeof s, "string"); 58 Assert.equal(s, "[object Object]"); 59 }