如何在浏览器中测试jQuery 3.0 beta是否与Promises/A +兼容? [英] How to test if jQuery 3.0 beta is Promises/A+ compatible in browser?

查看:83
本文介绍了如何在浏览器中测试jQuery 3.0 beta是否与Promises/A +兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据已发布jQuery 3.0 Beta

jQuery.Deferred现在与Promises/A +兼容 jQuery.Deferred对象已更新,以与Promises/A +和ES2015 Promises兼容,并已通过 Promises/A +进行了验证符合性测试套件.

jQuery.Deferred is now Promises/A+ compatible jQuery.Deferred objects have been updated for compatibility with Promises/A+ and ES2015 Promises, verified with the Promises/A+ Compliance Test Suite.


如何运行

测试可以在Node.js环境中运行,或者如果您设置了 在浏览器中正确显示.

The tests can run in either a Node.js environment or, if you set things up correctly, in the browser.

如何在没有nodejs进行验证的情况下在浏览器中运行测试?

How to run the tests in browser, without nodejs to verify ?

注意,没有nodejs的浏览器尚未完成运行测试. @JaredSmith的协助对于完全使用nodejs运行测试至关重要.

Note, have not yet achieved running tests in browser without nodejs. @JaredSmith 's assistance was essential to running tests at all using nodejs.

推荐答案

注释中引用的适配器意味着适应jQuery延迟构造函数以符合此处):

The adapter referenced in the comments means adapting the jQuery deferred constructor to meet the API in the spec. The way to do so might be as follows (taken more or less from here):

var promisesAplusTests = require("promises-aplus-tests");
var jq                 = require('jquery');
var jsdom              = require('jsdom');
jsdom.env('<p></p>', function(err, window) {
    if (err != null) {
        throw err;
    } else {
        var $ = jq(window);
        var adapter = {};
        adapter.deferred = function() {
            var deferred = $.Deferred();

            return {
                promise: deferred.promise(),
                resolve: deferred.resolve.bind( deferred ),
                reject: deferred.reject.bind( deferred )
            };
        };
        promisesAplusTests(adapter, function (err) {
            // All done; output is in the console. 
            // Or check `err` for number of failures:
            if (err) {
                console.log(err);
            }
        });
    }
});

此时,您需要通过npm安装依赖项并运行文件.请注意,使用jQuery 需要一个正确的文档(因此需要jsdom).

At which point you need to install the dependencies via npm and run the file. Note that using jquery requires a proper document (hence the need for jsdom).

或者您可以采用简单的路线:

Or you could take the easy route:

  • git克隆jquery仓库
  • cd jquery目录
  • npm安装
  • 永远等待它完成,忽略所有警告
  • npm test-运行jquery单元测试(包括A +套件)

尽管规范自述文件中有glib注释,但在浏览器中运行仍会涉及一些工作.您将需要使用commonJS模块加载器,例如browserify. Browserify将填充本机节点断言库,并且我相信文件系统api. Mocha在浏览器中应该可以正常工作.然后,上面的代码应运行.

Running in the browser, despite the glib comment in the spec readme, will involve some of work. You will need to use a commonJS module loader like browserify. Browserify will shim the native node assertion library and I believe filesystem api. Mocha should work fine in the browser. Then the above code should run.

如果您想完全避免使用node.js(而不仅仅是为了运行测试) ,您将有更多工作要做.

If you want to avoid node.js entirely (and not just for running the tests) , you'll have more work to do.

步骤1.找到或编写兼容的断言库,并将其包含在脚本标签中.

Step 1. Find or write a compatible assertion library and include it in a script tag.

第2步.在脚本标签中添加摩卡咖啡.

Step 2. Include mocha in a script tag.

第3步.编写自己的桩,用于require,assert,fs等.每个桩本身就是一个问题.

Step 3. Write your own stubs for require, assert, fs, etc. Each of those is a question in its own right.

第4步.使用适配器代码.

Step 4. Use the adapter code.

为了避免结点,是否值得做所有这一切,都是您的电话.

Whether its worth doing all that or not to avoid node is your call.

这篇关于如何在浏览器中测试jQuery 3.0 beta是否与Promises/A +兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆