Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

working node-qunit tests

  • Loading branch information...
commit 692b119d4ffb4b3b3b398e4a503e6b7939a271cd 1 parent 1693966
@bahmutov authored
View
15 Gruntfile.js
@@ -3,7 +3,8 @@ module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
- datetime: Date.now(),
+ //datetime: Date.now(),
+
jshint: {
options: {
jshintrc: '.jshintrc',
@@ -12,8 +13,17 @@ module.exports = function (grunt) {
src: [ '*.js' ]
}
},
+
qunit: {
all: ['index.html']
+ },
+
+ 'node-qunit': {
+ all: {
+ deps: './qunit-promises.js',
+ code: './test/node-tests.js',
+ tests: './test/node-tests.js'
+ }
}
});
@@ -21,6 +31,7 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-bump');
grunt.loadNpmTasks('grunt-deps-ok');
grunt.loadNpmTasks('grunt-contrib-qunit');
+ grunt.loadNpmTasks('grunt-node-qunit');
- grunt.registerTask('default', ['deps-ok', 'jshint', 'qunit']);
+ grunt.registerTask('default', ['deps-ok', 'jshint', 'node-qunit', 'qunit']);
};
View
2  index.html
@@ -19,6 +19,6 @@
<!-- include qunit first, then qunit-promises, then tests -->
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="qunit-promises.js"></script>
- <script src="tests.js"></script>
+ <script src="test/tests.js"></script>
</body>
</html>
View
4 package.json
@@ -8,7 +8,9 @@
"grunt-bump": "~0.0.11",
"grunt-contrib-jshint": "~0.6.4",
"grunt-contrib-qunit": "~0.2.2",
- "grunt-deps-ok": "~0.1.2"
+ "grunt-deps-ok": "~0.1.2",
+ "grunt-node-qunit": "~2.0.2",
+ "q": "~0.9.7"
},
"devDependencies": {},
"scripts": {
View
38 qunit-promises.js
@@ -1,4 +1,6 @@
(function (QUnit) {
+ // jQuery promise objects have .then and .always methods
+ // Q promise objects have .then and .finally methods
function verifyPromise(promise) {
if (!promise) {
throw new Error('expected a promise object');
@@ -6,67 +8,79 @@
if (typeof promise.then !== 'function') {
throw new Error('promise object does not have .then function');
}
- if (typeof promise.always !== 'function') {
- throw new Error('promise object does not have .always function');
+ if (typeof promise.always !== 'function' &&
+ typeof promise.finally !== 'function') {
+ throw new Error('promise object does not have .always or .finally');
+ }
+ return alwaysName(promise);
+ }
+
+ function alwaysName(promise) {
+ if (typeof promise.always === 'function') {
+ return 'always';
+ } else if (typeof promise.finally === 'function') {
+ return 'finally';
+ } else {
+ throw new Error('promise object does not have "always" method');
}
}
QUnit.extend(QUnit.assert, {
// resolved promises
will: function (promise, message) {
- verifyPromise(promise);
+ var always = verifyPromise(promise);
QUnit.stop();
promise.then(function () {
QUnit.push(true, undefined, undefined, message);
}, function () {
QUnit.push(false, undefined, undefined, 'promise rejected (but should have been resolved)');
- }).always(QUnit.start);
+ })[always](QUnit.start);
},
willEqual: function (promise, expected, message) {
- verifyPromise(promise);
+ var always = verifyPromise(promise);
QUnit.stop();
promise.then(function (actual) {
QUnit.push(actual == expected, actual, expected, message);
}, function (actual) {
QUnit.push(false, actual, expected, 'promise rejected (but should have been resolved)');
- }).always(QUnit.start);
+ })[always](QUnit.start);
},
willDeepEqual: function (promise, expected, message) {
- verifyPromise(promise);
+ var always = verifyPromise(promise);
QUnit.stop();
promise.then(function (actual) {
QUnit.push(QUnit.equiv(actual, expected), actual, expected, message);
}, function (actual) {
QUnit.push(false, actual, expected, 'promise rejected (but should have been resolved)');
- }).always(QUnit.start);
+ })[always](QUnit.start);
},
// rejected promises
wont: function (promise, message) {
- verifyPromise(promise);
+ var always = verifyPromise(promise);
QUnit.stop();
promise.then(function () {
QUnit.push(false, undefined, undefined, 'promise resolved (but should have been rejected)');
}, function () {
QUnit.push(true, undefined, undefined, message);
- }).always(QUnit.start);
+ })[always](QUnit.start);
},
wontEqual: function (promise, expected, message) {
- verifyPromise(promise);
+ var always = verifyPromise(promise);
QUnit.stop();
promise.then(function (actual) {
QUnit.push(false, actual, expected, 'promise resolved (but should have been rejected)');
}, function (actual) {
QUnit.push(actual == expected, actual, expected, message);
- }).always(QUnit.start);
+ })[always](QUnit.start);
}
});
}(QUnit));
View
26 test/node-tests.js
@@ -0,0 +1,26 @@
+var Q = require('q');
+
+QUnit.module('a test module');
+
+QUnit.test('first test', function (assert) {
+ assert.ok(true, 'true is ok');
+});
+
+QUnit.test('second test', 1, function (assert) {
+ assert.equal(1, "1", '1 == "1"');
+});
+
+QUnit.test('qunit-promises is present', 1, function (assert) {
+ assert.equal(typeof assert.will, 'function', 'assert.will is there');
+});
+
+function delayedFoo() {
+ var deferred = Q.defer();
+ setTimeout(function () { deferred.resolve('foo'); }, 100);
+ return deferred.promise;
+}
+
+QUnit.test('example promise test', function (assert) {
+ assert.will(delayedFoo(), 'promise is resolved');
+ assert.willEqual(delayedFoo(), 'foo', 'promise is resolved with correct value');
+});
View
0  tests.js → test/tests.js
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.