From a3d2fb7aa95bc100e1c1d88d18272d13b47fedee Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sun, 26 May 2013 19:41:05 -0400 Subject: [PATCH 1/5] adding mocha, getting npm test working --- package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ecd589610f..9210557905 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "start": "node app", - "test": "nodeunit core/test/ghost" + "test": "mocha core/test/ghost" }, "dependencies": { "express": "3.1.x", @@ -28,6 +28,7 @@ "grunt-mocha-test": "~0.4.0", "grunt-shell": "~0.2.2", "grunt-contrib-sass": "~0.3.0", - "sinon": "~1.7.2" + "sinon": "~1.7.2", + "mocha": "~1.10.0" } } From 3446fe746130ee2949b1e55b6fe104c307bbaa49 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sun, 26 May 2013 19:53:44 -0400 Subject: [PATCH 2/5] using promises correctly in tests --- core/test/ghost/api_posts_spec.js | 74 +++++++---------- core/test/ghost/api_settings_spec.js | 117 ++++++++++++--------------- core/test/ghost/api_users_spec.js | 41 +++++----- 3 files changed, 100 insertions(+), 132 deletions(-) diff --git a/core/test/ghost/api_posts_spec.js b/core/test/ghost/api_posts_spec.js index 48dea9f1d8..1a85e65af2 100644 --- a/core/test/ghost/api_posts_spec.js +++ b/core/test/ghost/api_posts_spec.js @@ -26,9 +26,7 @@ results.length.should.equal(2); done(); - }, function (error) { - throw error; - }); + }, done); }); it('can read', function (done) { @@ -41,18 +39,13 @@ firstPost = results.models[0]; - posts.read({slug: firstPost.attributes.slug}).then(function (found) { - should.exist(found); + return posts.read({slug: firstPost.attributes.slug}); + }).then(function (found) { + should.exist(found); - found.attributes.title.should.equal(firstPost.attributes.title); + found.attributes.title.should.equal(firstPost.attributes.title); - done(); - }, function (error) { - throw error; - }); - - }, function (error) { - throw error; + done(); }); }); @@ -60,25 +53,24 @@ var firstPost; posts.browse().then(function (results) { + should.exist(results); results.length.should.be.above(0); firstPost = results.models[0]; - posts.edit({id: firstPost.id, title: "new title"}).then(function (edited) { - should.exist(edited); + return posts.edit({id: firstPost.id, title: "new title"}); - edited.attributes.title.should.equal('new title'); + }).then(function (edited) { - done(); - }, function (error) { - throw error; - }); + should.exist(edited); - }, function (error) { - throw error; - }); + edited.attributes.title.should.equal('new title'); + + done(); + + }, done); }); it('can add', function (done) { @@ -101,39 +93,35 @@ }); it('can delete', function (done) { - var firstPostId, - ids, - hasDeletedId; - + var firstPostId; posts.browse().then(function (results) { + should.exist(results); results.length.should.be.above(0); firstPostId = results.models[0].id; - posts.destroy(firstPostId).then(function () { + return posts.destroy(firstPostId); - posts.browse().then(function (newResults) { + }).then(function () { - ids = _.pluck(newResults.models, "id"); + return posts.browse(); - hasDeletedId = _.any(ids, function (id) { - return id === firstPostId; - }); + }).then(function (newResults) { + var ids, hasDeletedId; - hasDeletedId.should.equal(false); + ids = _.pluck(newResults.models, "id"); - done(); - }, function (error) { - throw error; - }); - }, function (error) { - throw error; + hasDeletedId = _.any(ids, function (id) { + return id === firstPostId; }); - }, function (error) { - throw error; - }); + + hasDeletedId.should.equal(false); + + done(); + + }, done); }); }); }()); \ No newline at end of file diff --git a/core/test/ghost/api_settings_spec.js b/core/test/ghost/api_settings_spec.js index 77d88a11ef..bb79726807 100644 --- a/core/test/ghost/api_settings_spec.js +++ b/core/test/ghost/api_settings_spec.js @@ -27,9 +27,7 @@ results.length.should.be.above(0); done(); - }, function (error) { - throw error; - }); + }, done); }); it('can read', function (done) { @@ -43,20 +41,17 @@ firstSetting = results.models[0]; - settings.read(firstSetting.attributes.key).then(function (found) { + return settings.read(firstSetting.attributes.key); - should.exist(found); + }).then(function (found) { - found.attributes.value.should.equal(firstSetting.attributes.value); + should.exist(found); - done(); - }, function (error) { - throw error; - }); + found.attributes.value.should.equal(firstSetting.attributes.value); - }, function (error) { - throw error; - }); + done(); + + }, done); }); it('can edit single', function (done) { @@ -71,29 +66,26 @@ firstPost = results.models[0]; - // The edit method has been modified to take an object of + // The edit method has been modified to take an object of // key/value pairs toEdit[firstPost.attributes.key] = "new value"; - settings.edit(toEdit).then(function (edited) { + return settings.edit(toEdit); - should.exist(edited); + }).then(function (edited) { - edited.length.should.equal(1); + should.exist(edited); - edited = edited[0]; + edited.length.should.equal(1); - edited.attributes.key.should.equal(firstPost.attributes.key); - edited.attributes.value.should.equal('new value'); + edited = edited[0]; - done(); - }, function (error) { - throw error; - }); + edited.attributes.key.should.equal(firstPost.attributes.key); + edited.attributes.value.should.equal('new value'); - }, function (error) { - throw error; - }); + done(); + + }, done); }); it('can edit multiple', function (done) { @@ -111,34 +103,32 @@ firstPost = results.models[0]; secondPost = results.models[1]; - // The edit method has been modified to take an object of + // The edit method has been modified to take an object of // key/value pairs toEdit[firstPost.attributes.key] = "new value1"; toEdit[secondPost.attributes.key] = "new value2"; - settings.edit(toEdit).then(function (edited) { + return settings.edit(toEdit); - should.exist(edited); + }).then(function (edited) { - edited.length.should.equal(2); + should.exist(edited); - editedPost = edited[0]; + edited.length.should.equal(2); - editedPost.attributes.key.should.equal(firstPost.attributes.key); - editedPost.attributes.value.should.equal('new value1'); + editedPost = edited[0]; - editedPost = edited[1]; + editedPost.attributes.key.should.equal(firstPost.attributes.key); + editedPost.attributes.value.should.equal('new value1'); - editedPost.attributes.key.should.equal(secondPost.attributes.key); - editedPost.attributes.value.should.equal('new value2'); + editedPost = edited[1]; - done(); - }, function (error) { - throw error; - }); - }, function (error) { - throw error; - }); + editedPost.attributes.key.should.equal(secondPost.attributes.key); + editedPost.attributes.value.should.equal('new value2'); + + done(); + + }, done); }); it('can add', function (done) { @@ -155,15 +145,11 @@ createdSetting.attributes.value.should.equal(newSetting.value, "value is correct"); done(); - }, function (error) { - throw error; - }); + }, done); }); it('can delete', function (done) { - var firstSettingId, - ids, - hasDeletedId; + var firstSettingId; settings.browse().then(function (results) { @@ -173,28 +159,27 @@ firstSettingId = results.models[0].id; - settings.destroy(firstSettingId).then(function () { + return settings.destroy(firstSettingId); - settings.browse().then(function (newResults) { + }).then(function () { - ids = _.pluck(newResults.models, "id"); + return settings.browse(); - hasDeletedId = _.any(ids, function (id) { - return id === firstSettingId; - }); + }).then(function (newResults) { - hasDeletedId.should.equal(false); + var ids, hasDeletedId; - done(); - }, function (error) { - throw error; - }); - }, function (error) { - throw error; + ids = _.pluck(newResults.models, "id"); + + hasDeletedId = _.any(ids, function (id) { + return id === firstSettingId; }); - }, function (error) { - throw error; - }); + + hasDeletedId.should.equal(false); + + done(); + + }, done); }); }); }()); \ No newline at end of file diff --git a/core/test/ghost/api_users_spec.js b/core/test/ghost/api_users_spec.js index 82c44a08e3..939d4146be 100644 --- a/core/test/ghost/api_users_spec.js +++ b/core/test/ghost/api_users_spec.js @@ -103,9 +103,7 @@ }); it('can delete', function (done) { - var firstUserId, - ids, - hasDeletedId; + var firstUserId; users.browse().then(function (results) { @@ -115,33 +113,30 @@ firstUserId = results.models[0].id; - users.destroy(firstUserId).then(function () { + return users.destroy(firstUserId); - users.browse().then(function (newResults) { + }).then(function () { - if (newResults.length < 1) { - // Bug out if we only had one user and deleted it. - return done(); - } + return users.browse(); - ids = _.pluck(newResults.models, "id"); + }).then(function (newResults) { + var ids, hasDeletedId; - hasDeletedId = _.any(ids, function (id) { - return id === firstUserId; - }); + if (newResults.length < 1) { + // Bug out if we only had one user and deleted it. + return done(); + } - hasDeletedId.should.equal(false); + ids = _.pluck(newResults.models, "id"); - done(); - }, function (error) { - throw error; - }); - }, function (error) { - throw error; + hasDeletedId = _.any(ids, function (id) { + return id === firstUserId; }); - }, function (error) { - throw error; - }); + + hasDeletedId.should.equal(false); + + done(); + }, done); }); }); From 4a318e9a6a176a3b458924438cf2bcdd2028fdc7 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sun, 26 May 2013 20:15:46 -0400 Subject: [PATCH 3/5] using grunt validate for npm test, fixing random jslint errors --- core/admin/controllers/index.js | 2 +- core/shared/errorHandling.js | 4 ++-- core/shared/models/dataProvider.bookshelf.js | 8 ++++---- core/shared/models/dataProvider.bookshelf.posts.js | 2 +- core/shared/models/dataProvider.bookshelf.settings.js | 6 +++--- core/shared/models/dataProvider.bookshelf.users.js | 6 +++--- core/shared/models/dataProvider.json.js | 2 +- core/test/ghost/errorHandling_spec.js | 10 +++++----- core/test/ghost/helpers.js | 4 ++-- package.json | 2 +- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/core/admin/controllers/index.js b/core/admin/controllers/index.js index 5e70aa4a73..95b8c52f90 100755 --- a/core/admin/controllers/index.js +++ b/core/admin/controllers/index.js @@ -64,7 +64,7 @@ console.log('user found: ', user); req.session.user = "ghostadmin"; res.redirect(req.query.redirect || '/ghost/'); - }, function(err) { + }, function (err) { // Do something here to signal the reason for an error console.log(err.stack); res.redirect('/ghost/login/'); diff --git a/core/shared/errorHandling.js b/core/shared/errorHandling.js index dc17de13e1..f61dcaafc0 100644 --- a/core/shared/errorHandling.js +++ b/core/shared/errorHandling.js @@ -1,4 +1,4 @@ -(function() { +(function () { "use strict"; var _ = require('underscore'), @@ -8,7 +8,7 @@ * Basic error handling helpers */ errors = { - throwError: function(err) { + throwError: function (err) { if (!err) { return; } diff --git a/core/shared/models/dataProvider.bookshelf.js b/core/shared/models/dataProvider.bookshelf.js index d3ef28c866..a4130fcaa4 100644 --- a/core/shared/models/dataProvider.bookshelf.js +++ b/core/shared/models/dataProvider.bookshelf.js @@ -20,11 +20,11 @@ // Simple bootstraping of the data model for now. var migration = require('../data/migration/001'); - migration.down().then(function() { - migration.up().then(function () { - console.log('all done....'); - }); + return migration.down().then(function () { + return migration.up(); }); + }).then(function () { + console.log('all done....'); }); } diff --git a/core/shared/models/dataProvider.bookshelf.posts.js b/core/shared/models/dataProvider.bookshelf.posts.js index 56cf834516..8dcbc52eaa 100644 --- a/core/shared/models/dataProvider.bookshelf.posts.js +++ b/core/shared/models/dataProvider.bookshelf.posts.js @@ -1,4 +1,4 @@ -(function() { +(function () { "use strict"; var util = require('util'), diff --git a/core/shared/models/dataProvider.bookshelf.settings.js b/core/shared/models/dataProvider.bookshelf.settings.js index 021c7a2559..e8a6538a84 100644 --- a/core/shared/models/dataProvider.bookshelf.settings.js +++ b/core/shared/models/dataProvider.bookshelf.settings.js @@ -1,4 +1,4 @@ -(function() { +(function () { "use strict"; var _ = require('underscore'), @@ -17,7 +17,7 @@ util.inherits(SettingsProvider, BaseProvider); - SettingsProvider.prototype.read = function(_key) { + SettingsProvider.prototype.read = function (_key) { // Allow for just passing the key instead of attributes if (_.isString(_key)) { _key = { key: _key }; @@ -25,7 +25,7 @@ return BaseProvider.prototype.read.call(this, _key); }; - SettingsProvider.prototype.edit = function(_data) { + SettingsProvider.prototype.edit = function (_data) { return when.all(_.map(_data, function (value, key) { return this.model.forge({ key: key }).fetch().then(function (setting) { return setting.set('value', value).save(); diff --git a/core/shared/models/dataProvider.bookshelf.users.js b/core/shared/models/dataProvider.bookshelf.users.js index e2f8b38004..6c2e2becf1 100644 --- a/core/shared/models/dataProvider.bookshelf.users.js +++ b/core/shared/models/dataProvider.bookshelf.users.js @@ -1,4 +1,4 @@ -(function() { +(function () { "use strict"; var util = require('util'), @@ -30,7 +30,7 @@ // Clone the _user so we don't expose the hashed password unnecessarily userData = _.extend({}, _user); - return nodefn.call(bcrypt.hash, _user.password, 10).then(function(hash) { + return nodefn.call(bcrypt.hash, _user.password, 10).then(function (hash) { userData.password = hash; return BaseProvider.prototype.add.call(self, userData); }); @@ -46,7 +46,7 @@ return this.model.forge({ email_address: _userdata.email }).fetch().then(function (user) { - return nodefn.call(bcrypt.compare, _userdata.pw, user.get('password')).then(function(matched) { + return nodefn.call(bcrypt.compare, _userdata.pw, user.get('password')).then(function (matched) { if (!matched) { return when.reject(new Error('Password does not match')); } diff --git a/core/shared/models/dataProvider.json.js b/core/shared/models/dataProvider.json.js index 922882e563..993f911ffa 100644 --- a/core/shared/models/dataProvider.json.js +++ b/core/shared/models/dataProvider.json.js @@ -29,7 +29,7 @@ DataProvider.prototype.globals.data = []; - DataProvider.prototype.globals.findAll = function() { + DataProvider.prototype.globals.findAll = function () { return when(this.data); }; diff --git a/core/test/ghost/errorHandling_spec.js b/core/test/ghost/errorHandling_spec.js index 7dedeb9c46..816739e106 100644 --- a/core/test/ghost/errorHandling_spec.js +++ b/core/test/ghost/errorHandling_spec.js @@ -19,7 +19,7 @@ errors.throwError(toThrow); }; - runThrowError.should.throw("test1"); + runThrowError.should['throw']("test1"); }); it("throws error strings", function () { @@ -28,7 +28,7 @@ errors.throwError(toThrow); }; - runThrowError.should.throw("test2"); + runThrowError.should['throw']("test2"); }); it("logs errors", function () { @@ -52,7 +52,7 @@ logStub.restore(); }); - it("logs promise errors with custom messages", function(done) { + it("logs promise errors with custom messages", function (done) { var def = when.defer(), prom = def.promise, logStub = sinon.stub(console, "log"); @@ -71,12 +71,12 @@ def.reject(); }); - it("logs promise errors and redirects", function(done) { + it("logs promise errors and redirects", function (done) { var def = when.defer(), prom = def.promise, req = null, res = { - redirect: function() { + redirect: function () { return; } }, diff --git a/core/test/ghost/helpers.js b/core/test/ghost/helpers.js index ca507b733a..5481dfa057 100644 --- a/core/test/ghost/helpers.js +++ b/core/test/ghost/helpers.js @@ -1,4 +1,4 @@ -(function() { +(function () { "use strict"; // Use 'testing' Ghost config @@ -13,7 +13,7 @@ resetData: function () { return migrations.one.down().then(function () { return migrations.one.up(); - }, function() { + }, function () { throw new Error("Failed to reset data"); }); } diff --git a/package.json b/package.json index 9210557905..5bc458515b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "start": "node app", - "test": "mocha core/test/ghost" + "test": "grunt validate" }, "dependencies": { "express": "3.1.x", From 557d81f17890cc7c5757cb18f4b4353eb9daebc5 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sun, 26 May 2013 20:20:28 -0400 Subject: [PATCH 4/5] missing a done or two --- core/test/ghost/api_posts_spec.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/test/ghost/api_posts_spec.js b/core/test/ghost/api_posts_spec.js index 1a85e65af2..d53626a8f5 100644 --- a/core/test/ghost/api_posts_spec.js +++ b/core/test/ghost/api_posts_spec.js @@ -46,7 +46,7 @@ found.attributes.title.should.equal(firstPost.attributes.title); done(); - }); + }, done); }); it('can edit', function (done) { @@ -87,9 +87,7 @@ createdPost.attributes.slug.should.equal(newPost.title.toLowerCase().replace(/ /g, '-'), 'slug is correct'); done(); - }, function (error) { - throw error; - }); + }, done); }); it('can delete', function (done) { From df23939554ce6bb0aa8704e9068f1c529030c28d Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Sun, 26 May 2013 21:39:38 -0400 Subject: [PATCH 5/5] done handler in final then, to ensure errors in last block are reported --- core/test/ghost/api_posts_spec.js | 10 +++--- core/test/ghost/api_settings_spec.js | 12 +++---- core/test/ghost/api_users_spec.js | 47 +++++++++++++--------------- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/core/test/ghost/api_posts_spec.js b/core/test/ghost/api_posts_spec.js index d53626a8f5..c4f64c1927 100644 --- a/core/test/ghost/api_posts_spec.js +++ b/core/test/ghost/api_posts_spec.js @@ -26,7 +26,7 @@ results.length.should.equal(2); done(); - }, done); + }).then(null, done); }); it('can read', function (done) { @@ -46,7 +46,7 @@ found.attributes.title.should.equal(firstPost.attributes.title); done(); - }, done); + }).then(null, done); }); it('can edit', function (done) { @@ -70,7 +70,7 @@ done(); - }, done); + }).then(null, done); }); it('can add', function (done) { @@ -87,7 +87,7 @@ createdPost.attributes.slug.should.equal(newPost.title.toLowerCase().replace(/ /g, '-'), 'slug is correct'); done(); - }, done); + }).then(null, done); }); it('can delete', function (done) { @@ -119,7 +119,7 @@ done(); - }, done); + }).then(null, done); }); }); }()); \ No newline at end of file diff --git a/core/test/ghost/api_settings_spec.js b/core/test/ghost/api_settings_spec.js index bb79726807..5bb9fd5e49 100644 --- a/core/test/ghost/api_settings_spec.js +++ b/core/test/ghost/api_settings_spec.js @@ -27,7 +27,7 @@ results.length.should.be.above(0); done(); - }, done); + }).then(null, done); }); it('can read', function (done) { @@ -51,7 +51,7 @@ done(); - }, done); + }).then(null, done); }); it('can edit single', function (done) { @@ -85,7 +85,7 @@ done(); - }, done); + }).then(null, done); }); it('can edit multiple', function (done) { @@ -128,7 +128,7 @@ done(); - }, done); + }).then(null, done); }); it('can add', function (done) { @@ -145,7 +145,7 @@ createdSetting.attributes.value.should.equal(newSetting.value, "value is correct"); done(); - }, done); + }).then(null, done); }); it('can delete', function (done) { @@ -179,7 +179,7 @@ done(); - }, done); + }).then(null, done); }); }); }()); \ No newline at end of file diff --git a/core/test/ghost/api_users_spec.js b/core/test/ghost/api_users_spec.js index 939d4146be..7311f90e03 100644 --- a/core/test/ghost/api_users_spec.js +++ b/core/test/ghost/api_users_spec.js @@ -20,6 +20,7 @@ }); it('can browse', function (done) { + users.browse().then(function (results) { should.exist(results); @@ -27,9 +28,8 @@ results.length.should.be.above(0); done(); - }, function (error) { - throw error; - }); + + }).then(null, done); }); it('can read', function (done) { @@ -43,18 +43,18 @@ firstUser = results.models[0]; - users.read({email_address: firstUser.attributes.email_address}).then(function (found) { + return users.read({email_address: firstUser.attributes.email_address}); - should.exist(found); + }).then(function (found) { - found.attributes.username.should.equal(firstUser.attributes.username); + should.exist(found); - done(); - }, function (error) { - throw error; - }); + found.attributes.username.should.equal(firstUser.attributes.username); + + done(); + + }).then(null, done); - }); }); it('can edit', function (done) { @@ -68,19 +68,17 @@ firstUser = results.models[0]; - users.edit({id: firstUser.id, url: "some.newurl.com"}).then(function (edited) { + return users.edit({id: firstUser.id, url: "some.newurl.com"}); - should.exist(edited); + }).then(function (edited) { - edited.attributes.url.should.equal('some.newurl.com'); + should.exist(edited); - done(); - }, function (error) { - throw error; - }); - }, function (error) { - throw error; - }); + edited.attributes.url.should.equal('some.newurl.com'); + + done(); + + }).then(null, done); }); it('can add', function (done) { @@ -97,9 +95,7 @@ createdUser.attributes.email_address.should.eql(userData.email_address, "email address corred"); done(); - }, function (error) { - throw error; - }); + }).then(null, done); }); it('can delete', function (done) { @@ -136,7 +132,8 @@ hasDeletedId.should.equal(false); done(); - }, done); + + }).then(null, done); }); });