question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How Can I Use JSDom With ES6 Module Syntax?

See original GitHub issue

This 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:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
rinogocommented, Nov 19, 2021

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.

4reactions
machineghostcommented, Jul 29, 2016

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:

import jsdom;

jsdom({
    html: '<div></div>',
    done: () => {
        // don't do this:
        //      import setup from 'setup';

        // do this:
        System.import('setup')
            .then((setup) => {
                    // test code that uses setup
            })
    }
});

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 your it function, and then you’ll need to invoke done after the test finishes).

Anyhow, I hope that helps anyone else going down this road, and thanks again for the help in solving it.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found