Invoke chai request in loop inside Mocha test

Viewed 15

I'm trying to figure out how to get the chai request invoked inside the first chai callback and then test the list of results.

I receive this error:

usr/local/lib/node_modules/mocha/lib/runner.js:936 throw err; ^

TypeError: expect(...).to.be is not a function at /Users/jonathan/EWM/bitbucket/qa-latamfx/api_administrators_test/credit_service.js:43:38 at Test.Request.callback (/Users/jonathan/EWM/bitbucket/qa-latamfx/node_modules/superagent/lib/node/index.js:716:12) at /Users/jonathan/EWM/bitbucket/qa-latamfx/node_modules/superagent/lib/node/index.js:916:18 at IncomingMessage. (/Users/jonathan/EWM/bitbucket/qa-latamfx/node_modules/superagent/lib/node/parsers/json.js:19:7) at IncomingMessage.emit (node:events:402:35) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)

describe("Instruments", function () {
  const endpoint = "/instruments";


  it("Get all", (done) => {
    chai
      .request(URL)
      .get(endpoint)
      .end((err, res) => {
        expect(res, JSON.stringify(res)).to.have.status(200);
        expect(res.body.isSuccess).to.be.true;
        expect(res.body.count).to.be.gt(0);
        expect(res.body.data).to.have.lengthOf(res.body.count);
        res.body.data.forEach(instrument => {
            expect(instrument.idInstrument).to.be.a("number");
            expect(instrument.instrumentPair).to.match(/[A-Z]{3}\/[A-Z]{3}/);
            expect(instrument.idAssetClass).to.be.a("number");
            expect(instrument.assetClassName).to.be.a("string");
            chai
                .request(URL)
                .get(`${endpoint}/${instrument.instrumentPair.replace("/","-")}`)
                .end((err, res) => {
                    console.log(instrument.instrumentPair);
                    expect(res, JSON.stringify(res)).to.have.status(200);
                    expect(res.body.isSuccess).to.be.true;
                    console.log(res.body);
                    expect(res.body.count).to.be(1);
                });
        });
        done();
      });
  });
});
0 Answers
Related