README
Motivation
This is fork from sequelize-test-helpers. This library cut down features from original library and make it testing framework agnostick.
Getting started
A library for unit testing your Sequelize models and code that needs those models.
Installation
Add sequelize-easy-mock
as a devDependency
:
npm i -D sequelize-easy-mock
Examples
sequelize.define
Unit testing models created with Note: See below for how to test models created using Model.init
Let's say you have a Sequelize model User
as follows:
src/models/User.js
const model = (sequelize, DataTypes) => {
const User = sequelize.define(
'User',
{
age: {
type: DataTypes.INTEGER.UNSIGNED
},
firstname: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
lastname: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
lowercase: true,
validate: {
isEmail: true,
notEmpty: true
}
},
token: {
type: DataTypes.STRING,
validate: {
notEmpty: true
}
}
},
{
indexes: [
{ unique: true, fields: ['email'] },
{ unique: true, fields: ['token'] }
]
}
)
User.associate = ({ Company }) => {
User.belongsTo(Company)
}
return User
}
module.exports = model
test/unit/models/User.spec.js
const { sequelize, dataTypes } = require('sequelize-easy-mock')
const UserModel = require('../../src/models/User')
describe('src/models/User', () => {
const User = UserModel(sequelize, dataTypes)
const user = new User()
// TODO: Write your teste here
// ...
})
Prerequisites
Initialisation
npm install
Test it
npm test
— runs the unit testsnpm run test:unit:cov
— runs the unit tests with code coveragenpm run test:mutants
— runs the mutation tests
Lint it
npm run lint