Skip to content

Commit 08a4c55

Browse files
committed
Display location on top of code frame
1 parent 439a4d7 commit 08a4c55

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

src/__tests__/get-user-code-frame.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ test('it returns only client code frame when code frames from node_modules are f
4040
const userTrace = getUserCodeFrame(stack)
4141

4242
expect(userTrace).toMatchInlineSnapshot(`
43-
" 5 | document.createTextNode('Hello world')
43+
"/home/john/projects/sample-error/error-example.js:7:14
44+
5 | document.createTextNode('Hello world')
4445
6 | )
4546
> 7 | screen.debug()
4647
| ^
@@ -59,7 +60,8 @@ test('it returns only client code frame when node code frames are present afterw
5960
const userTrace = getUserCodeFrame()
6061

6162
expect(userTrace).toMatchInlineSnapshot(`
62-
" 5 | document.createTextNode('Hello world')
63+
"/home/john/projects/sample-error/error-example.js:7:14
64+
5 | document.createTextNode('Hello world')
6365
6 | )
6466
> 7 | screen.debug()
6567
| ^

src/__tests__/pretty-dom.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {prettyDOM, logDOM} from '../pretty-dom'
2+
import {getUserCodeFrame} from '../get-user-code-frame'
23
import {render, renderIntoDocument} from './helpers/test-utils'
34

4-
jest.mock('../get-user-code-frame', () => ({
5-
getUserCodeFrame: () => '',
6-
}))
5+
jest.mock('../get-user-code-frame')
76

87
beforeEach(() => {
98
jest.spyOn(console, 'log').mockImplementation(() => {})
@@ -64,6 +63,35 @@ test('logDOM logs prettyDOM to the console', () => {
6463
`)
6564
})
6665

66+
test('logDOM logs prettyDOM with code frame to the console', () => {
67+
getUserCodeFrame.mockImplementationOnce(
68+
() => `"/home/john/projects/sample-error/error-example.js:7:14
69+
5 | document.createTextNode('Hello World!')
70+
6 | )
71+
> 7 | screen.debug()
72+
| ^
73+
"
74+
`,
75+
)
76+
const {container} = render('<div>Hello World!</div>')
77+
logDOM(container)
78+
expect(console.log).toHaveBeenCalledTimes(1)
79+
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
80+
"<div>
81+
<div>
82+
Hello World!
83+
</div>
84+
</div>
85+
"/home/john/projects/sample-error/error-example.js:7:14
86+
5 | document.createTextNode('Hello World!')
87+
6 | )
88+
> 7 | screen.debug()
89+
| ^
90+
"
91+
"
92+
`)
93+
})
94+
6795
describe('prettyDOM fails with first parameter without outerHTML field', () => {
6896
test('with array', () => {
6997
expect(() => prettyDOM(['outerHTML'])).toThrowErrorMatchingInlineSnapshot(

src/get-user-code-frame.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function getCodeFrame(frame) {
4646
linesBelow: 0,
4747
},
4848
)
49-
return `${codeFrame}\n`
49+
return `${frameLocation}\n${codeFrame}\n`
5050
}
5151

5252
function getUserCodeFrame() {

src/pretty-dom.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ function prettyDOM(dom, maxLength, options) {
6262
: debugContent
6363
}
6464

65-
const logDOM = (...args) =>
66-
console.log(`${getUserCodeFrame()}${prettyDOM(...args)}`)
65+
const logDOM = (...args) => {
66+
const userCodeFrame = getUserCodeFrame()
67+
if (userCodeFrame) {
68+
console.log(`${prettyDOM(...args)}\n${userCodeFrame}`)
69+
} else {
70+
console.log(prettyDOM(...args))
71+
}
72+
}
6773

6874
export {prettyDOM, logDOM}

0 commit comments

Comments
 (0)