jeudi 26 juillet 2018

Testing a UI library with jest + jsdom

I'm trying to figure out how to test a Web UI library that manipulates DOM, while accessing global objects window and document directly.

And I have tried to use the combination of jest + jsdom:

const jsdom = require('jsdom');
const {JSDOM} = jsdom;

const dom = new JSDOM('<!DOCTYPE html><p>Hello world</p>');

// global.window = dom.window;
// global.document = dom.window.document;

test('can search the DOM', () => {
    // this doesn't work
    expect(global.document.querySelectorAll('p').length).toBe(1);
});

How can I make this DOM available inside the library, so it can be tested?

Or am I doing something entirely wrong here?

Aucun commentaire:

Enregistrer un commentaire