不要怂,就是干,撸起袖子干!

Commit b49a24d4 by Andy Edwards Committed by GitHub

fix(test/integration/hooks): asyncify (#12251)

1 parent 4dbfb5d1
...@@ -6,7 +6,7 @@ const chai = require('chai'), ...@@ -6,7 +6,7 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types'); DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Hooks'), () => { describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -17,12 +17,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -17,12 +17,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral'] values: ['happy', 'sad', 'neutral']
} }
}); });
return this.sequelize.sync({ force: true }); await this.sequelize.sync({ force: true });
}); });
describe('#count', () => { describe('#count', () => {
beforeEach(function() { beforeEach(async function() {
return this.User.bulkCreate([ await this.User.bulkCreate([
{ username: 'adam', mood: 'happy' }, { username: 'adam', mood: 'happy' },
{ username: 'joe', mood: 'sad' }, { username: 'joe', mood: 'sad' },
{ username: 'joe', mood: 'happy' } { username: 'joe', mood: 'happy' }
...@@ -30,35 +30,34 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -30,35 +30,34 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
}); });
describe('on success', () => { describe('on success', () => {
it('hook runs', function() { it('hook runs', async function() {
let beforeHook = false; let beforeHook = false;
this.User.beforeCount(() => { this.User.beforeCount(() => {
beforeHook = true; beforeHook = true;
}); });
return this.User.count().then(count => { const count = await this.User.count();
expect(count).to.equal(3); expect(count).to.equal(3);
expect(beforeHook).to.be.true; expect(beforeHook).to.be.true;
}); });
});
it('beforeCount hook can change options', function() { it('beforeCount hook can change options', async function() {
this.User.beforeCount(options => { this.User.beforeCount(options => {
options.where.username = 'adam'; options.where.username = 'adam';
}); });
return expect(this.User.count({ where: { username: 'joe' } })).to.eventually.equal(1); await expect(this.User.count({ where: { username: 'joe' } })).to.eventually.equal(1);
}); });
}); });
describe('on error', () => { describe('on error', () => {
it('in beforeCount hook returns error', function() { it('in beforeCount hook returns error', async function() {
this.User.beforeCount(() => { this.User.beforeCount(() => {
throw new Error('Oops!'); throw new Error('Oops!');
}); });
return expect(this.User.count({ where: { username: 'adam' } })).to.be.rejectedWith('Oops!'); await expect(this.User.count({ where: { username: 'adam' } })).to.be.rejectedWith('Oops!');
}); });
}); });
}); });
......
...@@ -8,7 +8,7 @@ const chai = require('chai'), ...@@ -8,7 +8,7 @@ const chai = require('chai'),
sinon = require('sinon'); sinon = require('sinon');
describe(Support.getTestDialectTeaser('Hooks'), () => { describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -19,12 +19,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -19,12 +19,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral'] values: ['happy', 'sad', 'neutral']
} }
}); });
return this.sequelize.sync({ force: true }); await this.sequelize.sync({ force: true });
}); });
describe('#create', () => { describe('#create', () => {
describe('on success', () => { describe('on success', () => {
it('should run hooks', function() { it('should run hooks', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(), afterHook = sinon.spy(),
beforeSave = sinon.spy(), beforeSave = sinon.spy(),
...@@ -35,17 +35,16 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -35,17 +35,16 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave); this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave); this.User.afterSave(afterSave);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(() => { await this.User.create({ username: 'Toni', mood: 'happy' });
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledOnce; expect(beforeSave).to.have.been.calledOnce;
expect(afterSave).to.have.been.calledOnce; expect(afterSave).to.have.been.calledOnce;
}); });
}); });
});
describe('on error', () => { describe('on error', () => {
it('should return an error from before', function() { it('should return an error from before', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
beforeSave = sinon.spy(), beforeSave = sinon.spy(),
afterHook = sinon.spy(), afterHook = sinon.spy(),
...@@ -59,15 +58,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -59,15 +58,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave); this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave); this.User.afterSave(afterSave);
return expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejected.then(() => { await expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called; expect(afterHook).not.to.have.been.called;
expect(beforeSave).not.to.have.been.called; expect(beforeSave).not.to.have.been.called;
expect(afterSave).not.to.have.been.called; expect(afterSave).not.to.have.been.called;
}); });
});
it('should return an error from after', function() { it('should return an error from after', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
beforeSave = sinon.spy(), beforeSave = sinon.spy(),
afterHook = sinon.spy(), afterHook = sinon.spy(),
...@@ -82,16 +80,15 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -82,16 +80,15 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave); this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave); this.User.afterSave(afterSave);
return expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejected.then(() => { await expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledOnce; expect(beforeSave).to.have.been.calledOnce;
expect(afterSave).not.to.have.been.called; expect(afterSave).not.to.have.been.called;
}); });
}); });
});
it('should not trigger hooks on parent when using N:M association setters', function() { it('should not trigger hooks on parent when using N:M association setters', async function() {
const A = this.sequelize.define('A', { const A = this.sequelize.define('A', {
name: Sequelize.STRING name: Sequelize.STRING
}); });
...@@ -101,28 +98,26 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -101,28 +98,26 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
let hookCalled = 0; let hookCalled = 0;
A.addHook('afterCreate', () => { A.addHook('afterCreate', async () => {
hookCalled++; hookCalled++;
return Promise.resolve();
}); });
B.belongsToMany(A, { through: 'a_b' }); B.belongsToMany(A, { through: 'a_b' });
A.belongsToMany(B, { through: 'a_b' }); A.belongsToMany(B, { through: 'a_b' });
return this.sequelize.sync({ force: true }).then(() => { await this.sequelize.sync({ force: true });
return Promise.all([
const [a, b] = await Promise.all([
A.create({ name: 'a' }), A.create({ name: 'a' }),
B.create({ name: 'b' }) B.create({ name: 'b' })
]).then(([a, b]) => { ]);
return a.addB(b).then(() => {
await a.addB(b);
expect(hookCalled).to.equal(1); expect(hookCalled).to.equal(1);
}); });
});
});
});
describe('preserves changes to instance', () => { describe('preserves changes to instance', () => {
it('beforeValidate', function() { it('beforeValidate', async function() {
let hookCalled = 0; let hookCalled = 0;
this.User.beforeValidate(user => { this.User.beforeValidate(user => {
...@@ -130,14 +125,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -130,14 +125,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++; hookCalled++;
}); });
return this.User.create({ mood: 'sad', username: 'leafninja' }).then(user => { const user = await this.User.create({ mood: 'sad', username: 'leafninja' });
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
expect(user.username).to.equal('leafninja'); expect(user.username).to.equal('leafninja');
expect(hookCalled).to.equal(1); expect(hookCalled).to.equal(1);
}); });
});
it('afterValidate', function() { it('afterValidate', async function() {
let hookCalled = 0; let hookCalled = 0;
this.User.afterValidate(user => { this.User.afterValidate(user => {
...@@ -145,14 +139,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -145,14 +139,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++; hookCalled++;
}); });
return this.User.create({ mood: 'sad', username: 'fireninja' }).then(user => { const user = await this.User.create({ mood: 'sad', username: 'fireninja' });
expect(user.mood).to.equal('neutral'); expect(user.mood).to.equal('neutral');
expect(user.username).to.equal('fireninja'); expect(user.username).to.equal('fireninja');
expect(hookCalled).to.equal(1); expect(hookCalled).to.equal(1);
}); });
});
it('beforeCreate', function() { it('beforeCreate', async function() {
let hookCalled = 0; let hookCalled = 0;
this.User.beforeCreate(user => { this.User.beforeCreate(user => {
...@@ -160,14 +153,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -160,14 +153,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++; hookCalled++;
}); });
return this.User.create({ username: 'akira' }).then(user => { const user = await this.User.create({ username: 'akira' });
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
expect(user.username).to.equal('akira'); expect(user.username).to.equal('akira');
expect(hookCalled).to.equal(1); expect(hookCalled).to.equal(1);
}); });
});
it('beforeSave', function() { it('beforeSave', async function() {
let hookCalled = 0; let hookCalled = 0;
this.User.beforeSave(user => { this.User.beforeSave(user => {
...@@ -175,14 +167,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -175,14 +167,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++; hookCalled++;
}); });
return this.User.create({ username: 'akira' }).then(user => { const user = await this.User.create({ username: 'akira' });
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
expect(user.username).to.equal('akira'); expect(user.username).to.equal('akira');
expect(hookCalled).to.equal(1); expect(hookCalled).to.equal(1);
}); });
});
it('beforeSave with beforeCreate', function() { it('beforeSave with beforeCreate', async function() {
let hookCalled = 0; let hookCalled = 0;
this.User.beforeCreate(user => { this.User.beforeCreate(user => {
...@@ -195,12 +186,11 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -195,12 +186,11 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++; hookCalled++;
}); });
return this.User.create({ username: 'akira' }).then(user => { const user = await this.User.create({ username: 'akira' });
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
expect(user.username).to.equal('akira'); expect(user.username).to.equal('akira');
expect(hookCalled).to.equal(2); expect(hookCalled).to.equal(2);
}); });
}); });
}); });
});
}); });
...@@ -7,7 +7,7 @@ const chai = require('chai'), ...@@ -7,7 +7,7 @@ const chai = require('chai'),
sinon = require('sinon'); sinon = require('sinon');
describe(Support.getTestDialectTeaser('Hooks'), () => { describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -18,29 +18,27 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -18,29 +18,27 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral'] values: ['happy', 'sad', 'neutral']
} }
}); });
return this.sequelize.sync({ force: true }); await this.sequelize.sync({ force: true });
}); });
describe('#destroy', () => { describe('#destroy', () => {
describe('on success', () => { describe('on success', () => {
it('should run hooks', function() { it('should run hooks', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
this.User.beforeDestroy(beforeHook); this.User.beforeDestroy(beforeHook);
this.User.afterDestroy(afterHook); this.User.afterDestroy(afterHook);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.User.create({ username: 'Toni', mood: 'happy' });
return user.destroy().then(() => { await user.destroy();
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
}); });
}); });
});
});
describe('on error', () => { describe('on error', () => {
it('should return an error from before', function() { it('should return an error from before', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
...@@ -50,15 +48,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -50,15 +48,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
}); });
this.User.afterDestroy(afterHook); this.User.afterDestroy(afterHook);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.User.create({ username: 'Toni', mood: 'happy' });
return expect(user.destroy()).to.be.rejected.then(() => { await expect(user.destroy()).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called; expect(afterHook).not.to.have.been.called;
}); });
});
});
it('should return an error from after', function() { it('should return an error from after', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
...@@ -68,14 +64,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -68,14 +64,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
throw new Error('Whoops!'); throw new Error('Whoops!');
}); });
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.User.create({ username: 'Toni', mood: 'happy' });
return expect(user.destroy()).to.be.rejected.then(() => { await expect(user.destroy()).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
}); });
}); });
});
});
describe('with paranoid mode enabled', () => { describe('with paranoid mode enabled', () => {
beforeEach(function() { beforeEach(function() {
...@@ -96,28 +90,24 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -96,28 +90,24 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
}); });
}); });
it('sets other changed values when soft deleting and a beforeDestroy hooks kicks in', function() { it('sets other changed values when soft deleting and a beforeDestroy hooks kicks in', async function() {
return this.ParanoidUser.sync({ force: true }) await this.ParanoidUser.sync({ force: true });
.then(() => this.ParanoidUser.create({ username: 'user1' })) const user0 = await this.ParanoidUser.create({ username: 'user1' });
.then(user => user.destroy()) await user0.destroy();
.then(() => this.ParanoidUser.findOne({ paranoid: false })) const user = await this.ParanoidUser.findOne({ paranoid: false });
.then(user => {
expect(user.updatedBy).to.equal(1); expect(user.updatedBy).to.equal(1);
}); });
});
it('should not throw error when a beforeDestroy hook changes a virtual column', function() { it('should not throw error when a beforeDestroy hook changes a virtual column', async function() {
this.ParanoidUser.beforeDestroy(instance => instance.virtualField = 2); this.ParanoidUser.beforeDestroy(instance => instance.virtualField = 2);
return this.ParanoidUser.sync({ force: true }) await this.ParanoidUser.sync({ force: true });
.then(() => this.ParanoidUser.create({ username: 'user1' })) const user0 = await this.ParanoidUser.create({ username: 'user1' });
.then(user => user.destroy()) await user0.destroy();
.then(() => this.ParanoidUser.findOne({ paranoid: false })) const user = await this.ParanoidUser.findOne({ paranoid: false });
.then(user => {
expect(user.virtualField).to.equal(0); expect(user.virtualField).to.equal(0);
}); });
}); });
}); });
});
}); });
...@@ -6,7 +6,7 @@ const chai = require('chai'), ...@@ -6,7 +6,7 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types'); DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Hooks'), () => { describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -18,28 +18,28 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -18,28 +18,28 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
} }
}); });
return this.sequelize.sync({ force: true }); await this.sequelize.sync({ force: true });
}); });
describe('#find', () => { describe('#find', () => {
beforeEach(function() { beforeEach(async function() {
return this.User.bulkCreate([ await this.User.bulkCreate([
{ username: 'adam', mood: 'happy' }, { username: 'adam', mood: 'happy' },
{ username: 'joe', mood: 'sad' } { username: 'joe', mood: 'sad' }
]); ]);
}); });
it('allow changing attributes via beforeFind #5675', function() { it('allow changing attributes via beforeFind #5675', async function() {
this.User.beforeFind(options => { this.User.beforeFind(options => {
options.attributes = { options.attributes = {
include: [['id', 'my_id']] include: [['id', 'my_id']]
}; };
}); });
return this.User.findAll({}); await this.User.findAll({});
}); });
describe('on success', () => { describe('on success', () => {
it('all hooks run', function() { it('all hooks run', async function() {
let beforeHook = false, let beforeHook = false,
beforeHook2 = false, beforeHook2 = false,
beforeHook3 = false, beforeHook3 = false,
...@@ -61,95 +61,98 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -61,95 +61,98 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
afterHook = true; afterHook = true;
}); });
return this.User.findOne({ where: { username: 'adam' } }).then(user => { const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
expect(beforeHook).to.be.true; expect(beforeHook).to.be.true;
expect(beforeHook2).to.be.true; expect(beforeHook2).to.be.true;
expect(beforeHook3).to.be.true; expect(beforeHook3).to.be.true;
expect(afterHook).to.be.true; expect(afterHook).to.be.true;
}); });
});
it('beforeFind hook can change options', function() { it('beforeFind hook can change options', async function() {
this.User.beforeFind(options => { this.User.beforeFind(options => {
options.where.username = 'joe'; options.where.username = 'joe';
}); });
return this.User.findOne({ where: { username: 'adam' } }).then(user => { const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('sad'); expect(user.mood).to.equal('sad');
}); });
});
it('beforeFindAfterExpandIncludeAll hook can change options', function() { it('beforeFindAfterExpandIncludeAll hook can change options', async function() {
this.User.beforeFindAfterExpandIncludeAll(options => { this.User.beforeFindAfterExpandIncludeAll(options => {
options.where.username = 'joe'; options.where.username = 'joe';
}); });
return this.User.findOne({ where: { username: 'adam' } }).then(user => { const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('sad'); expect(user.mood).to.equal('sad');
}); });
});
it('beforeFindAfterOptions hook can change options', function() { it('beforeFindAfterOptions hook can change options', async function() {
this.User.beforeFindAfterOptions(options => { this.User.beforeFindAfterOptions(options => {
options.where.username = 'joe'; options.where.username = 'joe';
}); });
return this.User.findOne({ where: { username: 'adam' } }).then(user => { const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('sad'); expect(user.mood).to.equal('sad');
}); });
});
it('afterFind hook can change results', function() { it('afterFind hook can change results', async function() {
this.User.afterFind(user => { this.User.afterFind(user => {
user.mood = 'sad'; user.mood = 'sad';
}); });
return this.User.findOne({ where: { username: 'adam' } }).then(user => { const user = await this.User.findOne({ where: { username: 'adam' } });
expect(user.mood).to.equal('sad'); expect(user.mood).to.equal('sad');
}); });
}); });
});
describe('on error', () => { describe('on error', () => {
it('in beforeFind hook returns error', function() { it('in beforeFind hook returns error', async function() {
this.User.beforeFind(() => { this.User.beforeFind(() => {
throw new Error('Oops!'); throw new Error('Oops!');
}); });
return this.User.findOne({ where: { username: 'adam' } }).catch(err => { try {
await this.User.findOne({ where: { username: 'adam' } });
} catch (err) {
expect(err.message).to.equal('Oops!'); expect(err.message).to.equal('Oops!');
}); }
}); });
it('in beforeFindAfterExpandIncludeAll hook returns error', function() { it('in beforeFindAfterExpandIncludeAll hook returns error', async function() {
this.User.beforeFindAfterExpandIncludeAll(() => { this.User.beforeFindAfterExpandIncludeAll(() => {
throw new Error('Oops!'); throw new Error('Oops!');
}); });
return this.User.findOne({ where: { username: 'adam' } }).catch(err => { try {
await this.User.findOne({ where: { username: 'adam' } });
} catch (err) {
expect(err.message).to.equal('Oops!'); expect(err.message).to.equal('Oops!');
}); }
}); });
it('in beforeFindAfterOptions hook returns error', function() { it('in beforeFindAfterOptions hook returns error', async function() {
this.User.beforeFindAfterOptions(() => { this.User.beforeFindAfterOptions(() => {
throw new Error('Oops!'); throw new Error('Oops!');
}); });
return this.User.findOne({ where: { username: 'adam' } }).catch(err => { try {
await this.User.findOne({ where: { username: 'adam' } });
} catch (err) {
expect(err.message).to.equal('Oops!'); expect(err.message).to.equal('Oops!');
}); }
}); });
it('in afterFind hook returns error', function() { it('in afterFind hook returns error', async function() {
this.User.afterFind(() => { this.User.afterFind(() => {
throw new Error('Oops!'); throw new Error('Oops!');
}); });
return this.User.findOne({ where: { username: 'adam' } }).catch(err => { try {
await this.User.findOne({ where: { username: 'adam' } });
} catch (err) {
expect(err.message).to.equal('Oops!'); expect(err.message).to.equal('Oops!');
}); }
}); });
}); });
}); });
......
...@@ -7,7 +7,7 @@ const chai = require('chai'), ...@@ -7,7 +7,7 @@ const chai = require('chai'),
sinon = require('sinon'); sinon = require('sinon');
describe(Support.getTestDialectTeaser('Hooks'), () => { describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -29,31 +29,28 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -29,31 +29,28 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
paranoid: true paranoid: true
}); });
return this.sequelize.sync({ force: true }); await this.sequelize.sync({ force: true });
}); });
describe('#restore', () => { describe('#restore', () => {
describe('on success', () => { describe('on success', () => {
it('should run hooks', function() { it('should run hooks', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
this.ParanoidUser.beforeRestore(beforeHook); this.ParanoidUser.beforeRestore(beforeHook);
this.ParanoidUser.afterRestore(afterHook); this.ParanoidUser.afterRestore(afterHook);
return this.ParanoidUser.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.ParanoidUser.create({ username: 'Toni', mood: 'happy' });
return user.destroy().then(() => { await user.destroy();
return user.restore().then(() => { await user.restore();
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
}); });
}); });
});
});
});
describe('on error', () => { describe('on error', () => {
it('should return an error from before', function() { it('should return an error from before', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
...@@ -63,17 +60,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -63,17 +60,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
}); });
this.ParanoidUser.afterRestore(afterHook); this.ParanoidUser.afterRestore(afterHook);
return this.ParanoidUser.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.ParanoidUser.create({ username: 'Toni', mood: 'happy' });
return user.destroy().then(() => { await user.destroy();
return expect(user.restore()).to.be.rejected.then(() => { await expect(user.restore()).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called; expect(afterHook).not.to.have.been.called;
}); });
});
});
});
it('should return an error from after', function() { it('should return an error from after', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
...@@ -83,16 +77,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -83,16 +77,13 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
throw new Error('Whoops!'); throw new Error('Whoops!');
}); });
return this.ParanoidUser.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.ParanoidUser.create({ username: 'Toni', mood: 'happy' });
return user.destroy().then(() => { await user.destroy();
return expect(user.restore()).to.be.rejected.then(() => { await expect(user.restore()).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
}); });
}); });
}); });
});
});
});
}); });
...@@ -7,7 +7,7 @@ const chai = require('chai'), ...@@ -7,7 +7,7 @@ const chai = require('chai'),
sinon = require('sinon'); sinon = require('sinon');
describe(Support.getTestDialectTeaser('Hooks'), () => { describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -18,12 +18,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -18,12 +18,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral'] values: ['happy', 'sad', 'neutral']
} }
}); });
return this.sequelize.sync({ force: true }); await this.sequelize.sync({ force: true });
}); });
describe('#update', () => { describe('#update', () => {
describe('on success', () => { describe('on success', () => {
it('should run hooks', function() { it('should run hooks', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(), afterHook = sinon.spy(),
beforeSave = sinon.spy(), beforeSave = sinon.spy(),
...@@ -34,20 +34,18 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -34,20 +34,18 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave); this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave); this.User.afterSave(afterSave);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.User.create({ username: 'Toni', mood: 'happy' });
return user.update({ username: 'Chong' }).then(user => { const user0 = await user.update({ username: 'Chong' });
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledTwice; expect(beforeSave).to.have.been.calledTwice;
expect(afterSave).to.have.been.calledTwice; expect(afterSave).to.have.been.calledTwice;
expect(user.username).to.equal('Chong'); expect(user0.username).to.equal('Chong');
});
});
}); });
}); });
describe('on error', () => { describe('on error', () => {
it('should return an error from before', function() { it('should return an error from before', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(), afterHook = sinon.spy(),
beforeSave = sinon.spy(), beforeSave = sinon.spy(),
...@@ -61,17 +59,15 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -61,17 +59,15 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave); this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave); this.User.afterSave(afterSave);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.User.create({ username: 'Toni', mood: 'happy' });
return expect(user.update({ username: 'Chong' })).to.be.rejected.then(() => { await expect(user.update({ username: 'Chong' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledOnce; expect(beforeSave).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called; expect(afterHook).not.to.have.been.called;
expect(afterSave).to.have.been.calledOnce; expect(afterSave).to.have.been.calledOnce;
}); });
});
});
it('should return an error from after', function() { it('should return an error from after', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(), afterHook = sinon.spy(),
beforeSave = sinon.spy(), beforeSave = sinon.spy(),
...@@ -85,47 +81,39 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -85,47 +81,39 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
this.User.beforeSave(beforeSave); this.User.beforeSave(beforeSave);
this.User.afterSave(afterSave); this.User.afterSave(afterSave);
return this.User.create({ username: 'Toni', mood: 'happy' }).then(user => { const user = await this.User.create({ username: 'Toni', mood: 'happy' });
return expect(user.update({ username: 'Chong' })).to.be.rejected.then(() => { await expect(user.update({ username: 'Chong' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
expect(beforeSave).to.have.been.calledTwice; expect(beforeSave).to.have.been.calledTwice;
expect(afterSave).to.have.been.calledOnce; expect(afterSave).to.have.been.calledOnce;
}); });
}); });
});
});
describe('preserves changes to instance', () => { describe('preserves changes to instance', () => {
it('beforeValidate', function() { it('beforeValidate', async function() {
this.User.beforeValidate(user => { this.User.beforeValidate(user => {
user.mood = 'happy'; user.mood = 'happy';
}); });
return this.User.create({ username: 'fireninja', mood: 'invalid' }).then(user => { const user0 = await this.User.create({ username: 'fireninja', mood: 'invalid' });
return user.update({ username: 'hero' }); const user = await user0.update({ username: 'hero' });
}).then(user => {
expect(user.username).to.equal('hero'); expect(user.username).to.equal('hero');
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
}); });
});
it('afterValidate', function() {
it('afterValidate', async function() {
this.User.afterValidate(user => { this.User.afterValidate(user => {
user.mood = 'sad'; user.mood = 'sad';
}); });
return this.User.create({ username: 'fireninja', mood: 'nuetral' }).then(user => { const user0 = await this.User.create({ username: 'fireninja', mood: 'nuetral' });
return user.update({ username: 'spider' }); const user = await user0.update({ username: 'spider' });
}).then(user => {
expect(user.username).to.equal('spider'); expect(user.username).to.equal('spider');
expect(user.mood).to.equal('sad'); expect(user.mood).to.equal('sad');
}); });
});
it('beforeSave', function() { it('beforeSave', async function() {
let hookCalled = 0; let hookCalled = 0;
this.User.beforeSave(user => { this.User.beforeSave(user => {
...@@ -133,16 +121,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -133,16 +121,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++; hookCalled++;
}); });
return this.User.create({ username: 'fireninja', mood: 'nuetral' }).then(user => { const user0 = await this.User.create({ username: 'fireninja', mood: 'nuetral' });
return user.update({ username: 'spider', mood: 'sad' }); const user = await user0.update({ username: 'spider', mood: 'sad' });
}).then(user => {
expect(user.username).to.equal('spider'); expect(user.username).to.equal('spider');
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
expect(hookCalled).to.equal(2); expect(hookCalled).to.equal(2);
}); });
});
it('beforeSave with beforeUpdate', function() { it('beforeSave with beforeUpdate', async function() {
let hookCalled = 0; let hookCalled = 0;
this.User.beforeUpdate(user => { this.User.beforeUpdate(user => {
...@@ -155,14 +141,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -155,14 +141,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
hookCalled++; hookCalled++;
}); });
return this.User.create({ username: 'akira' }).then(user => { const user0 = await this.User.create({ username: 'akira' });
return user.update({ username: 'spider', mood: 'sad' }); const user = await user0.update({ username: 'spider', mood: 'sad' });
}).then(user => {
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
expect(user.username).to.equal('spider'); expect(user.username).to.equal('spider');
expect(hookCalled).to.equal(3); expect(hookCalled).to.equal(3);
}); });
}); });
}); });
});
}); });
...@@ -8,7 +8,7 @@ const chai = require('chai'), ...@@ -8,7 +8,7 @@ const chai = require('chai'),
if (Support.sequelize.dialect.supports.upserts) { if (Support.sequelize.dialect.supports.upserts) {
describe(Support.getTestDialectTeaser('Hooks'), () => { describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -20,27 +20,26 @@ if (Support.sequelize.dialect.supports.upserts) { ...@@ -20,27 +20,26 @@ if (Support.sequelize.dialect.supports.upserts) {
values: ['happy', 'sad', 'neutral'] values: ['happy', 'sad', 'neutral']
} }
}); });
return this.sequelize.sync({ force: true }); await this.sequelize.sync({ force: true });
}); });
describe('#upsert', () => { describe('#upsert', () => {
describe('on success', () => { describe('on success', () => {
it('should run hooks', function() { it('should run hooks', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
this.User.beforeUpsert(beforeHook); this.User.beforeUpsert(beforeHook);
this.User.afterUpsert(afterHook); this.User.afterUpsert(afterHook);
return this.User.upsert({ username: 'Toni', mood: 'happy' }).then(() => { await this.User.upsert({ username: 'Toni', mood: 'happy' });
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
}); });
}); });
});
describe('on error', () => { describe('on error', () => {
it('should return an error from before', function() { it('should return an error from before', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
...@@ -50,13 +49,12 @@ if (Support.sequelize.dialect.supports.upserts) { ...@@ -50,13 +49,12 @@ if (Support.sequelize.dialect.supports.upserts) {
}); });
this.User.afterUpsert(afterHook); this.User.afterUpsert(afterHook);
return expect(this.User.upsert({ username: 'Toni', mood: 'happy' })).to.be.rejected.then(() => { await expect(this.User.upsert({ username: 'Toni', mood: 'happy' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).not.to.have.been.called; expect(afterHook).not.to.have.been.called;
}); });
});
it('should return an error from after', function() { it('should return an error from after', async function() {
const beforeHook = sinon.spy(), const beforeHook = sinon.spy(),
afterHook = sinon.spy(); afterHook = sinon.spy();
...@@ -66,15 +64,14 @@ if (Support.sequelize.dialect.supports.upserts) { ...@@ -66,15 +64,14 @@ if (Support.sequelize.dialect.supports.upserts) {
throw new Error('Whoops!'); throw new Error('Whoops!');
}); });
return expect(this.User.upsert({ username: 'Toni', mood: 'happy' })).to.be.rejected.then(() => { await expect(this.User.upsert({ username: 'Toni', mood: 'happy' })).to.be.rejected;
expect(beforeHook).to.have.been.calledOnce; expect(beforeHook).to.have.been.calledOnce;
expect(afterHook).to.have.been.calledOnce; expect(afterHook).to.have.been.calledOnce;
}); });
}); });
});
describe('preserves changes to values', () => { describe('preserves changes to values', () => {
it('beforeUpsert', function() { it('beforeUpsert', async function() {
let hookCalled = 0; let hookCalled = 0;
const valuesOriginal = { mood: 'sad', username: 'leafninja' }; const valuesOriginal = { mood: 'sad', username: 'leafninja' };
...@@ -83,12 +80,11 @@ if (Support.sequelize.dialect.supports.upserts) { ...@@ -83,12 +80,11 @@ if (Support.sequelize.dialect.supports.upserts) {
hookCalled++; hookCalled++;
}); });
return this.User.upsert(valuesOriginal).then(() => { await this.User.upsert(valuesOriginal);
expect(valuesOriginal.mood).to.equal('happy'); expect(valuesOriginal.mood).to.equal('happy');
expect(hookCalled).to.equal(1); expect(hookCalled).to.equal(1);
}); });
}); });
}); });
}); });
});
} }
...@@ -7,7 +7,7 @@ const Support = require('../support'); ...@@ -7,7 +7,7 @@ const Support = require('../support');
const DataTypes = require('../../../lib/data-types'); const DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Hooks'), () => { describe(Support.getTestDialectTeaser('Hooks'), () => {
beforeEach(function() { beforeEach(async function() {
this.User = this.sequelize.define('User', { this.User = this.sequelize.define('User', {
username: { username: {
type: DataTypes.STRING, type: DataTypes.STRING,
...@@ -18,12 +18,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -18,12 +18,12 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
values: ['happy', 'sad', 'neutral'] values: ['happy', 'sad', 'neutral']
} }
}); });
return this.sequelize.sync({ force: true }); await this.sequelize.sync({ force: true });
}); });
describe('#validate', () => { describe('#validate', () => {
describe('#create', () => { describe('#create', () => {
it('should return the user', function() { it('should return the user', async function() {
this.User.beforeValidate(user => { this.User.beforeValidate(user => {
user.username = 'Bob'; user.username = 'Bob';
user.mood = 'happy'; user.mood = 'happy';
...@@ -33,15 +33,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -33,15 +33,14 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
user.username = 'Toni'; user.username = 'Toni';
}); });
return this.User.create({ mood: 'ecstatic' }).then(user => { const user = await this.User.create({ mood: 'ecstatic' });
expect(user.mood).to.equal('happy'); expect(user.mood).to.equal('happy');
expect(user.username).to.equal('Toni'); expect(user.username).to.equal('Toni');
}); });
}); });
});
describe('#3534, hooks modifications', () => { describe('#3534, hooks modifications', () => {
it('fields modified in hooks are saved', function() { it('fields modified in hooks are saved', async function() {
this.User.afterValidate(user => { this.User.afterValidate(user => {
//if username is defined and has more than 5 char //if username is defined and has more than 5 char
user.username = user.username user.username = user.username
...@@ -56,7 +55,7 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -56,7 +55,7 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
}); });
return this.User.create({ username: 'T', mood: 'neutral' }).then(user => { const user = await this.User.create({ username: 'T', mood: 'neutral' });
expect(user.mood).to.equal('neutral'); expect(user.mood).to.equal('neutral');
expect(user.username).to.equal('Samorost 3'); expect(user.username).to.equal('Samorost 3');
...@@ -64,87 +63,77 @@ describe(Support.getTestDialectTeaser('Hooks'), () => { ...@@ -64,87 +63,77 @@ describe(Support.getTestDialectTeaser('Hooks'), () => {
user.mood = 'sad'; user.mood = 'sad';
user.username = 'Samorost Good One'; user.username = 'Samorost Good One';
return user.save(); const uSaved0 = await user.save();
}).then(uSaved => { expect(uSaved0.mood).to.equal('sad');
expect(uSaved.mood).to.equal('sad'); expect(uSaved0.username).to.equal('Samorost Good One');
expect(uSaved.username).to.equal('Samorost Good One');
//change attributes, expect to be replaced by hooks //change attributes, expect to be replaced by hooks
uSaved.username = 'One'; uSaved0.username = 'One';
return uSaved.save(); const uSaved = await uSaved0.save();
}).then(uSaved => {
//attributes were replaced by hooks ? //attributes were replaced by hooks ?
expect(uSaved.mood).to.equal('sad'); expect(uSaved.mood).to.equal('sad');
expect(uSaved.username).to.equal('Samorost 3'); expect(uSaved.username).to.equal('Samorost 3');
return this.User.findByPk(uSaved.id); const uFetched0 = await this.User.findByPk(uSaved.id);
}).then(uFetched => { expect(uFetched0.mood).to.equal('sad');
expect(uFetched.mood).to.equal('sad'); expect(uFetched0.username).to.equal('Samorost 3');
expect(uFetched.username).to.equal('Samorost 3');
uFetched.mood = null; uFetched0.mood = null;
uFetched.username = 'New Game is Needed'; uFetched0.username = 'New Game is Needed';
return uFetched.save(); const uFetchedSaved0 = await uFetched0.save();
}).then(uFetchedSaved => { expect(uFetchedSaved0.mood).to.equal('neutral');
expect(uFetchedSaved.mood).to.equal('neutral'); expect(uFetchedSaved0.username).to.equal('New Game is Needed');
expect(uFetchedSaved.username).to.equal('New Game is Needed');
return this.User.findByPk(uFetchedSaved.id); const uFetched = await this.User.findByPk(uFetchedSaved0.id);
}).then(uFetched => {
expect(uFetched.mood).to.equal('neutral'); expect(uFetched.mood).to.equal('neutral');
expect(uFetched.username).to.equal('New Game is Needed'); expect(uFetched.username).to.equal('New Game is Needed');
//expect to be replaced by hooks //expect to be replaced by hooks
uFetched.username = 'New'; uFetched.username = 'New';
uFetched.mood = 'happy'; uFetched.mood = 'happy';
return uFetched.save(); const uFetchedSaved = await uFetched.save();
}).then(uFetchedSaved => {
expect(uFetchedSaved.mood).to.equal('happy'); expect(uFetchedSaved.mood).to.equal('happy');
expect(uFetchedSaved.username).to.equal('Samorost 3'); expect(uFetchedSaved.username).to.equal('Samorost 3');
}); });
}); });
});
describe('on error', () => { describe('on error', () => {
it('should emit an error from after hook', function() { it('should emit an error from after hook', async function() {
this.User.afterValidate(user => { this.User.afterValidate(user => {
user.mood = 'ecstatic'; user.mood = 'ecstatic';
throw new Error('Whoops! Changed user.mood!'); throw new Error('Whoops! Changed user.mood!');
}); });
return expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejectedWith('Whoops! Changed user.mood!'); await expect(this.User.create({ username: 'Toni', mood: 'happy' })).to.be.rejectedWith('Whoops! Changed user.mood!');
}); });
it('should call validationFailed hook', function() { it('should call validationFailed hook', async function() {
const validationFailedHook = sinon.spy(); const validationFailedHook = sinon.spy();
this.User.validationFailed(validationFailedHook); this.User.validationFailed(validationFailedHook);
return expect(this.User.create({ mood: 'happy' })).to.be.rejected.then(() => { await expect(this.User.create({ mood: 'happy' })).to.be.rejected;
expect(validationFailedHook).to.have.been.calledOnce; expect(validationFailedHook).to.have.been.calledOnce;
}); });
});
it('should not replace the validation error in validationFailed hook by default', function() { it('should not replace the validation error in validationFailed hook by default', async function() {
const validationFailedHook = sinon.stub(); const validationFailedHook = sinon.stub();
this.User.validationFailed(validationFailedHook); this.User.validationFailed(validationFailedHook);
return expect(this.User.create({ mood: 'happy' })).to.be.rejected.then(err => { const err = await expect(this.User.create({ mood: 'happy' })).to.be.rejected;
expect(err.name).to.equal('SequelizeValidationError'); expect(err.name).to.equal('SequelizeValidationError');
}); });
});
it('should replace the validation error if validationFailed hook creates a new error', function() { it('should replace the validation error if validationFailed hook creates a new error', async function() {
const validationFailedHook = sinon.stub().throws(new Error('Whoops!')); const validationFailedHook = sinon.stub().throws(new Error('Whoops!'));
this.User.validationFailed(validationFailedHook); this.User.validationFailed(validationFailedHook);
return expect(this.User.create({ mood: 'happy' })).to.be.rejected.then(err => { const err = await expect(this.User.create({ mood: 'happy' })).to.be.rejected;
expect(err.message).to.equal('Whoops!'); expect(err.message).to.equal('Whoops!');
}); });
}); });
}); });
});
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!