I am trying to test a (to me) complex piece of code that I would like to just return dummy data so I can do assertions on how it is handled. Here is the code:
const { sql, dbConnPoolPromise } = require('../database/db.js');
router.get('/', async (req, res) => {
try {
const pool = await dbConnPoolPromise
const result = await pool.request()
.query(SQL_SELECT_ALL);
res.status(200);
res.json(result.recordset[0]);
} catch (err) {
res.status(500);
res.send("ERROR, General Get" + err.message);
}
});
I want to replace the const result with a set piece of dummy data, thus mocking the response a SQL server might give. I cannot figure out how to do this. I am currently using Jest as my testing suite and was hoping to use spyOn or Jest.fn() to replace it but that only works on functions. Is there a way in jest to replace variables? I cannot replace the entire router.get as I am testing that res.status(200) (amongst others) is being sent correctly to the client, I only want to replace the value of the variable.
If required the contents of dbConnPoolPromise are:
let sql = require('mssql');
const config = require('config');
let dbConnPoolPromise = new sql.ConnectionPool(config.get('connection'))
.connect()
.then(pool => {
return pool
})
.catch(err => console.log('Database Connection Failed - error: ', err))
module.exports = {
sql, dbConnPoolPromise, buildSelect
};