How can I stub the knex method(insert, update, delete) properly?

Viewed 90

I can't seem to properly stub the insert, update, and del() in my case.

I have figured out stub the createTable so far.

config.ts:

    export sync function method1(knex: Knex): Promise<Knex.SchemaBuilder>{

        await knex.schema.hashTable(tableA).then(async exists => { 
           if(!exists){ 
              await knex.schema.createTable(tableA, (xxxx: Knex.TableBuilder) => {
                 // do something
               }
            }
        });

        await knex(tableA).dele().where('colA' == 'conditionA');
        await knenx.insert(data).into(tableA);
        await knenx.update({colB, something}).into(tableA);
    }

test.ts:

import chai from 'chai';
import sinon from 'sinon';

import { method1 } from '../config.ts';

     describe('test', () => {
       let knexStub: any;

       beforeEach(() => {
         knexStub = {
                     schema: {
                               createTable: sinon.stub().resolves(),
                               hasTable: sinon.stub().resolves(Promise.resolve(false))
                     },
                     insert: sinon.stub().resolves()
         };
       });
     }

     it('method1 test', async () => {
          method1(knexStub);
          // assert call once delete
          // assert call once update
          sinon.assert.calledOnce(knexStub.schema.createTable);

     });

Is there a way to stub the knenx.insert(xxx).into(yyy), knenx.update(xxx).into(yyy) and knex(yyy).dele().where(xxx)?

stubbing the knenx.insert(xxx) is working but not with the .into().

I am wondering if anyone can see what I am missing.

0 Answers
Related