-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
stack_trace_filter.ts
53 lines (45 loc) · 1.46 KB
/
stack_trace_filter.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
import stackChain from 'stack-chain'
import path from 'path'
import { valueOrDefault } from './value_checker'
import CallSite = NodeJS.CallSite
const projectRootPath = path.join(__dirname, '..')
const projectChildDirs = ['src', 'lib', 'node_modules']
export function isFileNameInCucumber(fileName: string): boolean {
return projectChildDirs.some((dir) =>
fileName.startsWith(path.join(projectRootPath, dir))
)
}
export default class StackTraceFilter {
private currentFilter: CallSite[]
filter(): void {
this.currentFilter = stackChain.filter.attach(
(_err: any, frames: CallSite[]) => {
if (this.isErrorInCucumber(frames)) {
return frames
}
const index = frames.findIndex((x) => this.isFrameInCucumber(x))
if (index === -1) {
return frames
}
return frames.slice(0, index)
}
)
}
isErrorInCucumber(frames: CallSite[]): boolean {
const filteredFrames = frames.filter((x) => !this.isFrameInNode(x))
return (
filteredFrames.length > 0 && this.isFrameInCucumber(filteredFrames[0])
)
}
isFrameInCucumber(frame: CallSite): boolean {
const fileName = valueOrDefault(frame.getFileName(), '')
return isFileNameInCucumber(fileName)
}
isFrameInNode(frame: CallSite): boolean {
const fileName = valueOrDefault(frame.getFileName(), '')
return !fileName.includes(path.sep)
}
unfilter(): void {
stackChain.filter.deattach(this.currentFilter)
}
}