Preact: Make preact support jsdom

Created on 6 Feb 2017  路  6Comments  路  Source: preactjs/preact

I'm working on fixing a test suite for React so it works with Preact. The original suite uses jsDom, but with Preact it gave some errors. The errors are caused by Preact assuming some globals exist and it can be easily fixed in userland code, like this:

jsdom.env('<!doctype html><html><head></head><body></body></html>', (error, window) => {
  if (!error) {
    global.window = window;
    global.document = window.document;
    // preact tests for SVGElement, but jsdom does not support it
    // https://github.com/tmpvar/jsdom/issues/1423
    global.SVGElement = function(){};
    // Preact tests for Text, supported by jsdom
    global.Text = window.Text
  }

  done(error);
});

Still'I think it may be worth it to safeguard these tests. I.s.o:

if (x instanceof SVGElement)

write:

if (typeof SVGElement == 'function' && (x instanceof SVGElement))

It's a few more bytes but I think being compatible out of the box with jsDom would be a good thing for Preact.

Ideas?

beginner-friendly has fix

Most helpful comment

Good news, the Text global is dropped in 8.0. Hoping to have a beta out this weekend :)

All 6 comments

Hey @Download - I actually removed the dependency on SVGElement in [email protected]. It's beta though, so npm i -S preact@beta.

As for Text, I'm looking into that now. It's the same type of check as SVGElement, so I might end up switching from node instanceof Text to typeof node.wholeText!=='undefined.

Good news, the Text global is dropped in 8.0. Hoping to have a beta out this weekend :)

Great job @developit !
Thanks a lot!

was wondering why i was getting Text is undefined... glad I found this issue (which led me to adding global.Text = win.Text in my test / jsdom config.

@peter-mouland Good to hear - setting that constructor will keep working in 8.0, just it won't be necessary anymore :)

Fixed in 8.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SabirAmeen picture SabirAmeen  路  3Comments

philipwalton picture philipwalton  路  3Comments

kay-is picture kay-is  路  3Comments

matthewmueller picture matthewmueller  路  3Comments

jescalan picture jescalan  路  3Comments