Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

javascript - How to test if jQuery 3.0 beta is Promises/A+ compatible in browser?

According to jQuery 3.0 Beta Released

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.


How To Run

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

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


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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
            }
        });
    }
});

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 clone the jquery repo
  • cd jquery directory
  • npm install
  • wait forever for it to finish, ignore all warnings
  • npm test -which runs the jquery unit tests (including A+ suite)

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.

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

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

Step 2. Include mocha in a script tag.

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

Step 4. Use the adapter code.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...