Skip to content

Commit fd0c18c

Browse files
npeterkampsKent C. Dodds
authored and
Kent C. Dodds
committed
feat(logging): don't log DOM in Cypress, don't highlight in the browser (#45)
* fix: don't crash when jsdom's document is passed to prettyDOM. * feat(logging): don't log DOM in Cypress, don't highlight in the browser. - added test to check whether Cypress doesn't log DOM - ignore .idea folder (JetBrains IDE's) - added myself to contributors * fix: `.control` support landed in jsdom, updated istanbul ignore comments for improved coverage. * Updated error message code style. Updated istanbul ignore. * Improved error handling code style. * Updated environment check.
1 parent b9f7597 commit fd0c18c

10 files changed

+115
-66
lines changed

.all-contributorsrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@
209209
"contributions": [
210210
"code"
211211
]
212+
},
213+
{
214+
"login": "npeterkamps",
215+
"name": "Peter Kamps",
216+
"avatar_url": "https://avatars1.githubusercontent.com/u/25429764?v=4",
217+
"profile": "https://github.com/npeterkamps",
218+
"contributions": [
219+
"bug",
220+
"code",
221+
"test"
222+
]
212223
}
213224
]
214225
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dist
66
.DS_Store
77
.eslintcache
88
yarn-error.log
9+
.idea/
910

1011
# these cause more harm than good
1112
# when working with contributors

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-20-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-21-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -857,13 +857,11 @@ light-weight, simple, and understandable.
857857
Thanks goes to these people ([emoji key][emojis]):
858858

859859
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
860-
861860
<!-- prettier-ignore -->
862861
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
863862
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
864863
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Tests") |
865-
| [<img src="https://avatars3.githubusercontent.com/u/725236?v=4" width="100px;"/><br /><sub><b>Alex Cook</b></sub>](https://github.com/alecook)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [💡](#example-alecook "Examples") | [<img src="https://avatars3.githubusercontent.com/u/10348212?v=4" width="100px;"/><br /><sub><b>Daniel Cook</b></sub>](https://github.com/dfcook)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Tests") | [<img src="https://avatars2.githubusercontent.com/u/21194045?s=400&v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [<img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;"/><br /><sub><b>Tim Deschryver</b></sub>](https://github.com/tdeschryver)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") |
866-
864+
| [<img src="https://avatars3.githubusercontent.com/u/725236?v=4" width="100px;"/><br /><sub><b>Alex Cook</b></sub>](https://github.com/alecook)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [💡](#example-alecook "Examples") | [<img src="https://avatars3.githubusercontent.com/u/10348212?v=4" width="100px;"/><br /><sub><b>Daniel Cook</b></sub>](https://github.com/dfcook)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Tests") | [<img src="https://avatars2.githubusercontent.com/u/21194045?s=400&v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [<img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;"/><br /><sub><b>Tim Deschryver</b></sub>](https://github.com/tdeschryver)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars1.githubusercontent.com/u/25429764?v=4" width="100px;"/><br /><sub><b>Peter Kamps</b></sub>](https://github.com/npeterkamps)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Anpeterkamps "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Tests") |
867865
<!-- ALL-CONTRIBUTORS-LIST:END -->
868866

869867
This project follows the [all-contributors][all-contributors] specification.

src/__tests__/__snapshots__/element-queries.js.snap

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,77 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`get throws a useful error message 1`] = `
4-
"Unable to find a label with the text of: LucyRicardo
4+
"Unable to find a label with the text of: LucyRicardo
55
66
<div>
77
<div />
88
</div>"
99
`;
1010

1111
exports[`get throws a useful error message 2`] = `
12-
"Unable to find an element with the placeholder text of: LucyRicardo
12+
"Unable to find an element with the placeholder text of: LucyRicardo
1313
1414
<div>
1515
<div />
1616
</div>"
1717
`;
1818

1919
exports[`get throws a useful error message 3`] = `
20-
"Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
20+
"Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
2121
2222
<div>
2323
<div />
2424
</div>"
2525
`;
2626

2727
exports[`get throws a useful error message 4`] = `
28-
"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]
28+
"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]
2929
3030
<div>
3131
<div />
3232
</div>"
3333
`;
3434

3535
exports[`get throws a useful error message 5`] = `
36-
"Unable to find an element with the alt text: LucyRicardo
36+
"Unable to find an element with the alt text: LucyRicardo
3737
3838
<div>
3939
<div />
4040
</div>"
4141
`;
4242

4343
exports[`get throws a useful error message 6`] = `
44-
"Unable to find an element with the title: LucyRicardo.
44+
"Unable to find an element with the title: LucyRicardo.
4545
4646
<div>
4747
<div />
4848
</div>"
4949
`;
5050

5151
exports[`get throws a useful error message 7`] = `
52-
"Unable to find an element with the value: LucyRicardo.
52+
"Unable to find an element with the value: LucyRicardo.
5353
5454
<div>
5555
<div />
5656
</div>"
5757
`;
5858

59+
exports[`get throws a useful error message without DOM in Cypress 1`] = `"Unable to find a label with the text of: LucyRicardo"`;
60+
61+
exports[`get throws a useful error message without DOM in Cypress 2`] = `"Unable to find an element with the placeholder text of: LucyRicardo"`;
62+
63+
exports[`get throws a useful error message without DOM in Cypress 3`] = `"Unable to find an element with the text: LucyRicardo. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible."`;
64+
65+
exports[`get throws a useful error message without DOM in Cypress 4`] = `"Unable to find an element by: [data-testid=\\"LucyRicardo\\"]"`;
66+
67+
exports[`get throws a useful error message without DOM in Cypress 5`] = `"Unable to find an element with the alt text: LucyRicardo"`;
68+
69+
exports[`get throws a useful error message without DOM in Cypress 6`] = `"Unable to find an element with the title: LucyRicardo."`;
70+
71+
exports[`get throws a useful error message without DOM in Cypress 7`] = `"Unable to find an element with the value: LucyRicardo."`;
72+
5973
exports[`label with no form control 1`] = `
60-
"Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
74+
"Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
6175
6276
<div>
6377
<label>
@@ -67,7 +81,7 @@ exports[`label with no form control 1`] = `
6781
`;
6882

6983
exports[`totally empty label 1`] = `
70-
"Found a label with the text of: , however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
84+
"Found a label with the text of: , however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
7185
7286
<div>
7387
<label />

src/__tests__/__snapshots__/pretty-dom.js.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ exports[`it prints out the given DOM element tree 1`] = `
77
</div>
88
</div>"
99
`;
10+
11+
exports[`it supports receiving the document element 1`] = `
12+
"<html>
13+
<head />
14+
<body />
15+
</html>"
16+
`;

src/__tests__/__snapshots__/wait-for-element.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports[`it throws if timeout is exceeded 2`] = `
2424

2525
exports[`it throws the same error that the callback has thrown if timeout is exceeded 1`] = `
2626
Array [
27-
[Error: Unable to find an element by: [data-testid="test"]
27+
[Error: Unable to find an element by: [data-testid="test"]
2828
2929
<div
3030
data-test-attribute="something changed twice"

src/__tests__/element-queries.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import 'jest-dom/extend-expect'
22
import {render} from './helpers/test-utils'
33

4+
beforeEach(() => {
5+
window.Cypress = null;
6+
});
7+
48
test('query can return null', () => {
59
const {
610
queryByLabelText,
@@ -368,4 +372,26 @@ test('test the debug helper prints the dom state here', () => {
368372
process.env.DEBUG_PRINT_LIMIT = originalDebugPrintLimit
369373
})
370374

375+
test('get throws a useful error message without DOM in Cypress', () => {
376+
window.Cypress = {}
377+
const {
378+
getByLabelText,
379+
getByPlaceholderText,
380+
getByText,
381+
getByTestId,
382+
getByAltText,
383+
getByTitle,
384+
getByValue,
385+
} = render('<div />')
386+
expect(() => getByLabelText('LucyRicardo')).toThrowErrorMatchingSnapshot()
387+
expect(() =>
388+
getByPlaceholderText('LucyRicardo'),
389+
).toThrowErrorMatchingSnapshot()
390+
expect(() => getByText('LucyRicardo')).toThrowErrorMatchingSnapshot()
391+
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingSnapshot()
392+
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingSnapshot()
393+
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingSnapshot()
394+
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingSnapshot()
395+
})
396+
371397
/* eslint jsx-a11y/label-has-for:0 */

src/__tests__/pretty-dom.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ test('it supports truncating the output length', () => {
1010
const {container} = render('<div>Hello World!</div>')
1111
expect(prettyDOM(container, 5)).toMatch(/\.\.\./)
1212
})
13+
14+
test('it supports receiving the document element', () => {
15+
expect(prettyDOM(document)).toMatchSnapshot()
16+
})

src/pretty-dom.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import prettyFormat from 'pretty-format'
22

33
const {DOMElement, DOMCollection} = prettyFormat.plugins
44

5-
function prettyDOM(htmlElement, maxLength) {
6-
const debugContent = prettyFormat(htmlElement, {
5+
function prettyDOM(htmlElement, maxLength, options) {
6+
if (htmlElement.documentElement) {
7+
htmlElement = htmlElement.documentElement
8+
}
9+
10+
const debugContent = prettyFormat(htmlElement, Object.assign({
711
plugins: [DOMElement, DOMCollection],
812
printFunctionName: false,
913
highlight: true,
10-
})
14+
}, options))
1115
return maxLength !== undefined && htmlElement.outerHTML.length > maxLength
1216
? `${debugContent.slice(0, maxLength)}...`
1317
: debugContent

0 commit comments

Comments
 (0)