How can I start unit testing my application? First of I wanna test that I can access my SQL database. Second i wanna test how i can test the creation of a new User
I can't seem to find any documentation on this subject
Which kind of test framework can i use, mocha & chai?
This is my azure function for register
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.')
try {
await db.startDb(); //start db connection
} catch (error) {
console.log("Error connecting to the database", error.message)
}
switch (req.method) {
case 'GET':
await get(context, req);
break;
case 'POST':
await post(context, req);
break
default:
context.res = {
body: "Please get or post"
};
break
}
}
async function get(context, req){
try{
let name = req.query.name;
let user = await db.select(name)
context.res = {
body: user
};
} catch(error){
context.res = {
status: 400,
body: `No user - ${error.message}`
}
}
}
async function post(context, req){
try{
let payload = req.body;
await db.insert(payload)
console.log(payload)
context.res = {
body: ["succes"]
}
} catch(error){
context.res = {
status: 400,
body: error.message
}
}
}
This is my DB file
const { Connection, Request, TYPES } = require('tedious');
const config = require('./config.json');
const jwt = require("jsonwebtoken");
const safeJWT = require("../middleware/Jwt")
const bcrypt = require("bcryptjs");
var connection = new Connection(config);
function startDb() {
return new Promise((resolve, reject) => {
connection.on('connect', (err) => {
if (err) {
console.log('Connection failed')
reject(err)
throw (err);
} else {
console.log('Connected')
resolve();
}
})
connection.connect();
})
}
module.exports.sqlConnection = connection
module.exports.startDb = startDb
This is some of my register.js file
fetch("http://localhost:7071/api/register", {
method: 'POST',
body: JSON.stringify({
username: username,
email: email,
password: password
}),
headers: {
"Content-Type": "application/json; charset-UTF-8"
}
}).then((response) =>
response.json()).then((data) => {
if(data[0] = "succes"){
location.href = "login.html"
} else {
alert("failed")
}
}).catch((err) => {
console.log(err)
})
})