chai
chai copied to clipboard
Error: getaddrinfo ENOTFOUND with Docker in chai test
Hello i have a issue with my project. I made a REST API project in express, i have dockerize all my project (the rest api in express, the database in mariadb and phpmyadmin) i try to test the endpoint of my rest api but i have this error. All the project work perfectly, my docker is up, my api is working, i have tested my posts, my gets, in postman and my browser it's work. But in my chai test i have a Error: getaddrinfo ENOTFOUND mariadb (mariadb is the hostname).
my test.js
let chai=require("chai");
let chaiHttp = require("chai-http");
let server=require("../server");
//Assertion Style
chai.should();
chai.use(chaiHttp);
describe('Task API', ()=>{
describe("GET / ", ()=>{
it("it should Get hello world", (done)=>{
chai.request(server)
.get("/")
.end((err,res)=>{
res.should.have.status(200);
res.text.should.be.a("string");
done();
})
})
})
describe("GET /all", ()=>{
it("it should Get all users", (done)=>{
chai.request(server)
.get("/user/all")
.end((err,res)=>{
res.should.have.status(200);
res.body.should.be.a("array");
done();
})
})
})
});
My package.json's scripts
"scripts": {
"test": "mocha --timeout 10000",
},
my server.js
const pool = require('./db.js')
const env = require('./config/env.js');
const express = require('express');
var bodyParser = require('body-parser')
const Sequelize = require('sequelize');
const sequelize = new Sequelize(env.database, env.username, env.password, {
host: env.host,
dialect: env.dialect,
operatorsAliases: false,
pool: {
max: env.pool.max,
min: env.pool.min,
acquire: env.pool.acquire,
idle: env.pool.idle
}
});
try {
sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
exit()
}
//Create an app
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
let usersRouter = require('./routes/user');
app.use('/user', usersRouter);
app.get('/', (req, res) => {
res.send('Hello world\n');
});
app.get('/test', async (req, res) => {
let conn;
try {
// establish a connection to MariaDB
conn = await pool.getConnection();
// create a new query
var query = "select * from user";
// execute the query and set the result to a new variable
var rows = await conn.query(query);
// return the results
res.send(rows);
} catch (err) {
console.log(err)
throw err;
} finally {
if (conn) return conn.release();
}
});
//Listen port
const PORT = 8080;
if(require.main === module){
app.listen(PORT);
console.log(`Running on port ${PORT}`);
}
module.exports = app;
my routes
var express = require('express');
var router = express.Router();
var user = require("../controllers/UserController");
var google = require("../controllers/GoogleAuthenticatorController");
let JWTMiddleware = require("../middleware/JWT")
// route for register action
router.post('/register', user.doRegister);
// route for login action
router.post('/login', user.doLogin);
//TODO Mettre uniquement en admin ?
router.get('/all', user.getAllUsers);
router.post('/token', user.verificationToken)
router.post('/resendtoken', user.resendToken)
router.post('/terms', user.acceptTerms)
router.delete('/delete', JWTMiddleware, user.deleteAccount)
router.put('/update', JWTMiddleware, user.update)
router.post('/generate/2ftp', JWTMiddleware, google.generateTwoFactorAuthenticationCode)
router.post('/enable/2ftp',JWTMiddleware, google.turnOnTwoFactorAuthentication)
router.post('/verify/2ftp',JWTMiddleware, user.verifyGoogleAuthCode)
router.post('/disable/2ftp',JWTMiddleware, google.turnOffTwoFactorAuthentication)
module.exports = router;
my env and db.config
const env = {
database: 'mapsenseBackBDDR',
username: 'root',
password: 'qwerty',
host: 'mariadb',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
module.exports = env;
const env = require('./env.js');
const Sequelize = require('sequelize');
const sequelize = new Sequelize(env.database, env.username, env.password, {
host: env.host,
dialect: env.dialect,
operatorsAliases: false,
pool: {
max: env.pool.max,
min: env.pool.min,
acquire: env.pool.acquire,
idle: env.pool.idle
}
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
//Models/tables
db.users = require('../models/user.js')(sequelize, Sequelize);
db.verificationtoken = require('../models/vericationtoken.js')(sequelize, Sequelize);
module.exports = db;
When i try to run my test with my request url (localhost:8080) it don't work but it works with an external api (pokeapi) and i receive the data. I don't understand why chai don't seem to work with my localhost with the port 8080. And the Error: getaddrinfo ENOTFOUND step in when the connection to the database is wrong because chai don't find the database. But my database is working and all the project work.
Hi @Nikuu971, got the same problem Did you find the solution ? Thanks