summaryrefslogtreecommitdiff
path: root/cli/js/error_stack_test.ts
blob: 4c7edb2fd795b1635076961e703df1446d7cc328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert } from "./test_util.ts";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { setPrepareStackTrace } = Deno as any;

interface CallSite {
  getThis(): unknown;
  getTypeName(): string;
  getFunction(): Function;
  getFunctionName(): string;
  getMethodName(): string;
  getFileName(): string;
  getLineNumber(): number | null;
  getColumnNumber(): number | null;
  getEvalOrigin(): string | null;
  isToplevel(): boolean;
  isEval(): boolean;
  isNative(): boolean;
  isConstructor(): boolean;
  isAsync(): boolean;
  isPromiseAll(): boolean;
  getPromiseIndex(): number | null;
}

function getMockCallSite(
  filename: string,
  line: number | null,
  column: number | null
): CallSite {
  return {
    getThis(): unknown {
      return undefined;
    },
    getTypeName(): string {
      return "";
    },
    getFunction(): Function {
      return (): void => {};
    },
    getFunctionName(): string {
      return "";
    },
    getMethodName(): string {
      return "";
    },
    getFileName(): string {
      return filename;
    },
    getLineNumber(): number | null {
      return line;
    },
    getColumnNumber(): number | null {
      return column;
    },
    getEvalOrigin(): null {
      return null;
    },
    isToplevel(): false {
      return false;
    },
    isEval(): false {
      return false;
    },
    isNative(): false {
      return false;
    },
    isConstructor(): false {
      return false;
    },
    isAsync(): false {
      return false;
    },
    isPromiseAll(): false {
      return false;
    },
    getPromiseIndex(): null {
      return null;
    }
  };
}

test(function prepareStackTrace(): void {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const MockError = {} as any;
  setPrepareStackTrace(MockError);
  assert(typeof MockError.prepareStackTrace === "function");
  const prepareStackTrace: (
    error: Error,
    structuredStackTrace: CallSite[]
  ) => string = MockError.prepareStackTrace;
  const result = prepareStackTrace(new Error("foo"), [
    getMockCallSite("CLI_SNAPSHOT.js", 23, 0)
  ]);
  assert(result.startsWith("Error: foo\n"));
  assert(result.includes(".ts:"), "should remap to something in 'js/'");
});

test(function applySourceMap(): void {
  const result = Deno.applySourceMap({
    filename: "CLI_SNAPSHOT.js",
    line: 23,
    column: 0
  });
  assert(result.filename.endsWith(".ts"));
  assert(result.line != null);
  assert(result.column != null);
});