I’m newbie with jest and I have made a test to check several calls to datababase from an url.
I run the test with this command:
npx jest --watchAll file.test.js
And everything works fine. But, If I modify a comma, for example, the test is running again and I’ve got errors. I haven’t any “OK” from the api and I’ve got this error too:
TypeError: Cannot read properties of undefined (reading '0')
When I try to check what I have received with what I have expected, like we can see in this line:
expect(1).toBe(result.data[0].id);
If I stop the test and run again with: npx jest –watchAl file.test.js everything works fine.
What happend? Why Have I got these errors when the test is running for twice?
The code of my test is:
import config from "../config";
import {getData} from "../com/functions";
describe("Test several connections with Postgress making a request via POST", () => {
describe("Test read data from database", () => {
test("Test 1: Read data from tbl001_users", async () => {
let data = {
"query": 1,
"origin": "Mapping-Service"
};
let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
console.log(result);
expect(1).toBe(result.data[0].id);
expect("OVT").toBe(result.data[0].code);
expect(2).toBe(result.data[1].id);
expect("TTHOT").toBe(result.data[1].code);
});
test("Test 2: Read data from tbl001_users", async () => {
let data = {
"query": 1,
"origin": "Mapping-Service"
};
let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
expect(1).toBe(result.data[0].id);
expect("OVT").toBe(result.data[0].code);
expect(2).toBe(result.data[1].id);
expect("TTHOT").toBe(result.data[1].code);
});
test("Test 3: Read data from tbl001_users", async () => {
let data = {
"query": 1,
"origin": "Mapping-Service"
};
let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
expect(1).toBe(result.data[0].id);
expect("OVT").toBe(result.data[0].code);
expect(2).toBe(result.data[1].id);
expect("TTHOT").toBe(result.data[1].code);
});
test("Test 4: Read data from tbl002_suppliers", async () => {
let data = {
"query": 2,
"params": ["OVT"],
"origin": "Mapping-Service"
};
let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
expect("OK").toBe(result.result);
expect(1).toBe(result.data.length);
});
test("Test 5: Read data from tbl002_suppliers", async () => {
let data = {
"query": 2,
"params": ["OVT"],
"origin": "Mapping-Service"
};
let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
expect("OK").toBe(result.result);
expect(1).toBe(result.data.length);
});
test("Test 6: Read data from tbl002_suppliers", async () => {
let data = {
"query": 2,
"params": ["OVT"],
"origin": "Mapping-Service"
};
let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
expect("OK").toBe(result.result);
expect(1).toBe(result.data.length);
});
});
});
The file where I dispatch the requests has this code:
import express from "express";
import {getData} from "../com/functions";
import config from "../config";
import cors from "cors";
import {YoumalouDDBB} from "../database/Youmalou/youmalou_ddbb";
const router = express.Router();
const ddbb = new YoumalouDDBB();
async function start(){
await ddbb.connect();
}
start();
let url = null;
let urls = null;
if ((config.node_env === "test") || (config.node_env === "development") || (config.node_env === "production")){
url = `${config.frontend.protocol}://${config.frontend.domain}:${config.frontend.port}`;
urls = `${config.frontend.protocols}://${config.frontend.domain}:${config.frontend.port}`;
}
const corsOptions = {
origin: [url, urls],
optionSuccessStatus: 200,
methods: ["POST", "OPTIONS"],
};
if ((config.node_env === "test") || (config.node_env === "development") || (config.node_env === "production")){
router.all("*", cors(corsOptions));
}
router.post("/api/youmalou", async(req, res)=> {
let result = null;
if (typeof(req.body.query) === "number"){
console.log(req.body);
result = await ddbb.getDataFromDDBB(req.body.query, req.body.params);
}
res.json(result.data);
});
module.exports.Router = router;