forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-error.tsx
103 lines (86 loc) · 2.92 KB
/
test-error.tsx
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
import _ from 'lodash'
import React, { MouseEvent } from 'react'
import { observer } from 'mobx-react'
import Markdown from 'markdown-it'
import Collapsible from '../collapsible/collapsible'
import ErrorCodeFrame from '../errors/error-code-frame'
import ErrorStack from '../errors/error-stack'
import events from '../lib/events'
import FlashOnClick from '../lib/flash-on-click'
import { onEnterOrSpace } from '../lib/util'
import Attempt from '../attempts/attempt-model'
import Command from '../commands/command-model'
import { formattedMessage } from '../commands/command'
import WarningIcon from '-!react-svg-loader!@packages/frontend-shared/src/assets/icons/warning_x8.svg'
import TerminalIcon from '-!react-svg-loader!@packages/frontend-shared/src/assets/icons/technology-terminal_x16.svg'
interface DocsUrlProps {
url: string | string[]
}
const DocsUrl = ({ url }: DocsUrlProps) => {
if (!url) return null
const urlArray = _.castArray(url)
return (<>
{_.map(urlArray, (url) => (
<a className='runnable-err-docs-url' href={url} target='_blank' key={url}>
Learn more
</a>
))}
</>)
}
interface TestErrorProps {
model: Attempt | Command
onPrintToConsole?: () => void
}
const TestError = observer((props: TestErrorProps) => {
const md = new Markdown('zero')
md.enable(['backticks', 'emphasis', 'escape'])
const onPrint = props.onPrintToConsole || (() => {
events.emit('show:error', props.model)
})
const _onPrintClick = (e: MouseEvent) => {
e.stopPropagation()
onPrint()
}
const { err } = props.model
const { codeFrame } = err
if (!err.displayMessage) return null
return (
<div className='runnable-err-wrapper'>
<div className='runnable-err'>
<div className='runnable-err-header'>
<div className='runnable-err-name'>
<WarningIcon />
{err.name}
</div>
</div>
<div className='runnable-err-message'>
<span dangerouslySetInnerHTML={{ __html: formattedMessage(err.message) }} />
<DocsUrl url={err.docsUrl} />
</div>
{codeFrame && <ErrorCodeFrame codeFrame={codeFrame} />}
{err.stack &&
<Collapsible
header='View stack trace'
headerClass='runnable-err-stack-expander'
headerExtras={
<FlashOnClick onClick={_onPrintClick} message="Printed output to your console">
<div
className="runnable-err-print"
onKeyPress={onEnterOrSpace(onPrint)}
role='button'
tabIndex={0}
>
<div tabIndex={-1}><TerminalIcon /> <span>Print to console</span></div>
</div>
</FlashOnClick>
}
contentClass='runnable-err-stack-trace'
>
<ErrorStack err={err} />
</Collapsible>
}
</div>
</div>
)
})
export default TestError