Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the use cases of jsdom

After reading this Micro templates are dead article. I've become curious:

  1. Whether Using the DOM on the server results in cleaner more maintainable code then templating.
  2. Whether it's more efficient to use jsdom instead of a templating engine.
  3. How to factor jsdom into the View of a standard MVC setup.

And generally in what situations would it be better to use a server-side DOM abstraction, like jsdom rather then a templating engine, like EJS or jade.

The question is specific to node.js and other SSJS

like image 944
Raynos Avatar asked May 23 '11 19:05

Raynos


People also ask

What is Jsdom used for?

JSDOM is a library which parses and interacts with assembled HTML just like a browser. The benefit is that it isn't actually a browser. Instead, it implements web standards like browsers do. You can feed it some HTML, and it will parse that HTML.

What is Jsdom in jest?

jsdom is a pure JavaScript implementation of the DOM and browser APIs that runs in node. If you're not using Jest and you would like to run your tests in Node, then you must install jsdom yourself. There's also a package called global-jsdom which can be used to setup the global environment to simulate the browser APIs.

Does enzyme use Jsdom?

JSDOM is a JavaScript based headless browser that can be used to create a realistic testing environment. Since enzyme's mount API requires a DOM, JSDOM is required in order to use mount if you are not already in a browser environment (ie, a Node environment).

Does Jsdom include Fetch?

jsdom/Nodejs is doing the fetching whenever the JavaScript is doing an implicit fetch. However, when passed into the JSDOM constructor, within the source string or even nested files within that were implicitly fetched within any JavaScript file, explicit fetch is not supported.


1 Answers

Well, I actually needed JSDom for a small project I built over the weekend in node.js. So, on my server, I had to accept a URL to fetch, grab all of the HTML from the given URL, parse it, and display images to the user so that the user could select a thumbnail from that URL. (Kind of like when you drop a link into the Facebook input box) So, I used a module called Request which allows me to fetch HTML on the server-side. However, when that HTML reached my program, I had no way to traverse it like you do with client-side javascript. Because there was no actual DOM, I couldn't say document.getElementById('someId'). Therefore, JSDom came in handy by giving me a "makeshift" DOM that allowed me to traverse the returned HTML. Now, even though I was still on the server side, JSDOM created a window object very similar to the window object in the browser, and created a DOM out of the returned HTML. Now, even on the server, I was able to get all images by calling window.$('img'). I could target and parse the elements like normal. So, this is just one problem where JSDom turned out to be the solution, but it worked amazingly well. Hope this helps some!

like image 128
Hacknightly Avatar answered Sep 26 '22 18:09

Hacknightly