Created
April 16, 2019 19:35
-
-
Save davidroman0O/ab79712972b1a32380244649ff14dd3c to your computer and use it in GitHub Desktop.
Moleculer Jest Setup for docker-compose unit testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM node:8-alpine | |
ENV NODE_ENV=production | |
RUN mkdir /app | |
WORKDIR /app | |
COPY package.json . | |
RUN npm install --production | |
COPY . . | |
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
name: "@hi", | |
actions: { | |
hello: { | |
handler(ctx) { | |
return "hello unit test"; | |
} | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, very useful!