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.

Brainstorming: what would a modernized jsdom API look like?

See original GitHub issue

I feel like the current API is not the most user-friendly. Here are a few of my complaints:

  • The lack of clear default export (require("jsdom") doing something useful)
  • The split between env and jsdom, plus the under-loved jQuerify cousin
  • jQuerify is a lot less convenient now that we require an explicit jQuery URL
  • The util methods (createVirtualConsole etc.) are kind of sprouting everywhere
  • env’s magic is often a bit too magical, especially the auto-detection of strings
  • The document-centric interface is awkward; often you want a window
  • The different default settings between env and jsdom is confusing
  • The “features” configuration is a bit crazy

I think as a prerequisite to any redesign, we’d need to fix:

  • #1039 so we don’t have the weird mix of error types during initialization (and in particular never have more than one “error”)
  • #1128 so that we know what we’re doing with options (although maybe we’d fix this at the same time as doing this work)

If we got those down, here are some broad ideas on what I could imagine:

  • A new type of class, Jsdom, which has stuff like:
    • A window getter
    • virtualConsole and cookieJar getters (which would just return the one you passed in, if you did)
    • A serialize() method
    • Other meta stuff? Maybe promises for when everything loads?
  • A new set of top-level APIs:
    • jsdom: takes a HTML string and returns a Jsdom
    • jsdom.fromUrl: takes a URL and returns a Promise<Jsdom>
    • jsdom.fromFile: takes a filename and returns a Promise<Jsdom>
    • Each should take args in the form (relevantString, commonOptions, typeSpecificOptions)
    • commonOption defaults are all the same
    • current features stuff is rolled up into commonOptions (or possibly into resourceLoader?)

Alternately I was considering a “builder” class instead of a Jsdom class, which would allow things like .insertScript() to get back that jsdom.env/jsdom.jQueryify functionality, and other nicer methods… might be worth sketching that out too. We could put a promise-returning insertScript on Jsdom.prototype though perhaps.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:34 (32 by maintainers)

github_iconTop GitHub Comments

1reaction
domeniccommented, May 30, 2016

The tentative idea is to provide a dom.loaded promise, as described at the bottom of the document. jsdom() is not synchronous for any external resources.

1reaction
domeniccommented, May 29, 2016

That doesn’t really seem to have anything to do with the new jsdom API, and is instead some kind of jsdom feature request that it support shadow DOM and custom elements and CSP?

Read more comments on GitHub >

github_iconTop Results From Across the Web

jsdom - UNPKG
As such, we are doing a major release with them removed, so that jsdom ... where we are brainstorming a modernized jsdom API...
Read more >
@forbeslindesay/jsdom | Yarn - Package Manager
jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js.
Read more >
Proof of concept for implementing frontend integration tests
The frontend testing guide mentions frontend integration tests as a testing level between unit tests and system tests (spec/features/).
Read more >
Boyan Georgiev / jsdom-with-fix | GitLab
Previously, it would treat them as `text/html`. ... where we are brainstorming a modernized jsdom API that could get rid of many of...
Read more >
Modern APIs: Building and Evolving - QCon San Francisco
A modern cloud operating model is API-driven, but what does that really look like? Should you use GraphQL or ReST?
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