1

I have the following code:

async save(id: string) {
 const person = await PersonModel.findOne({
  where: { id: id },
 });

 if (!person) {
  await PersonModel.create({
    id: '2345',
    name: 'John Doe',
    age: 25
  });
  return;
 }

 await person.increment({ age: 15 });
}

Now, I wanted to test person.increment() in which the age will be added with 15. I have the following code to escape the condition that will create a new record for the model.

const findOneFake = sinon.spy(() => {
  return {}; //returns empty object or true
});

const proxy = (proxyquire('./path/to/file.ts', {
  './path/to/PersonModel.ts': {
    default: {
      findOne: findOneFake
    }
  }
})).default;

beforeEach(async () => {
  await save();
});

it('should increment age with 15');

How am I going to do that? What do I do to test it? I can use sinon.fake() to PersonModel.create or PersonModel.update but I am troubled testing the instance of a Sequelize Model.

0 Answers0