Test utils for sequelize
$ npm install --save-dev sequelize-test-utils
user migration:
export default {
up(queryInterface, Sequelize) {
return queryInterface.createTable('users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
email: {
allowNull: false,
type: Sequelize.STRING,
},
});
},
down(queryInterface) {
return queryInterface.dropTable('users');
},
};
import chai from 'chai';
import dirtyChai from 'dirty-chai';
import sinonChai from 'sinon-chai';
chai.use(dirtyChai);
chai.use(sinonChai);
import { expect } from 'chai';
import { spy } from 'sinon';
import Sequelize from 'sequelize';
import { createQueryInterfaceMock, field } from 'sequelize-test-utils';
import { up, down } from '../20160827174306-create-user';
describe('#up', () => {
it('creates users table', () => {
const queryInterface = createQueryInterfaceMock(spy);
up(queryInterface, Sequelize);
expect(queryInterface.createTable).to.have.been.calledOnce();
const call = queryInterface.createTable.getCall(0);
expect(call.args[0]).to.equal('users');
});
it('table has auto increment id', () => {
const queryInterface = createQueryInterfaceMock(spy);
up(queryInterface, Sequelize);
const call = queryInterface.createTable.getCall(0);
const idField = field(call.args[1].id);
expect(idField.allowNull()).to.be.false();
expect(idField.isAutoIncrement()).to.be.true();
expect(idField.isPrimaryKey()).to.be.true();
expect(idField.isInteger()).to.be.true();
});
});
describe('#down', () => {
it('drops users table', () => {
const queryInterface = createQueryInterfaceMock(spy);
down(queryInterface, Sequelize);
expect(queryInterface.dropTable).to.have.been.calledWith('users');
});
});
import Sequelize from 'sequelize';
import { createQueryInterfaceMock, field } from 'sequelize-test-utils';
import { up, down } from '../20160827174306-create-user';
describe('#up', () => {
it('creates users table', () => {
const queryInterface = createQueryInterfaceMock(jest.fn);
up(queryInterface, Sequelize);
expect(queryInterface.createTable).toBeCalled();
const call = queryInterface.createTable.mock.calls[0];
expect(call[0]).to.equal('users');
});
it('table has auto increment id', () => {
const queryInterface = createQueryInterfaceMock(jest.fn);
up(queryInterface, Sequelize);
const call = queryInterface.createTable.mock.calls[0];
const idField = field(call[1].id);
expect(idField.allowNull()).toBe(false);
expect(idField.isAutoIncrement()).toBe(true);
expect(idField.isPrimaryKey()).toBe(true);
expect(idField.isInteger()).toBe(true);
});
});
describe('#down', () => {
it('drops users table', () => {
const queryInterface = createQueryInterfaceMock(jest.fn);
down(queryInterface, Sequelize);
expect(queryInterface.dropTable).toBeCalledWith('users');
});
});
MIT © C.T. Lin