mardi 28 mai 2019

How to test a routing handler calling via mock from express

I'm unit-testing some code of mine in nodejs that works with express. While it seems obvious, I want to test that my routes are calling the right handler functions.

I'm using jest to create a mock function for the handler function. While I successfully tried to test that a function is called inside another function, I cannot seem to test that a function (handler) is called from express get of post. Let's see an examplee:

fruits.js

function orange (req,res,next) {res.status(200)} ;
module.exports = {orange}

app.js

const fruits = require('./fruits')
const express = require('express');
const app = express();

app.get('/fruits', fruits.orange);

module.exports = { app };

When I'm trying to test (with jest) that GET request (with the help of axios module) to /fruits is calling orange(), altough actually orange() works, it won't be saved as a calling with a mock function cover of spy cover.

How can I test that orange() has been called from the GET request ?

Aucun commentaire:

Enregistrer un commentaire