-
Notifications
You must be signed in to change notification settings - Fork 469
Pr/440 show line in debug #733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kentcdodds
merged 12 commits into
testing-library:master
from
victorandcode:pr/440-show-line-in-debug
Sep 2, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fe31e34
feat(get-user-trace): create utility to obtain the client's stack fra…
victorandcode 9692ebe
feat(pretty-dom): return user stack from pretty-dom
victorandcode 2c0797b
feat(get-user-trace): add coloring to get-user-trace
victorandcode 3f02326
feat: instead of showing location of debug call, print full code frame
victorandcode 202abcd
feat: conditionally load node dependencies and return empty if they c…
victorandcode 32cd44c
test: ignore coverage of catch when dependencies to render trace can'…
victorandcode 3be5856
test: refactor test names to be more expressive and move user code fr…
victorandcode 514f841
refactor: rename get-user-trace to get-user-code-frame
victorandcode 859aa7e
feat: display location on top of code frame
victorandcode b108fe0
feat: make frame location color dimmer
victorandcode c47fca6
test: shorten paths used as test data for get user code frame
victorandcode 6c6df90
feat: remove warnings from get user code frame
victorandcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import fs from 'fs' | ||
import {getUserCodeFrame} from '../get-user-code-frame' | ||
|
||
jest.mock('fs', () => ({ | ||
// We setup the contents of a sample file | ||
readFileSync: jest.fn( | ||
() => ` | ||
import {screen} from '@testing-library/dom' | ||
it('renders', () => { | ||
document.body.appendChild( | ||
document.createTextNode('Hello world') | ||
) | ||
screen.debug() | ||
expect(screen.getByText('Hello world')).toBeInTheDocument() | ||
}) | ||
`, | ||
), | ||
})) | ||
|
||
const userStackFrame = 'at somethingWrong (/sample-error/error-example.js:7:14)' | ||
|
||
let globalErrorMock | ||
|
||
beforeEach(() => { | ||
// Mock global.Error so we can setup our own stack messages | ||
globalErrorMock = jest.spyOn(global, 'Error') | ||
}) | ||
|
||
afterEach(() => { | ||
global.Error.mockRestore() | ||
}) | ||
|
||
test('it returns only user code frame when code frames from node_modules are first', () => { | ||
const stack = `Error: Kaboom | ||
at Object.<anonymous> (/sample-error/node_modules/@es2050/console/build/index.js:4:10) | ||
${userStackFrame} | ||
` | ||
globalErrorMock.mockImplementationOnce(() => ({stack})) | ||
const userTrace = getUserCodeFrame(stack) | ||
|
||
expect(userTrace).toMatchInlineSnapshot(` | ||
"/sample-error/error-example.js:7:14 | ||
5 | document.createTextNode('Hello world') | ||
6 | ) | ||
> 7 | screen.debug() | ||
| ^ | ||
" | ||
`) | ||
}) | ||
|
||
test('it returns only user code frame when node code frames are present afterwards', () => { | ||
const stack = `Error: Kaboom | ||
at Object.<anonymous> (/sample-error/node_modules/@es2050/console/build/index.js:4:10) | ||
${userStackFrame} | ||
at Object.<anonymous> (/sample-error/error-example.js:14:1) | ||
at internal/main/run_main_module.js:17:47 | ||
` | ||
globalErrorMock.mockImplementationOnce(() => ({stack})) | ||
const userTrace = getUserCodeFrame() | ||
|
||
expect(userTrace).toMatchInlineSnapshot(` | ||
"/sample-error/error-example.js:7:14 | ||
5 | document.createTextNode('Hello world') | ||
6 | ) | ||
> 7 | screen.debug() | ||
| ^ | ||
" | ||
`) | ||
}) | ||
|
||
test("it returns empty string if file from code frame can't be read", () => { | ||
// Make fire read purposely fail | ||
fs.readFileSync.mockImplementationOnce(() => { | ||
throw Error() | ||
}) | ||
const stack = `Error: Kaboom | ||
${userStackFrame} | ||
` | ||
globalErrorMock.mockImplementationOnce(() => ({stack})) | ||
|
||
expect(getUserCodeFrame(stack)).toEqual('') | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// We try to load node dependencies | ||
let chalk = null | ||
let readFileSync = null | ||
let codeFrameColumns = null | ||
|
||
try { | ||
const nodeRequire = module && module.require | ||
|
||
readFileSync = nodeRequire.call(module, 'fs').readFileSync | ||
codeFrameColumns = nodeRequire.call(module, '@babel/code-frame') | ||
.codeFrameColumns | ||
chalk = nodeRequire.call(module, 'chalk') | ||
} catch { | ||
// We're in a browser environment | ||
} | ||
|
||
// frame has the form "at myMethod (location/to/my/file.js:10:2)" | ||
function getCodeFrame(frame) { | ||
const locationStart = frame.indexOf('(') + 1 | ||
const locationEnd = frame.indexOf(')') | ||
const frameLocation = frame.slice(locationStart, locationEnd) | ||
|
||
const frameLocationElements = frameLocation.split(':') | ||
const [filename, line, column] = [ | ||
frameLocationElements[0], | ||
parseInt(frameLocationElements[1], 10), | ||
parseInt(frameLocationElements[2], 10), | ||
] | ||
|
||
let rawFileContents = '' | ||
try { | ||
rawFileContents = readFileSync(filename, 'utf-8') | ||
} catch { | ||
return '' | ||
} | ||
|
||
const codeFrame = codeFrameColumns( | ||
rawFileContents, | ||
{ | ||
start: {line, column}, | ||
}, | ||
{ | ||
highlightCode: true, | ||
linesBelow: 0, | ||
}, | ||
) | ||
return `${chalk.dim(frameLocation)}\n${codeFrame}\n` | ||
} | ||
|
||
function getUserCodeFrame() { | ||
// If we couldn't load dependencies, we can't generate the user trace | ||
/* istanbul ignore next */ | ||
if (!readFileSync || !codeFrameColumns) { | ||
return '' | ||
} | ||
const err = new Error() | ||
const firstClientCodeFrame = err.stack | ||
.split('\n') | ||
.slice(1) // Remove first line which has the form "Error: TypeError" | ||
.find(frame => !frame.includes('node_modules/')) // Ignore frames from 3rd party libraries | ||
|
||
return getCodeFrame(firstClientCodeFrame) | ||
} | ||
|
||
export {getUserCodeFrame} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.