Skip to content

Commit f0d576f

Browse files
committed
Refactor test names to be more expressive and move user code frame into variable
1 parent 18fb7dd commit f0d576f

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/__tests__/get-user-trace.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@ import fs from 'fs'
22
import {getUserTrace} from '../get-user-trace'
33

44
jest.mock('fs', () => ({
5+
// We setup the contents of a sample file
56
readFileSync: jest.fn(
67
() => `
78
import { screen } from '@testing-library/dom'
8-
99
it('renders', () => {
1010
document.body.appendChild(
1111
document.createTextNode('Hello world')
1212
)
1313
screen.debug()
14-
15-
1614
expect(screen.getByText('Hello world')).toBeInTheDocument()
1715
})
1816
`,
1917
),
2018
}))
2119

20+
const userStackFrame =
21+
'at somethingWrong (/home/john/projects/sample-error/error-example.js:7:14)'
22+
2223
let globalErrorMock
2324

2425
beforeEach(() => {
@@ -30,46 +31,43 @@ afterEach(() => {
3031
global.Error.mockRestore()
3132
})
3233

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', () => {
3435
const stack = `Error: Kaboom
3536
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}
3738
`
3839
globalErrorMock.mockImplementationOnce(() => ({stack}))
3940
const userTrace = getUserTrace(stack)
41+
4042
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+
| ^
4547
"
4648
`)
4749
})
4850

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', () => {
5052
const stack = `Error: Kaboom
5153
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}
5355
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)
5956
at internal/main/run_main_module.js:17:47
6057
`
6158
globalErrorMock.mockImplementationOnce(() => ({stack}))
6259
const userTrace = getUserTrace()
60+
6361
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+
| ^
6866
"
6967
`)
7068
})
7169

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", () => {
7371
const consoleWarnSpy = jest
7472
.spyOn(global.console, 'warn')
7573
.mockImplementationOnce(jest.fn)
@@ -78,15 +76,14 @@ test("it returns empty string if file from frame can't be read", () => {
7876
fs.readFileSync.mockImplementationOnce(() => {
7977
throw Error()
8078
})
81-
const filePath = '/home/john/projects/sample-error/error-example.js'
8279
const stack = `Error: Kaboom
83-
at somethingWrong (${filePath}:8:7)
80+
${userStackFrame}
8481
`
8582
globalErrorMock.mockImplementationOnce(() => ({stack}))
8683

8784
expect(getUserTrace(stack)).toEqual('')
8885
expect(consoleWarnSpy).toHaveBeenCalledTimes(1)
8986
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`,
9188
)
9289
})

0 commit comments

Comments
 (0)