-
Notifications
You must be signed in to change notification settings - Fork 472
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
feat(logDOM): add logDOM export #336
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,61 @@ | ||
import {prettyDOM} from '../pretty-dom' | ||
import {render} from './helpers/test-utils' | ||
import {prettyDOM, logDOM} from '../pretty-dom' | ||
import {render, renderIntoDocument} from './helpers/test-utils' | ||
|
||
test('prints out the given DOM element tree', () => { | ||
beforeEach(() => { | ||
jest.spyOn(console, 'log').mockImplementation(() => {}) | ||
}) | ||
|
||
afterEach(() => { | ||
console.log.mockRestore() | ||
}) | ||
|
||
test('prettyDOM prints out the given DOM element tree', () => { | ||
const {container} = render('<div>Hello World!</div>') | ||
expect(prettyDOM(container)).toMatchInlineSnapshot(` | ||
"<div> | ||
<div> | ||
Hello World! | ||
</div> | ||
</div>" | ||
`) | ||
"<div> | ||
<div> | ||
Hello World! | ||
</div> | ||
</div>" | ||
`) | ||
}) | ||
|
||
test('supports truncating the output length', () => { | ||
test('prettyDOM supports truncating the output length', () => { | ||
const {container} = render('<div>Hello World!</div>') | ||
expect(prettyDOM(container, 5)).toMatch(/\.\.\./) | ||
}) | ||
|
||
test('supports receiving the document element', () => { | ||
test('prettyDOM defaults to document.body', () => { | ||
renderIntoDocument('<div>Hello World!</div>') | ||
expect(prettyDOM()).toMatchInlineSnapshot(` | ||
"<body> | ||
<div> | ||
Hello World! | ||
</div> | ||
</body>" | ||
`) | ||
}) | ||
|
||
test('prettyDOM supports receiving the document element', () => { | ||
expect(prettyDOM(document)).toMatchInlineSnapshot(` | ||
"<html> | ||
<head /> | ||
<body /> | ||
</html>" | ||
`) | ||
"<html> | ||
<head /> | ||
<body /> | ||
</html>" | ||
`) | ||
}) | ||
|
||
test('logDOM logs prettyDOM to the console', () => { | ||
const {container} = render('<div>Hello World!</div>') | ||
logDOM(container) | ||
expect(console.log).toHaveBeenCalledTimes(1) | ||
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(` | ||
"<div> | ||
<div> | ||
Hello World! | ||
</div> | ||
</div>" | ||
`) | ||
}) | ||
|
||
/* eslint no-console:0 */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,50 @@ | ||
import prettyFormat from 'pretty-format' | ||
import {getDocument} from './helpers' | ||
|
||
function inCypress(dom) { | ||
const window = | ||
(dom.ownerDocument && dom.ownerDocument.defaultView) || undefined | ||
return ( | ||
(typeof global !== 'undefined' && global.Cypress) || | ||
(typeof window !== 'undefined' && window.Cypress) | ||
) | ||
} | ||
|
||
const inNode = () => | ||
typeof process !== 'undefined' && | ||
process.versions !== undefined && | ||
process.versions.node !== undefined | ||
|
||
const getMaxLength = dom => | ||
inCypress(dom) ? 0 : process.env.DEBUG_PRINT_LIMIT || 7000 | ||
|
||
const {DOMElement, DOMCollection} = prettyFormat.plugins | ||
|
||
function prettyDOM(htmlElement, maxLength, options) { | ||
if (htmlElement.documentElement) { | ||
htmlElement = htmlElement.documentElement | ||
function prettyDOM( | ||
dom = getDocument().body, | ||
maxLength = getMaxLength(dom), | ||
options, | ||
) { | ||
if (maxLength === 0) { | ||
return '' | ||
} | ||
if (dom.documentElement) { | ||
dom = dom.documentElement | ||
} | ||
|
||
const debugContent = prettyFormat(htmlElement, { | ||
const debugContent = prettyFormat(dom, { | ||
plugins: [DOMElement, DOMCollection], | ||
printFunctionName: false, | ||
highlight: true, | ||
highlight: inNode(), | ||
...options, | ||
}) | ||
return maxLength !== undefined && htmlElement.outerHTML.length > maxLength | ||
return maxLength !== undefined && dom.outerHTML.length > maxLength | ||
? `${debugContent.slice(0, maxLength)}...` | ||
: debugContent | ||
} | ||
|
||
export {prettyDOM} | ||
const logDOM = (...args) => console.log(prettyDOM(...args)) | ||
|
||
export {prettyDOM, logDOM} | ||
|
||
/* eslint no-console:0 */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ export type AllByAttribute = ( | |
|
||
export const queryByAttribute: QueryByAttribute | ||
export const queryAllByAttribute: AllByAttribute | ||
export const debugDOM: (htmlElement: HTMLElement) => string | ||
export const logDOM: (htmlElement: HTMLElement) => void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the breaking change. Impact is minimal because people probably aren't relying on this for their actual tests and it's more of a workflow method (which I doubt many people use). That said, some libraries built on top of DTL may use this method, and the upgrade strategy for them is to switch from |
||
export const getElementError: (message: string, container: HTMLElement) => Error | ||
|
||
/** | ||
|
@@ -53,7 +53,7 @@ export type BuiltQueryMethods<Arguments extends any[]> = [ | |
GetAllBy<Arguments>, | ||
GetBy<Arguments>, | ||
FindAllBy<Arguments>, | ||
FindBy<Arguments> | ||
FindBy<Arguments>, | ||
] | ||
export const buildQueries: <Arguments extends any[]>( | ||
queryByAll: GetAllBy<Arguments>, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone's not running in an environment where the document is globally available, then they have to provide a DOM node to log, otherwise we'll just default to
document.body
which I think is a good default. I imagine lots of people will just calllogDOM()
and that'll be helpful to them!