mercredi 4 décembre 2019

Error: ENOENT: no such file or directory, open '../test/testFiles/shadab.jpeg' test cases for file upload using chai

I am trying to test my node API with Mocha/Chai. I created a "test" folder which contain File.js with this code inside and the same test folder contains a folder called testFile in which I have one image "shadad.jpeg". The test.js file contains the following code

describe("/POST upload", () => {
    it("it should POST a single file upload", done => {
        chai
            .request(server)
            .post("/upload")
            .field("Content-Type", "multipart/form-data")
            .attach(
                "file",
                fs.readFileSync("./testFiles/shadab.jpeg"),
                "shadab.jpeg"
            )
            .end((err, res) => {
                res.should.have.status(200);
                res.body.should.be.a("object");
                res.body.should.have.property("success").eql(true);
                done();
            });
    });
});

But when I am doing the command npm test, I got the error

Error: ENOENT: no such file or directory, open '../test/testFiles/shadab.jpeg'

With PostMan I am testing my api and sending from form-data key as "file" and value as the file "shadab.jpeg". And getting this response.

{
    "success": true,
    "data": "Added Successfully"
}

What am I doing wrong here? Please help.

Aucun commentaire:

Enregistrer un commentaire