error-stack.js 1.1 KB

12345678910111213141516171819202122232425262728
  1. test("Anonymous function", function () {
  2. let stackString = (() => {
  3. return Error();
  4. })().stack;
  5. let [header, ...stackFrames] = stackString.split("\n");
  6. expect(header).toBe("Error");
  7. expect(!!stackFrames[0].match(/^ at Error \(.*\/error-stack\.js:3:\d+\)$/)).toBeTrue();
  8. expect(!!stackFrames[1].match(/^ at .*\/error-stack\.js:3:\d+$/)).toBeTrue();
  9. expect(!!stackFrames[2].match(/^ at .*\/error-stack\.js:2:\d+$/)).toBeTrue();
  10. });
  11. test("Named function with message", function () {
  12. function f() {
  13. throw Error("You Shalt Not Pass!");
  14. }
  15. try {
  16. f();
  17. } catch (e) {
  18. let stackString = e.stack;
  19. let [header, ...stack_frames] = stackString.split("\n");
  20. expect(header).toBe("Error: You Shalt Not Pass!");
  21. expect(!!stack_frames[0].match(/^ at Error \(.*\/error-stack\.js:15:\d+\)$/)).toBeTrue();
  22. expect(!!stack_frames[1].match(/^ at f \(.*\/error-stack\.js:15:\d+\)$/)).toBeTrue();
  23. expect(!!stack_frames[2].match(/^ at .*\/error-stack\.js:18:\d+$/)).toBeTrue();
  24. }
  25. });