您实际上可以在 nodeunit 本身内测试隔离的 jQuery 逻辑。例如,这是我为我的 datepicker 库编写的一些测试:
/* jshint evil:true */
/* global module, require, DatePair */
var fs = require('fs');
// Some of the plugins use a jQuery variable instead of $
var jQuery = require('jquery');
var $ = jQuery;
// Recreate window and document
var window = { jQuery: jQuery };
var document = window;
// The file is included here:
eval(fs.readFileSync('../lib/js/jquery/plugins/jquery.cookie.js') + '');
eval(fs.readFileSync('../lib/js/jquery.timepicker/jquery.timepicker.js') + '');
eval(fs.readFileSync('../lib/js/bootstrap/bootstrap-datepicker.js') + '');
eval(fs.readFileSync('../homepage/js/datepair.js') + '');
// Initialize DatePair Object
var datePair = DatePair(new Date(), {
dateStart: $('<input type="text" autocomplete="off">'),
dateEnd: $('<input type="text" autocomplete="off">'),
timeStart: $('<input type="text" autocomplete="off">'),
timeEnd: $('<input type="text" autocomplete="off">')
});
module.exports = {
'Time Delta Var Is 1 hour' : function (test) {
test.equal(datePair.getTimeDelta(), 3600);
test.done();
},
'Change Date And Check For 1 Hour Difference' : function (test) {
datePair.startTime.val("10:00pm").change();
test.equal(datePair.endTime.val(), "11:00pm");
test.done();
},
'Make End Date Move Up One Day' : function (test) {
var expectedEndDates = [1];
datePair.startTime.val("11:00pm").change();
expectedEndDates.push(parseInt(datePair.startDate.val().split("/")[1], 10) + 1);
test.ok($.inArray(parseInt(datePair.endDate.val().split("/")[1], 10), expectedEndDates) !== -1);
test.equal(datePair.endTime.val(), "12:00am");
test.done();
},
'Change To 11:30pm Of Previous Day' : function (test) {
// Move startTime 1 Day Forward
datePair.startTime.val("11:00pm").change();
// Move EndDate 1 Day Backwards
datePair.endTime.val("11:30pm").change();
test.equal(datePair.endDate.val(), datePair.startDate.val());
test.done();
}
}
在此处查看完整的代码 + 库:https ://github.com/keithhackbarth/jquery-datetime-picker