@@ -2,23 +2,24 @@ import fs from 'fs'
2
2
import { getUserTrace } from '../get-user-trace'
3
3
4
4
jest . mock ( 'fs' , ( ) => ( {
5
+ // We setup the contents of a sample file
5
6
readFileSync : jest . fn (
6
7
( ) => `
7
8
import { screen } from '@testing-library/dom'
8
-
9
9
it('renders', () => {
10
10
document.body.appendChild(
11
11
document.createTextNode('Hello world')
12
12
)
13
13
screen.debug()
14
-
15
-
16
14
expect(screen.getByText('Hello world')).toBeInTheDocument()
17
15
})
18
16
` ,
19
17
) ,
20
18
} ) )
21
19
20
+ const userStackFrame =
21
+ 'at somethingWrong (/home/john/projects/sample-error/error-example.js:7:14)'
22
+
22
23
let globalErrorMock
23
24
24
25
beforeEach ( ( ) => {
@@ -30,46 +31,43 @@ afterEach(() => {
30
31
global . Error . mockRestore ( )
31
32
} )
32
33
33
- test ( 'it returns only client error when frames from node_modules are first' , ( ) => {
34
+ test ( 'it returns only client code frame when code frames from node_modules are first' , ( ) => {
34
35
const stack = `Error: Kaboom
35
36
at Object.<anonymous> (/home/john/projects/projects/sample-error/node_modules/@es2050/console/build/index.js:4:10)
36
- at somethingWrong (/home/john/projects/sample-error/error-example.js:8:7)
37
+ ${ userStackFrame }
37
38
`
38
39
globalErrorMock . mockImplementationOnce ( ( ) => ( { stack} ) )
39
40
const userTrace = getUserTrace ( stack )
41
+
40
42
expect ( userTrace ) . toMatchInlineSnapshot ( `
41
- " 6 | document.createTextNode('Hello world')
42
- 7 | )
43
- > 8 | screen.debug()
44
- | ^
43
+ " 5 | document.createTextNode('Hello world')
44
+ 6 | )
45
+ > 7 | screen.debug()
46
+ | ^
45
47
"
46
48
` )
47
49
} )
48
50
49
- test ( 'it returns only client error when node frames are present afterwards' , ( ) => {
51
+ test ( 'it returns only client code frame when node code frames are present afterwards' , ( ) => {
50
52
const stack = `Error: Kaboom
51
53
at Object.<anonymous> (/home/john/projects/projects/sample-error/node_modules/@es2050/console/build/index.js:4:10)
52
- at somethingWrong (/home/john/projects/sample-error/error-example.js:8:7)
54
+ ${ userStackFrame }
53
55
at Object.<anonymous> (/home/user/Documents/projects/sample-error/error-example.js:14:1)
54
- at Module._compile (internal/modules/cjs/loader.js:1151:30)
55
- at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
56
- at Module.load (internal/modules/cjs/loader.js:1000:32)
57
- at Function.Module._load (internal/modules/cjs/loader.js:899:14)
58
- at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
59
56
at internal/main/run_main_module.js:17:47
60
57
`
61
58
globalErrorMock . mockImplementationOnce ( ( ) => ( { stack} ) )
62
59
const userTrace = getUserTrace ( )
60
+
63
61
expect ( userTrace ) . toMatchInlineSnapshot ( `
64
- " 6 | document.createTextNode('Hello world')
65
- 7 | )
66
- > 8 | screen.debug()
67
- | ^
62
+ " 5 | document.createTextNode('Hello world')
63
+ 6 | )
64
+ > 7 | screen.debug()
65
+ | ^
68
66
"
69
67
` )
70
68
} )
71
69
72
- test ( "it returns empty string if file from frame can't be read" , ( ) => {
70
+ test ( "it returns empty string if file from code frame can't be read" , ( ) => {
73
71
const consoleWarnSpy = jest
74
72
. spyOn ( global . console , 'warn' )
75
73
. mockImplementationOnce ( jest . fn )
@@ -78,15 +76,14 @@ test("it returns empty string if file from frame can't be read", () => {
78
76
fs . readFileSync . mockImplementationOnce ( ( ) => {
79
77
throw Error ( )
80
78
} )
81
- const filePath = '/home/john/projects/sample-error/error-example.js'
82
79
const stack = `Error: Kaboom
83
- at somethingWrong ( ${ filePath } :8:7)
80
+ ${ userStackFrame }
84
81
`
85
82
globalErrorMock . mockImplementationOnce ( ( ) => ( { stack} ) )
86
83
87
84
expect ( getUserTrace ( stack ) ) . toEqual ( '' )
88
85
expect ( consoleWarnSpy ) . toHaveBeenCalledTimes ( 1 )
89
86
expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
90
- `Couldn't read file ${ filePath } for displaying the code frame` ,
87
+ `Couldn't read file /home/john/projects/sample-error/error-example.js for displaying the code frame` ,
91
88
)
92
89
} )
0 commit comments