DEV Community

almantas88
almantas88

Posted on

How to do Firebase, NodeJS, ExpressJS API endpoint unit tests?

I'm lost, I have been searching for good resources but I can't find any. How to do unit testing for a back-end NodeJS, ExpressJS, Firestore, RESTful app? What tools to use? How to mock data? I don't want to have a test database for testing, I want to mock data. Can any one help me?

Example of an endpoint:

router.post("/color", async (request, response) => { 

    if(Object.keys(request.body).length === 0) return response.status(406).send("Ups... There was missing data.");

    try {
        await db.collection('Colors').add({
            realColor: request.body.realColor,
            colorMap: request.body.colorMap
        });
        response.status(201).send("Success! A color was creted!");
    }
    catch (error) {
        console.log(error);
        response.status(400).send("Ups... A color was NOT creted!");
    }
});
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
ayrtonvwf profile image
Ayrton Fidelis

Did you find your way through it? I'm curious about it too.

Collapse
 
almantas88 profile image
almantas88

Sadly, no. We just mock data with sinnon, and check response statuses, but i dont think its correct unit tests

Collapse
 
ayrtonvwf profile image
Ayrton Fidelis

Maybe use something like Firebase Emulator or Firebase Mock?

Collapse
 
utkarsh23 profile image
Utkarsh Ghadage

did you find any way guys ??