Skip to content

Commit

Permalink
Patch to prevent timeouts in unit tests
Browse files Browse the repository at this point in the history
Temporary patch for TryGhost#362

- Split out database teardown and initialization so they each have their
own 2 second timeout.
- Added some test-specific increased timeouts.
  • Loading branch information
gotdibbs committed Aug 24, 2013
1 parent 9d8bf4b commit ff3a9dd
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 153 deletions.
4 changes: 4 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ var path = require('path'),
src: ['core/test/unit/**/api*_spec.js']
},

frontend: {
src: ['core/test/unit/**/frontend*_spec.js']
},

perm: {
src: ['core/test/unit/**/permissions_spec.js']
},
Expand Down
30 changes: 28 additions & 2 deletions core/test/unit/api_permissions_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ describe("Role Model", function () {

should.exist(RoleModel);

before(function (done) {
helpers.clearData().then(function () {
done();
}, done);
});

beforeEach(function (done) {
helpers.resetData().then(function () {
this.timeout(5000);
helpers.initData().then(function () {
done();
}, done);
});

afterEach(function (done) {
helpers.clearData().then(function () {
done();
}, done);
});
Expand Down Expand Up @@ -91,8 +104,21 @@ describe("Permission Model", function () {

should.exist(PermissionModel);

before(function (done) {
helpers.clearData().then(function () {
done();
}, done);
});

beforeEach(function (done) {
helpers.resetData().then(function () {
this.timeout(5000);
helpers.initData().then(function () {
done();
}, done);
});

afterEach(function (done) {
helpers.clearData().then(function () {
done();
}, done);
});
Expand Down
100 changes: 49 additions & 51 deletions core/test/unit/api_posts_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,32 @@ var _ = require("underscore"),
describe('Post Model', function () {

var PostModel = Models.Post,
UserModel = Models.User;
UserModel = Models.User,
userData = {
password: 'testpass1',
email_address: "[email protected]",
full_name: "Mr Biscuits"
};

before(function (done) {
helpers.clearData().then(function () {
done();
}, done);
});

beforeEach(function (done) {
helpers.resetData().then(function () {
this.timeout(5000);
helpers.initData()
.then(function () {
return UserModel.add(userData);
})
.then(function () {
done();
}, done);
});

afterEach(function (done) {
helpers.clearData().then(function () {
done();
}, done);
});
Expand Down Expand Up @@ -48,62 +70,36 @@ describe('Post Model', function () {
});

it('can findAll, returning author and user data', function (done) {
var firstPost,
userData = {
password: 'testpass1',
email_address: "[email protected]",
full_name: "Mr Biscuits"
};

helpers.resetData().then(function () {
UserModel.add(userData).then(function (createdUser) {

PostModel.findAll({}).then(function (results) {
should.exist(results);
results.length.should.be.above(0);
firstPost = results.models[0].toJSON();

firstPost.author.should.be.a("object");
firstPost.user.should.be.a("object");
firstPost.author.full_name.should.equal("Mr Biscuits");
firstPost.user.full_name.should.equal("Mr Biscuits");
var firstPost;

return true;
PostModel.findAll({}).then(function (results) {
should.exist(results);
results.length.should.be.above(0);
firstPost = results.models[0].toJSON();

}).then(null, done);
firstPost.author.should.be.a("object");
firstPost.user.should.be.a("object");
firstPost.author.full_name.should.equal("Mr Biscuits");
firstPost.user.full_name.should.equal("Mr Biscuits");

done();
}).then(null, done);
});
done();
}, done);
});

it('can findOne, returning author and user data', function (done) {
var firstPost,
userData = {
password: 'testpass1',
email_address: "[email protected]",
full_name: "Mr Biscuits"
};

helpers.resetData().then(function () {
UserModel.add(userData).then(function (createdUser) {

PostModel.findOne({}).then(function (result) {
should.exist(result);
firstPost = result.toJSON();

firstPost.author.should.be.a("object");
firstPost.user.should.be.a("object");
firstPost.author.full_name.should.equal("Mr Biscuits");
firstPost.user.full_name.should.equal("Mr Biscuits");
var firstPost;

return true;
PostModel.findOne({}).then(function (result) {
should.exist(result);
firstPost = result.toJSON();

}).then(null, done);
firstPost.author.should.be.a("object");
firstPost.user.should.be.a("object");
firstPost.author.full_name.should.equal("Mr Biscuits");
firstPost.user.full_name.should.equal("Mr Biscuits");

done();
}).then(null, done);
});
done();
}, done);
});

it('can edit', function (done) {
Expand Down Expand Up @@ -172,7 +168,9 @@ describe('Post Model', function () {
content_raw: 'Test Content 1'
};

// Create 12 posts with the sametitle
this.timeout(5000); // this is a patch to ensure it doesn't timeout.

// Create 12 posts with the same title
sequence(_.times(12, function (i) {
return function () {
return PostModel.add({
Expand Down Expand Up @@ -241,7 +239,7 @@ describe('Post Model', function () {
});

it('can fetch a paginated set, with various options', function (done) {
this.timeout(5000);
this.timeout(10000); // this is a patch to ensure it doesn't timeout.

helpers.insertMorePosts().then(function () {

Expand Down
15 changes: 14 additions & 1 deletion core/test/unit/api_settings_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@ describe('Settings Model', function () {

var SettingsModel = Models.Settings;

before(function (done) {
helpers.clearData().then(function () {
done();
}, done);
});

beforeEach(function (done) {
helpers.resetData().then(function () {
this.timeout(5000);
helpers.initData().then(function () {
done();
}, done);
});

afterEach(function (done) {
helpers.clearData().then(function () {
done();
}, done);
});
Expand Down
Loading

0 comments on commit ff3a9dd

Please sign in to comment.