-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution.ts
72 lines (62 loc) · 2 KB
/
execution.ts
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
import type { StackFrame, Stacktrace } from "./stacktrace.ts";
/**
* Holds the execution context for a given assertion, and is used to render the error message.
*/
export interface ExecutionContext {
/**
* The file path where the assertion was called. e.g. "/some/path.ts".
*/
filePath: string;
/**
* The file name where the assertion was called. e.g. "path.ts".
*/
fileName: string;
/**
* The line number within `filename` where the assertion was called. e.g. 42.
*/
lineNumber: number;
/**
* The column number within `filename` where the assertion was called. e.g. 24.
*/
columnNumber: number;
/**
* The location of the assertion. e.g. "/some/path.ts:124:12".
*/
at: string;
/**
* The stacktrace this execution context was captured from.
*/
stacktrace?: Stacktrace;
}
/**
* Captures the execution context from the provided stacktrace.
*
* If no stacktrace is provided, the execution context is not captured and the function returns `undefined`.
*
* @param stacktrace - The stacktrace to capture the execution context from, as returned by `new Error().stack`.
* @returns the execution context
*/
export function captureExecutionContext(
st: Stacktrace,
): ExecutionContext | undefined {
// In order to capture a useful execution context, we need at least 2 stack frames.
// The first frame is the current function, the second frame is the one that called it.
// In most cases, the first frame will be the matcher function itself, which is not useful to users.
// What we want is the second frame, which is the one that called the matcher.
if (!st || st.length <= 1) {
return undefined;
}
const stackFrame: StackFrame = st[1];
const filePath = stackFrame.filePath;
const fileName = stackFrame.fileName;
const lineNumber = stackFrame.lineNumber;
const columnNumber = stackFrame.columnNumber;
const at = `${filePath}:${lineNumber}:${columnNumber}`;
return {
filePath,
fileName,
lineNumber,
columnNumber,
at,
};
}