dimanche 29 novembre 2020

Mocha testing with mongo fixtures throws me the Timeout of 2000ms exceeded error

I am trying to implement the mocha and chai simple test but I have an error in the response and I cannot figure out why it cannot pass. There are a few questions similar to mine on Stackoverflow and I tried the solutions and advice given there but with no result so I decided to create another question.

What I want to do:

I want to test the request to /api/flasCards endpoint and It should return me two objects:

[
    {
        "_id": "1",
        "front": "hello",
        "back": "czesc"
    },
    {
        "_id": "2",
        "front": "apple",
        "back": "jablko"
    }
]

These objects are added as fixtures(using node-mongodb-fixtures) because I don't want to test it on a real DB. I am making the request in the test file using chai-http. Below you can see the full test file code.

const DB_URI = "mongodb://localhost:27017/flashCard";
const Fixtures = require("node-mongodb-fixtures");
const fixtures = new Fixtures();

const chai = require("chai");
const expect = require("chai").expect;
const chaiHttp = require("chai-http");

const app = require("../src/app");

chai.use(chaiHttp);

const endpoint = "http://localhost:4000/api/flashCard";

describe("FlashCards", () => {
  before((done) => {
    fixtures
      .connect(DB_URI)
      .then((res) => {
        res.unload();
        res.load();
        done();
      })
      .catch((error) => done(error));
  });

  after((done) => {
    fixtures
      .unload()
      .then(() => {
        fixtures.disconnect();
        done();
      })
      .catch(done);
  });

  it("should allow me to get flashcard if I dont have auth token", (done) => {
    console.log("asdasasdasd");

    chai
      .request(app)
      .get(endpoint)
      .then((res) => {
        const body = res.body;

        expect(res.status).to.equal(200);
        expect(body).to.contain.property("front", "hello");
        expect(body).to.contain.property("back", "czesc");
        done();
      })
      .catch((err) => done(err));
  });
});

The response in the console after running the test command is:

$ npm run test

> flashCards@1.0.0 test C:\Users\pstan\Desktop\flashCards
> mocha --require @babel/register --recursive --exit

[info ] Using fixtures directory: fixtures
[info ] No filtering in use
Listening on port 4000


  FlashCards
(node:16760) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.  
[info ] Using database flashCard
asdasasdasd
[start] load flashcards
[done ] unload flashcards
[done ] *unload all
[done ] load flashcards
[done ] *load all
[done ] *script all
    1) should allow me to get flashcard if I dont have auth token
[done ] unload flashcards
[done ] *unload all


  0 passing (2s)
  1 failing

  1) FlashCards
       should allow me to get flashcard if I dont have auth token:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\pstan\Desktop\flashCards\test\flashcards.js)
      at listOnTimeout (internal/timers.js:549:17)
      at processTimers (internal/timers.js:492:7)

When I am adding more timeout: 10000 in the test running command error message is changed to:

 1) FlashCards
       should allow me to get flashcard if I dont have auth token:
     Error: connect ECONNREFUSED 127.0.0.1:80

I don't understand what I am doing wrong here. I am still learning mocha and to do the tests on the backend so I expect that the problem might be my lack of understanding of how it works but I hope you can help me with that.

Fixtures are created in the DB(checked in the mongo compass) and when I am hitting the endpoint in the postman I have the correct response.

Here you can find full app code repo

Aucun commentaire:

Enregistrer un commentaire