javascript - Using jsdom and Jest - Stack Overflow

I'm experimenting with Jest to unit test an angular application. In order to test the angular bind

I'm experimenting with Jest to unit test an angular application. In order to test the angular bindings I'm trying to use jsdom (N.B. I'm using v3.1.2 as I'm using node not IO). When I need to load a script with the html the test seems to plete before the script is loaded.

I've simplified my test case to use an example from jsdom:

it('updates the view without error', function(){
    var jsdom = require('../../../../node_modules/jsdom/lib/jsdom');

    jsdom.env(
      '<p><a class="the-link" href="">jsdom!</a></p>',
      [".js"],
      function (errors, window) {
        //console.log("contents of a.the-link:", window.$("a.the-link").text());
        console.log("Completed");
        expect(errors).toBeNull();
      }
    );

    console.log('exiting...');
});

If I run this test, the test will pass but the "Completed" log message will not be printed, I can also replace the expect with something that should obviously fail, like expect(false).toBeTruthy() and the test will still "pass". If I remove the injection of jquery then all works as expected.

How should I make sure that the script is loaded before I exit the test? And more generally, using jsdom explicitly with Jest feels a bit wrong. Is there a better way?

I'm experimenting with Jest to unit test an angular application. In order to test the angular bindings I'm trying to use jsdom (N.B. I'm using v3.1.2 as I'm using node not IO). When I need to load a script with the html the test seems to plete before the script is loaded.

I've simplified my test case to use an example from jsdom:

it('updates the view without error', function(){
    var jsdom = require('../../../../node_modules/jsdom/lib/jsdom');

    jsdom.env(
      '<p><a class="the-link" href="https://github./tmpvar/jsdom">jsdom!</a></p>',
      ["http://code.jquery./jquery.js"],
      function (errors, window) {
        //console.log("contents of a.the-link:", window.$("a.the-link").text());
        console.log("Completed");
        expect(errors).toBeNull();
      }
    );

    console.log('exiting...');
});

If I run this test, the test will pass but the "Completed" log message will not be printed, I can also replace the expect with something that should obviously fail, like expect(false).toBeTruthy() and the test will still "pass". If I remove the injection of jquery then all works as expected.

How should I make sure that the script is loaded before I exit the test? And more generally, using jsdom explicitly with Jest feels a bit wrong. Is there a better way?

Share Improve this question asked Mar 2, 2015 at 14:32 Andrew JonesAndrew Jones 1,0011 gold badge13 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Because .env is probably asynchronous, the test will always exist before the callback is called.

According to the jest tutorial, you can simply assign the HTML to document.body.innerHTML:

// Set up our document body
document.body.innerHTML =
    '<div>' +
    ' <span id="username" />' +
    ' <button id="button" />' +
    '</div>';
var $ = require('jquery');

Alternatively you could use pit instead of it and return a promise from the function.

I think the best way to test asynchronous code is to pass the 'done' callback as explained in the docs. Your code would then bee:

it('updates the view without error', function(done){
    var jsdom = require('../../../../node_modules/jsdom/lib/jsdom');

    jsdom.env(
      '<p><a class="the-link" href="https://github./tmpvar/jsdom">jsdom!</a></p>',
      ["http://code.jquery./jquery.js"],
      function (errors, window) {
        //console.log("contents of a.the-link:", window.$("a.the-link").text());
        console.log("Completed");
        expect(errors).toBeNull();
        done();
      }
    );

    console.log('exiting...');
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744625794a4584570.html

相关推荐

  • javascript - Using jsdom and Jest - Stack Overflow

    I'm experimenting with Jest to unit test an angular application. In order to test the angular bind

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信