Skip to content

prettyDOM error when container is document #44

Closed
@npeterkamps

Description

@npeterkamps
  • dom-testing-library version: 2.3.2
  • cypress-testing-library version: 2.0.0
  • cypress version: 2.1.0
  • react version: 16.3.2
  • node version: 8.4.0
  • npm version: 5.3.0

Relevant code or config:

cy.getByLabelText("something not on the page");

What you did:

I'm selecting an element using getByLabelText, but my code isn't setting the htmlFor correctly, so it couldn't find it.

What happened:

  1. cypress-testing-library passes the window.document as container to the query.
  2. Since getByLabelText can't find the element, it wants to log a friendly error message using prettyDOM.
  3. prettyDOM uses outerHTML.length to determine whether to truncate. However, document.outerHTML is undefined resulting in the error.
TypeError: Cannot read property 'length' of undefined
    at prettyDOM (http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:898:59)
    at debugDOM (http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:920:35)
    at getAllByLabelText (http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:1181:216)
    at firstResultOrNull (http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:932:30)
    at getByLabelText (http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:1194:28)
    at container (http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:82:28)
    at onMutation (http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:1309:22)
    at http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:1327:7
    at new Promise (<anonymous>)
    at waitForElement (http://localhost:3001/__cypress/tests?p=cypress\support\index.js-147:1289:10)
From previous event:
    at Context.thenFn (http://localhost:3001/__cypress/runner/cypress_runner.js:56945:26)
    at Context.then (http://localhost:3001/__cypress/runner/cypress_runner.js:57207:21)
    at Context.<anonymous> (http://localhost:3001/__cypress/runner/cypress_runner.js:63338:21)
    at http://localhost:3001/__cypress/runner/cypress_runner.js:63053:33
From previous event:
    at runCommand (http://localhost:3001/__cypress/runner/cypress_runner.js:63043:14)
    at next (http://localhost:3001/__cypress/runner/cypress_runner.js:63125:14)
    at http://localhost:3001/__cypress/runner/cypress_runner.js:63137:18

Reproduction:

I've spent a little bit of time to try and test this, but with JSDOM, this doesn't seem to be easy. prettyDOM(document) crashes before it hits the outerHTML.length part due to pretty-format package (RangeError: Invalid string length).

Full `pretty-format` error

  RangeError: Invalid string length
      at printListItems (node_modules/pretty-format/build/collections.js:105:33)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printListItems (node_modules/pretty-format/build/collections.js:105:35)
      at printComplexValue (node_modules/pretty-format/build/index.js:176:141)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)
      at printComplexValue (node_modules/pretty-format/build/index.js:187:169)
      at printer (node_modules/pretty-format/build/index.js:236:10)
      at printObjectProperties (node_modules/pretty-format/build/collections.js:140:21)


I believe this can be reproduced by just using cypress-testing-library and selecting something that isn't on the page? I'll do some more testing in the weekend.

Problem description:

prettyDOM function shouldn't assume outerHTML is defined.

Maybe a more accurate description would be: I don't know how to write a test case for this within dom-testing-library to test a pull request.

Suggested solution:

I'm not sure why outerHTML is being used, since it's the debugContent that is being truncated. So my suggestion would be to check debugContent.length instead. Otherwise add an && htmlContent.outerHTML before the length check.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions