mardi 24 novembre 2020

error test unit: node / mocha / chai / sinon

Currently this code brings an error in the unit test, the instant it enters the select. This code is showing normal operation, it was tested in insomnia and it is certainly an error with the test

unit test

import chai from 'chai';
import { restore, stub, assert } from 'sinon';
import sinonChai from 'sinon-chai';
import { query } from 'express';
import model from '../../promocode/model';
const { promocodeModel } = model;
import { registerCampaing, campaign } from '../../util/tests/mocks';
const { expect } = chai;    

let req;
      beforeEach(() => {
        req = {
          query: {
            campaign_id: '5f6260e1a4ec6932990807f8',
          },
        };
      });
      it.only('Should return status 200, campaign and promocodes list',  async() => {
        const teste =  stub(promocodeModel, 'find')
          .withArgs({ campaign_id: query.campaign_id })
          .returns({
          select:()=> Promise.resolve(campaign),
        });
        const res = {
          json: stub().returnsThis(),
          status: stub().returnsThis(),
        };
        await CampaignController.getAllBySearch(req, res);
        assert.calledWithExactly(teste, campaign);
        assert.calledWithExactly(campaign);
        assert.calledOnceWithExactly
        assert.calledWith(200);
      });
      afterEach(() => {
        restore();
      });

controller

async function getAllBySearch(req, res) {
  const { campaign_id: id } = req.query;
  const ObjectId = mongoose.Types.ObjectId;
  let match = {};
  if (id) {
    match = {
      ...match,
      campaign_id: ObjectId(id),
    };
  }
  

const data = await promocodeModel.find(match).select('+consumers')

  return res.json(data);
}

error

1) Get /campaign/search-promocodes/
       Should return status 200, campaign and promocodes list:
     TypeError: Cannot read property 'select' of undefined
      at Object.getAllBySearch (src/campaign/controller/campaign.controller.js:114:46)
      at Context.<anonymous> (src/campaign/controller/campaign.controller.test.js:118:22)
      at processImmediate (internal/timers.js:461:21)

o erro que tras é TypeError: Cannot read property 'select' of undefined

Aucun commentaire:

Enregistrer un commentaire