Any module helps to test sequelize model with jest

Viewed 266

I am doing a unit test on sequelize (5.x) models. Here is an example of Group model:

class Group extends Model {};
Group.init({
    name: {type: Sql.STRING,
          validate: {
            len:{
              args:[1,50],
              msg:"Name between 1 to 50 chars"
            } ,
          }
    },
    owner_id: {
      type: Sql.INTEGER,
      allowNull:false,
      validate: {
        isInt: true,
      },
    },   
    status: {type: Sql.STRING,
             validate: {
              isIn: [['active', 'inactive']],
             },

    },
    group_data: {
        type: Sql.JSONB
    },
    last_updated_by_id: {type: Sql.INTEGER},
    },
    createdAt: Sql.DATE,
    updatedAt: Sql.DATE
  }, {
    sequelize:db,
    modelName: 'group',
    indexes: [
      { 
        fields: ['name', 'owner_id']
      }, {
        fields: ['owner_id', 'status']
      }, {
        fields: ['owner_id']
      }, {
        fields: ['status']
      }
    ]   

  });

Here is the test I am doing the test:

it("shall create a group", () => {
        expect(async () => {await Group.create(payload)}).not.toThrow;  //payload is defined before test

    })

But the test causes error for the callback of the DB connection as the test invokes the connection to the DB.

There is a module sequelize-test-helper which does model test nicely but requires Mocha framework. I did extensive search for a similar jest helper module with no avail. Is there any module I can use to help unit test sequelize model with jest? Or how shall I unit test sequelize model in jest? I found little information about jest with sequelize model.

0 Answers
Related