I'm running unittests via Mocha / Chai on a sequelize definition as shown:
Main tests.js that is run with mocha tests.js
:
// Testing Dependencies
expect = require("chai").expect;
should = require("chai").should;
require('dotenv').load();
var Sequelize = require('sequelize');
var sequelize = new Sequelize(
process.env.PG_DB_TEST,
process.env.PG_USER,
process.env.PG_PASSWORD, {
dialect: "postgres",
logging: false
});
var models = require('./models/db')(sequelize);
var seq_test = function (next) {
return function () {
beforeEach(function (done) {
sequelize.sync({ force: true }).then(function() {
done();
});
});
afterEach(function (done) {
sequelize.drop().then(function() {
done();
});
});
next();
};
}
describe("Model Unittests", seq_test(function () {
require("./models/tests/test_user.js")(models);
require("./models/tests/test_interest.js")(models);
}));
test_user.js
var mockedUser = require("./mocks/user");
module.exports = function (models) {
var User = models.user;
it("User should have the correct fields", function (done) {
User.create(mockedUser).then(function (result) {
expect(result.pack()).to.include.all.keys(
["id", "name", "email", "intro"]
);
done();
});
});
it("User should require an email", function (done) {
User.create({
"name": mockedUser['name']
}).then(function (result) {
expect.fail();
done();
}).catch(function (err) {
expect(err['name']).to.be.equal('SequelizeValidationError');
done();
});
});
it("User should require a name", function (done) {
User.create({
"email": mockedUser['email']
}).then(function (result) {
expect.fail();
done();
}).catch(function (err) {
expect(err['name']).to.be.equal('SequelizeValidationError');
done();
});
});
}
Sometimes (about 1 out of 15 on a Codeship (CI)), it gives this error:
Model Unittests
Unhandled rejection SequelizeUniqueConstraintError: Validation error
at Query.formatError (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/sequelize/lib/dialects/postgres/query.js:402:16)
at null.<anonymous> (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/sequelize/lib/dialects/postgres/query.js:108:19)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at Query.handleError (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/pg/lib/query.js:108:8)
at null.<anonymous> (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/pg/lib/client.js:171:26)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at Socket.<anonymous> (/home/rof/src/github.com/podtogether/pod-test-prototype/node_modules/pg/lib/connection.js:109:12)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
1) "before each" hook for "User should have the correct fields"
Locally, these unittests haven't failed (I've run it perhaps... 60 times in a row). I saw similar issues earlier when I didn't use the done
callback in the beforeEach
and afterEach
. Both of those were async and needed to wait before continuing. After fixing that, I stopped seeing these issues locally.
Can anyone shed some light on this issue? (ssh'ed into Codeship and ran the tests resulted in the 1 / ~15 error)