0

So, basically I am developing my first MERN stack project in which I have an api endpoint which resets the collection vehicles. I delete the data from the collection, then I parse the data from a csv and convert them to json and insert the new data. When running my backend code, everything runs smoothly in postman and robo3T. But when I run my testing code, the execution hangs at the instruction:
const vehicles_model = await tojson().fromFile(vehicles_csv);
and consequently the testing fails (res.status 500 is returned). Also, while running the testing code, the endpoint fails to run in postman, as well. What am I doing wrong?
PS: It is my first time writing on stackoverflow, if I haven't made the problem clear, I'll be happy to elaborate :)

this is my endpoint

exports.postResetVehicles = async (req, res) => {
   const vehicles_csv = "vehicles.csv";
   try {
       Vehicles.deleteMany({}).then(function () {
          console.log("Data deleted and");
       });

       const vehicles_model = await tojson().fromFile(vehicles_csv);

       Vehicles.insertMany(vehicles_model).then(function () {
           console.log("data inserted"); // Success
           console.log("=>Data reseted");
       }).catch(function (error) {
           console.log(error); // Failure
       });
       return res.status(200).json({ "status": "OK" });
   } catch (err) {
       return res.status(500).json({ "status": "failed", "error": err });
   }
};

and this is my testing code

describe("Testing 'admin/resetvehicles'", () => {
    it("should succeed if Vehicles collection is reset", async () => {
        const response = await request(app).post(
            "/interoperability/api/admin/resetvehicles"
        );
        expect(response.status).to.equal(200)
    });
});
jac
  • 1
  • Just FYI, in general, you don't want to actually hit endpoints in your unit tests. It's best to abstract that logic out into it's own function, spy on that method, and return a dummy value. – Pytth Mar 03 '22 at 00:35
  • Thanks for the advice! What do you mean by abstracting that logic out into its own function? – jac Mar 03 '22 at 20:11
  • I tried typing it here, but take a look at [this gist](https://gist.github.com/chief10/29f9c3217223aa097e6c58b44c757d75) I put together for you. I hope that explains things a little better. – Pytth Mar 10 '22 at 17:33

0 Answers0