Comments (12)
@prcesar it's the same scenario as with a http api. You should do unit tests to test your business code. You should do integration tests to test your stack. How to do integration tests you can see in the tests directory or in the documentation. Unit tests are independent from the used framework. If you have services which calls other services you have to mock that interface in your unit tests. I hope it is clear otherwise refine your question. Thanks!
from hemera.
@acehko @pocesar see the example we provide a small but neat feature to stub the act and add interfaces without to start NATS.
https://github.com/hemerajs/hemera/blob/master/examples/unittest.js
from hemera.
Hi @pocesar we use NATS as transport layer so we cant emulate it for tests but you can look in the test directory there you can find a very simple way to testing your services. There is a package called hemera-testsuite
with that you can start and stop the server. Thanks.
from hemera.
If you have any ideas to create test utilities do not hesitate to make an approach. Please reopen when needed.
from hemera.
Hi @StarpTech
Since hemera is used only with NATS, and is not transport independent, why not make the nats
package a dependency of nats-hemera
? Or make it optional. That way it would be a lot easier to test without the need to run nats. Then we could use something like hemera.start()
to connect to nats.
from hemera.
@StarpTech yes, it makes sense to test only the methods, like Seneca does
it wasn't obvious because the given examples use closures and doesn't separate each add
from the implementation
from hemera.
@acehko it was a design decision that hemera isn't transport independent Overview. I won't emulate the NATS messaging logic. NATS function as router, transport layer. You don't have to start NATS if you write your code with testing in mind. Start with testing your business logic instead to cover it with an integration test.
from hemera.
@StarpTech I understand.
But since hemera depends on nats, why is nats not a dependecy of hemera? Right now we need to pass a nats objec to hemera. For that reason we always need to use hemera-testsuite
for unit testing.
What would be nice is to create a new hemera instance without connecting to nats. That way we could unit test without extra dependencies, and only connect to nats when we run the service in production.
Something like this:
const heemra = new Hemera();
// Connect only in production
hemera.connect(natsURL);
from hemera.
nats-hemera
means that hemera is tool for nats. Hemera should be small as possible and I seperate the hemera abstraction from the transport. You can also plug your own NATS Server when it match with the interface. I think you misunderstood it. Unit test aren't integration tests. You dont need NATS to do it.
Since we rely on NATS you have to start NATS to simulate a real scenario. For unit testing you can simple test your library.
A unit test is a test written by the programmer to verify that a relatively small piece of code is doing what it is intended to do.
An integration test is done to demonstrate that different pieces of the system work together. Integration tests cover whole applications, and they require much more effort to put together. They usually require resources like database instances and hardware to be allocated for them.
from hemera.
@StarpTech What you are saying makes sense. I was viewing hemera to be more like seneca than it actually is. As for testing, what I meant was that I want to test methods that were registered with .add
without the need to run a nats server. I was wrong to call it unit testing.
from hemera.
@acehko great. btw you also has access to your server method without to start NATS.
const payload = hemera.router.lookup({ topic: 'math', cmd: 'add' }) //get by pattern
//call add method manually
const request = { a: 1, b: 2 }
payload.action(request, function(err,result) {
//result = 3
//be aware this function has no scope
})
But there is no way to just initialize node-nats
without starting it. But I think it is easy to mock because you dont need NATS for unit testing.
class NatsMock extends EventEmitter {}
const hemera = new Hemera(new NatsMock)
const payload = hemera.router.lookup({ topic: 'math', cmd: 'add' }) //get by pattern
//call add method manually
const request = { a: 1, b: 2 }
payload.action(request, function(err,result) {
//result = 3
//be aware this function has no scope
})
from hemera.
@acehko https://github.com/hemerajs/hemera/blob/master/examples/unittest.js
from hemera.
Related Issues (20)
- Logging id HOT 15
- Direct communication ? HOT 3
- stats plugin not returning all actions HOT 7
- Does hemera.close() killall running jobs? HOT 2
- TypeError when defining custom Pino logger HOT 1
- Why https://hemerajs.github.io/hemera/docs/reference.html contains not documentation for add() function. HOT 3
- Adding hemera to Docusaurus users page HOT 3
- Unsubscribe by RegExp HOT 9
- Unable to subscribe to NATS topic when the publisher is not running on Hemera HOT 1
- Client Timeout HOT 3
- hemera-ajv doesnt seem to be working correctly HOT 4
- Hemera-blipp has old hemera version HOT 3
- Have you looked into ts-nats? HOT 3
- How to reply from middleware or extension? HOT 5
- Looking for maintainers and collaborators HOT 5
- NATS disconnection question HOT 1
- Calling reply.send inside async middleware doesn't stop the chain HOT 2
- Typescript support HOT 1
- hemera-zipkin package missing HOT 3
- Any way to unregister an extension? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hemera.