Run rest API tests from docker compose - node.js

I'm trying to implement tests in my node.js project. I decided to use mocha / chai / chai-http to test my rest API.
I just have 1 simple file in my test folder:
movie.ts:
import chai from 'chai';
import chaiHttp from 'chai-http';
let app = require('../app');
chai.should();
chai.use(chaiHttp);
declare var process : {
env: {
API_KEY: string
}
}
describe('/GET movies', () => {
it('it should GET all the movies', (done) => {
chai.request(app)
.get('/v1/movies')
.set("Authorization", process.env.API_KEY)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
done();
});
});
});
Here is my dockerfile:
FROM node:latest
WORKDIR /app/
COPY package.json .
RUN npm install
COPY . .
Docker compose:
version: '3.8'
services:
mariadb:
image: mariadb
env_file: ./.env
environment:
MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
MYSQL_USER: $MYSQL_USER
MYSQL_PASSWORD: $MYSQL_PASSWORD
MYSQL_DATABASE: $MYSQL_DATABASE
ports:
- $MYSQL_LOCAL_PORT:$MYSQL_DOCKER_PORT
volumes:
- mysql:/var/lib/mysql
- mysql_config:/etc/mysql
- ./sql/:/docker-entrypoint-initdb.d/
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_HOSTS: mariadb
web:
build: .
env_file: ./.env
command: npm start
volumes:
- .:/app/
- /app/node_modules
ports:
- $NODE_LOCAL_PORT:$NODE_DOCKER_PORT
depends_on:
- mariadb
environment:
MYSQL_HOST: mariadb
web-tests:
image: hypescript_web
command: npm test
environment:
MYSQL_HOST: mariadb
depends_on:
- mariadb
- web
volumes:
mysql:
mysql_config:
This test is working but I have 2 problems when I run it during docker compose up cmd.
As you can see I created another container just for the test and I'm not sure if it's the best practice to test my API. web-tests container uses my node.js image built in the docker compose so if I don't have the image already built in my machine I have to comment all my web-tests services and when the image is built then I can uncomment web-tests and I can use it to run mocha tests.
The 2nd problem is that I need to set a timeout to run my test (e.g after 10 seconds) because I build my database at the same time and the first time I'll get database errors because the tests are running and the database init isn't finished. I tried to add a timeout to mocha in the package.json but the timeout is ignored.
"scripts": {
"start": "nodemon -L app.ts",
"debug": "export DEBUG=* && npm run start",
"test": "mocha --timeout 10000 -r ts-node/register test/**/*.ts"
}
I think I'm doing something wrong because it's very heavy to run my API tests.
I'd like to runs my tests without comment / uncomment at the first run and add a timeout.
How do you do to runs your API tests in docker ?

Related

How to configure webpack hot reload to work inside Docker?

I'm working on a Symfony 4 project for months, and I want to Dockerize it.
I make everything work except Webpack, I use it to compile my .scss and .js files with the npm run watch or npm run dev command.
Actually webpack does not listen changes I do in a .scss or .js file for example.
Here is my config, I surely miss something in my files.
My docker-compose.yml :
version: '3.8'
services:
mysql:
image: mysql:8.0
command: --default-authentication-plugin=mysql_native_password
restart: on-failure
environment:
MYSQL_ROOT_PASSWORD: rootpassword
phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: on-failure
depends_on:
- mysql
ports:
- '8004:80'
environment:
PMA_HOSTS: mysql
php:
build:
context: .
dockerfile: php/Dockerfile
volumes:
- '../.:/usr/src/app'
restart: on-failure
env_file:
- .env
nginx:
image: nginx:1.19.0-alpine
restart: on-failure
volumes:
- '../public:/usr/src/app'
- './nginx/default.conf:/etc/nginx/conf.d/default.conf:ro'
ports:
- '80:80'
depends_on:
- php
node:
build:
context: .
dockerfile: node/Dockerfile
volumes:
- '../.:/usr/src/app'
command: npm run watch
My Dockerfile for Node Image :
FROM node:12.10.0
RUN apt-get update && \
apt-get install -y \
curl
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
WORKDIR /usr/src/app
CMD ["npm", "run", "watch"]
My webpack.config.js :
var Encore = require('#symfony/webpack-encore');
var CopyWebpackPlugin = require('copy-webpack-plugin');
if (!Encore.isRuntimeEnvironmentConfigured()) {
Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
Encore
.setOutputPath('public/build/')
.setPublicPath('/build')
.addEntry('app', './assets/js/app.js')
.splitEntryChunks()
.disableSingleRuntimeChunk()
.enableSassLoader()
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.configureBabel(() => {}, {
useBuiltIns: 'usage',
corejs: 3
})
.addPlugin(new CopyWebpackPlugin([
{ from: './assets/pictures', to: 'pictures' }
]))
;
module.exports = Encore.getWebpackConfig();
// module.exports = {
// mode: 'development',
// devServer: {
// port: 80,
// host: '0.0.0.0',
// disableHostCheck: true,
// watchOptions: {
// ignored: /node_modules/,
// poll: 1000,
// aggregateTimeout: 1000
// }
// }
// }
As you can see I already tried some thing in webpack.config.js, I saw many things about watchOptions but I didn't get it.
And here is my project's organisation :
project's organisation
I want to be able to launch my Docker with Webpack listening any change I do in real time.
Here is the command console after running docker-compose up:
console command docker-compose up
If you have some advise to improve my Docker environment, I take it all !
Thank you !
i just use this:
docker-compose.yml:
node:
image: node:16-alpine3.13
working_dir: /var/www/app
user: "$USERID"
volumes:
- .:/var/www/app
tty: true
and docker-compose exec node yarn watch
working as expected.
Okay i think i solved my issue,
I followed #Rufinus answer; i had to docker-compose up in a first console command, open a second console command and execute winpty docker-compose exec node yarn watch but for some reason i had issue with node-sass compatibility : i mounted my node_module (windows 10) folder into the container (Linux).
So i opened my node CLI container and execute npm rebuild node-sass to solve this and finally it worked !
But i don't know why, my current solution is to execute npm run watch on my local folders (like i used to do it before Dockerizing all my application) and it re-builds assets when i change .scss or .js file.

Why does VueJS not run production build?

I'm using a Docker-compose to initialize ExpressJS + VueJS and the RestFull API
This is docker compose:
version: '3'
services:
webserver:
build: ./webserver
ports:
- "3000:3000"
container_name: boleto_webserver
networks:
- boleto
volumes:
- ./webserver:/app/webserver
- /app/webserver/node_modules
website:
build: ./website
ports:
- "8080:8080"
container_name: boleto_website
networks:
- boleto
volumes:
- ./website:/app/website
- /app/webiste/node_modules
api:
build: ./api
ports:
- "3030:3030"
container_name: boleto_api
networks:
- boleto
volumes:
- ./api:/app/api
- /app/api/node_modules
networks:
boleto:
external: true
name: boleto
In DockerFile inside Website (VueJS) I run a production build:
FROM node:12
WORKDIR /app/website
COPY package*.json ./
COPY . /app/website
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm","run","build"]
Browsing localhost:3000 here is where the webserve is being launched I have this message:
Error: ENOENT: no such file or directory, stat '/app/website/build/index.html'
Analyzing website logs:
Note that the development build is not optimized.
To create a production build, run npm run build.
If I run this applications separately, all them works well! Throwing inside a docker compose this happens! Anyone can help me?
Updating
The webserver insists on run script dev, if I remove it, it does not compile
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "nodemon app.js",
"dev": "nodemon app.js"
},
FROM node:12
WORKDIR /app/webserver
COPY package*.json ./
COPY . /app/webserver
RUN npm install && npm install nodemon
EXPOSE 3000
CMD ["npm","run","serve"]

Why NextJS using Docker container did not reload after changed code for dev environment?

I'm trying to run the NextJS on a Docker container using Dockerfile and running via docker-compose, after I changed my code in a JS file (such as index.js) the Next server did not reload.
But when I've tried to run outside without using Docker (by executing the "npm run dev" command directly) the Next server did reload smoothly.
I've also tried to run the server by "nodemon" command (inside a container), it did not make it either.
Dockerfile:
FROM node:10.14.2-alpine
COPY . /home/next_app
WORKDIR /home/next_app
RUN npm install
docker-compose.yml:
version: "3.6"
services:
self_nextjs:
container_name: self_nextjs
build:
context: ./app
dockerfile: Dockerfile
ports:
- 3000:3000
volumes:
- ./app:/home/next_app
- /home/next_app/node_modules
networks:
- zen_frontend
restart: always
command: npm run dev
networks:
zen_frontend:
name: zen_frontend
driver: bridge
Any suggestions would be appreciated.
I had the same issue on Windows 10. I followed some of the instructions in this thread https://github.com/zeit/next.js/issues/6417. Basically, you have to add a next.config.js to poll for changes. I'm not sure if MacOS has the same problem.
module.exports = {
webpackDevMiddleware: config => {
config.watchOptions = {
poll: 800,
aggregateTimeout: 300,
}
return config
},
}
Have you tested by exposing webpack default hot reload port?
add to your Dockerfile
...
EXPOSE 49153
...
and update your docker-compose.yml
version: "3.6"
services:
self_nextjs:
container_name: self_nextjs
build:
context: ./app
dockerfile: Dockerfile
ports:
- 3000:3000
- 49153:49153
volumes:
- ./app:/home/next_app
- /home/next_app/node_modules
networks:
- zen_frontend
restart: always
command: npm run dev
networks:
zen_frontend:
name: zen_frontend
driver: bridge
Hope this help,
Regards
I had to change #davidatthepark solution a little bit to get it to work for me. It looks like webpackDevMiddleware is not supported any more.
module.exports = {
webpack: (config, _) => ({
...config,
watchOptions: {
...config.watchOptions,
poll: 800,
aggregateTimeout: 300,
},
}),
}

WebStorm 2018.1: I am not able to hit breakpoints running remote debugging typescript from docker container

I am using the docker integration tool to run docker-compose to start two containers, one for node and one for mongodb.
Here is the docker-compose.yml file:
version: '2.1'
services:
mongo:
container_name: "app_mongo"
hostname: "mongo"
tty: true
image: mongo:latest
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
- MONGO_INITDB_DATABASE=***********
- MONGO_INITDB_ROOT_USERNAME=************
- MONGO_INITDB_ROOT_PASSWORD=********************
volumes:
- /data/db:/data/db
ports:
- 27017:27017
command: "mongod --smallfiles --auth"
networks:
- my-app-network
group:
container_name: "app_api1"
hostname: "api1"
build:
context: .
dockerfile: api1.dev.yml
entrypoint: ["npm", "run", "debug"]
volumes:
- ".:/home/app"
ports:
- 3000:3000
- 56745:56745
depends_on:
- "mongo"
networks:
- my-app-network
networks:
my-app-network:
driver: bridge
Here is the api1.dev.yml file:
FROM node:latest
ADD package.json /tmp/package.json
RUN cd /tmp && npm install --production && npm install -g nodemon
RUN mkdir -p /home/app && cp -a /tmp/node_modules /home/app/ && mkdir -p /home/app/dist
ADD package.json /home/app/package.json
ADD .env /home/app/.env
WORKDIR /home/app
Here is the script entry in package.json:
"scripts": {
"debug": "nodemon --inspect=56745 --require ts-node/register app/app.ts"
// "debug": "nodemon -L --inspect=56745 dist/myapp/app.js"
}
I also added a new "Attach to Node.js/Chrome" item to attach to the debugging port for node.
I run the docker-compose file followed by debugging the "Attach to Node.js/Chrome" item after node is up and listening.
When I try to hit a breakpoint in a .ts file, nothing is happening. I am seeing the endpoint is called.
What are the steps involved in debugging a typescript app from docker and what am I doing wrong?
Where can I find a good tutorial that walks through how to debug typecript for a node.js app hosted inside of a docker container?
An answer was finally provided to me. Ultimately, I had to change the debug script from "debug": "nodemon -L --inspect=56745 dist/myapp/app.js" to "nodemon -L --inspect=0.0.0.0:56745 dist/myapp/app.js".

Why is my docker node container exiting

I'm trying to run a node container with docker-compose -
services:
node:
build:
context: nodejs/
ports:
- "3000:3000"
volumes:
- ../nodejs:/usr/src/app
working_dir: '/usr/src/app'
My docker file
FROM node:6.10
EXPOSE 3000
The problem is it exits immediately -
$ docker-compose up
Starting docker_node_1
Attaching to docker_node_1
docker_node_1 exited with code 0
And there's nothing in the logs - docker logs docker_node_1 returns nothing.
There's a package.json referencing the main script -
{
...
"main": "server.js",
...
}
And my main script is just a simple express server -
const express = require('express');
const app = express();
const port = 3000;
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err);
}
console.log(`server is listening on ${port}`);
});
I guess I'm missing something obvious but I can't see what it is...
It's missing specifying the docker command. That is the key concept that represents the container: sort of isolated process (process = the command, your program)
You can do it in Dockerfile:
CMD npm start
Or in docker-compose.yml:
services:
node:
command: npm start
build:
context: nodejs/
ports:
- "3000:3000"
volumes:
- ../nodejs:/usr/src/app
working_dir: '/usr/src/app'
Both approaches are equivalent. But edit it as your needs (npm run, npm run build, etc)

Resources