Skip to content
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

HtmlElementTypeError for custom jsdom instance #58

Closed
sheerun opened this issue Sep 17, 2018 · 6 comments
Closed

HtmlElementTypeError for custom jsdom instance #58

sheerun opened this issue Sep 17, 2018 · 6 comments

Comments

@sheerun
Copy link
Contributor

sheerun commented Sep 17, 2018

  • jest-dom version: 1.11.0
  • node version: 10.4.1
  • npm (or yarn) version: yarn 1.7.0

Relevant code or config:

describe('Index', () => {
  it('Suggests cities to search by', async () => {
    const { window } = await next.render('/')
    expect(window.document).toHaveTextContent('Warszawa')
  })
})

next.render is a helper I've created that returns custom jsdom instance

What you did:

Tried to use toHaveTextContent matcher with custom jsdom instance

What happened:

Error is thrown:

    received value must be an HTMLElement or an SVGElement.
    Received:
      object:

Reproduction:

As above

Problem description:

Problem description:

It seems you throw HtmlElementTypeError with htmlElement instanceof HTMLElement but the element that I pass has separate of HTMLElement created along separate window object.

See:

const { window } = new JSDOM(...)
console.log(window.document instanceof Document) # => false
console.log(window.document instanceof window.Document) # => true

Suggested solution:

  • Search for Document class in scope of passed container?
  • Use constructor.name, __proto__.constructor.name, __proto__.__proto__.constructor.name etc. instead of instanceof?
@sheerun
Copy link
Contributor Author

sheerun commented Sep 17, 2018

Also, why I do it? Among others here's the quote for jsdom readme:

Note that we strongly advise against trying to "execute scripts" by mashing together the jsdom and Node global environments (e.g. by doing global.window = dom.window), and then executing scripts or test code inside the Node global environment. Instead, you should treat jsdom like you would a browser, and run all scripts and tests that need access to a DOM inside the jsdom environment, using window.eval or runScripts: "dangerously". This might require, for example, creating a browserify bundle to execute as a <script> element—just like you would in a browser.

@gnapse
Copy link
Member

gnapse commented Sep 17, 2018

Can you provide a small repo where others can reproduce the problem? Also, even better, if you can provide the fix yourself, attempting what you suggest in the suggested solution section, that'd be great.

Thanks!

@sheerun
Copy link
Contributor Author

sheerun commented Sep 17, 2018

Here you go...

npm init --force
npm install --save jsdom jest-dom

And execute following index.js file:

const { JSDOM } = require('jsdom')
const { toHaveTextContent } = require('jest-dom')
const { window } = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
console.log(window.document.body instanceof window.HTMLElement)
toHaveTextContent(window.document.body, 'Hello world')

@sheerun
Copy link
Contributor Author

sheerun commented Sep 17, 2018

I don't want to suggest any fix, I just think it should work with this method of using

@gnapse
Copy link
Member

gnapse commented Oct 3, 2018

Sorry for the delayed response.

I'm not familiar with using custom jsdom instances. So I do not feel comfortable fixing it myself. It seems you have some suggested solutions in your original issue report above. Maybe you can contribute a fix yourself?

@gnapse
Copy link
Member

gnapse commented Oct 9, 2018

🎉 This issue has been resolved in version 2.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants