How Can I Use JSDom With ES6 Module Syntax?
See original GitHub issueThis Stack Overflow describes the problem in details:
http://stackoverflow.com/questions/38651209/how-can-i-wrap-an-import-with-jsdom
But basically my question is this: jsdom has to “wrap” code to get used, but it’s impossible to “wrap” an ES6 module import (ie. import foo form 'bar'
) because they have to come before any code. And if DOM-using code is part of the import itself, it needs to be wrapped by JSDom
Is there a way around this?
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
How to test es6 module that imports jquery with jsdom
Save this answer. Show activity on this post. import {JSDOM} from 'jsdom'; import * as $ from 'jquery'; const dom = new JSDOM(`...
Read more >Using JSDOM with ES modules - Alex Kovar
window; // Execute my library by inserting a <script> tag containing it. const scriptEl = window.document.createElement("script"); scriptEl.
Read more >Building modular javascript applications in ES6 with React ...
Note that I'd used import statements for jsdom and localStorage, but used require() for React. This is because import statements are hoisted and...
Read more >Finally I can unit test my ES6 Module based browser ... - Reddit
Getting Node/Mocha to parse es6 modules was solved by calling ... way to use JSDom... but it is the way everyone seems to...
Read more >How to use ES6 import syntax in Node.js - DEV Community
How to use ES6 import syntax in Node.js ... A module is a JavaScript file that exports one or more values. The exported...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for this, @machineghost ! Question for you and any others - In a new Node.js project (NestJS, to be specific), I need something like jsdom. In 2021, it feels pretty “hacky” to have to do this. Is there a better library to use, or is jsdom still well-liked?
I was going to use jsdom, but after seeing this code, I’m inclined to give LinkeDOM a shot.
First off, thanks for the wiki link; it was a piece in the puzzle which ultimately led me to the solution (at this awesome blog: http://www.2ality.com/2014/09/es6-modules-final.html). As it explains, there is a way to do imports in a way that they can be wrapped by JSDom:
System.import
.Here’s the solution to the problem for other ES6/JSDom-lovers:
NOTE: If you’re using a test framework like Mocha, you’ll need to use it’s asynchronous test functionality to make this work (ie. you need a
done
argument in yourit
function, and then you’ll need to invokedone
after the test finishes).Anyhow, I hope that helps anyone else going down this road, and thanks again for the help in solving it.