Skip to content

Instantly share code, notes, and snippets.

@davidroman0O
Created April 16, 2019 19:35
Show Gist options
  • Save davidroman0O/ab79712972b1a32380244649ff14dd3c to your computer and use it in GitHub Desktop.
Save davidroman0O/ab79712972b1a32380244649ff14dd3c to your computer and use it in GitHub Desktop.
Moleculer Jest Setup for docker-compose unit testing
version: '3.0'
volumes:
database_data:
driver: local
services:
nats:
image: nats
ports:
- "4222:4222"
traefik:
image: traefik:latest
command: --web --docker --docker.domain=docker.localhost --logLevel=INFO --docker.exposedbydefault=false
ports:
- "3000:80"
- "8080:8080"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /dev/null:/traefik.toml
k8s-db:
image: postgres
restart: always
ports:
- '30100:5432'
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- database_data:/var/lib/postgresql/data
k8s-svc-hi:
image: k8s-david/hi.api:${TAG:-latest}
build: ../k8s-svc-hi
env_file: docker-compose.env
tty: true
command: npm run dev
volumes:
- ./app
links:
- k8s-db
- nats
depends_on:
- nats
- k8s-db
volumes:
- ../k8s-svc-hi:/app
environment:
DATABASE_URL: postgres://postgres:postgres@k8s-db:5432
NODE_ENV: development
## to connect directly ::
## docker-compose exec k8s-svc-units sh
k8s-svc-units:
image: k8s-david/units.api:${TAG:-latest}
build: ../k8s-svc-units
env_file: docker-compose.env
tty: true
stdin_open: true
command: npm run dev
volumes:
- ./app
links:
- nats
depends_on:
- nats
volumes:
- ../k8s-svc-units:/app
environment:
NODE_ENV: development
FROM node:8-alpine
ENV NODE_ENV=production
RUN mkdir /app
WORKDIR /app
COPY package.json .
RUN npm install --production
COPY . .
CMD ["npm", "start"]
module.exports = {
name: "@hi",
actions: {
hello: {
handler(ctx) {
return "hello unit test";
}
}
}
};
"use strict";
const {
ServiceBroker
} = require("moleculer");
const {
ValidationError
} = require("moleculer").Errors;
describe("Test", function() {
it("should do nothing...", async (done) => {
expect.assertions(0);
console.time('start');
try {
console.log('start the test');
var hello = await global.broker.call("@hi.hello");
console.log('Hello -- ', hello);
} catch (e) {
console.error(e);
done(e);
}
console.log('end the test');
console.timeEnd('start');
done();
}, 15000);
});
{
"name": "@davidroman/units",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "export TZ=UTC && nodemon --exec npm run dev:runner",
"dev:runner": "export TZ=UTC && moleculer-runner --repl --hot --config moleculer.dev.config.js services",
"test": "jest --runInBand --coverage --watchAll"
},
"jest": {
"testEnvironment": "node",
"setupFilesAfterEnv": [
"<rootDir>/setupTests.js"
]
},
"dependencies": {
"dotenv": "^6.2.0",
"moleculer": "^0.13.5",
"moleculer-mail": "^1.2.1",
"nats": "^1.2.2"
},
"devDependencies": {
"jest": "^24.7.1",
"jest-cli": "^24.7.1",
"moleculer-repl": "^0.5.5",
"nodemon": "^1.18.11"
},
"nodemonConfig": {
"ignore": [
"docs/*"
],
"delay": "250"
}
}
const {
ServiceBroker
} = require("moleculer");
require('dotenv').config()
const os = require("os");
// jest.setTimeout(10000); // 1 second
jest.setTimeout(30000);
jest.useRealTimers();
/*
* We need to start this container inside of the micro-services environment
* Everything is reloaded and started with nodedemon
*/
global.beforeAll(async () => {
global.broker = new ServiceBroker({
namespace: "moleculer",
transporter: process.env.TRANSPORTER,
serializer: "JSON",
requestTimeout: 10 * 1000,
registry: {
strategy: "RoundRobin",
preferLocal: true
}
});
global.broker.emit = jest.fn();
await global.broker.waitForServices();
await global.broker.start();
}, 20000);
global.afterAll(async () => {
try {
await global.broker.stop();
} catch(e) {
console.log('/!\ Failed to stop');
console.error(e);
}
}, 20000);
@icebob
Copy link

icebob commented Apr 17, 2019

Thanks, very useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment