Closed PatrickHeneise closed 6 years ago
Hi @PatrickHeneise
for what do you need proxyquire
? Did you started the NATS
server ?
No, from what I understand this is an in-memory solution that works without nats-server? I actually just want to check if everything is exported properly, not the integration with NATS.
That's correct. Try this:
const test = require('tape')
const Hemera= require('nats-hemera')
const Nats = require('hemera-testsuite/nats')
test('hemera', function (assert) {
const expected = 40
const hemera = new Hemera(new Nats())
hemera.ready(async (err) => {
if (err) {
console.error(err)
assert.fail(err)
}
hemera.add({
topic: 'math',
cmd: 'add'
}, async function (req) {
return req.a + req.b
})
const actual = await hemera.act({ topic: 'math', cmd: 'add', a: 10, b: 30 })
assert.equal(actual, expected, 'hemera.add() and hemera.act() pass')
hemera.close()
assert.end()
})
})
That's what I got from the example, but I wouldn't test my own code with that, which is so far the code from the readme. There's no new Nats()
and hemera.connect()
in the default implementation. That's why I tried to proxyquire nats
in the file.
The code from the readme does not use hemera-testsuite
if you want to do in-memory tests you have to initiate nats as shown in the readme here.
That's the issue about. How would I test hemera, if the implementation isn't compatible with hemera-testsuite? Means it's not testable without having NATS running.
I don't know what's your problem is. The only difference is the way how NATS is initiated. It doesn't touch your application.
const Hemera = require('nats-hemera')
const Nats = require('hemera-testsuite/nats')
const nats = new Nats()
const hemera = new Hemera(nats, {
logLevel: 'info'
})
// test your application
vs
const Hemera = require('nats-hemera')
const Nats = require('nats')
const hemera = new Hemera(Nats.connect(), {
logLevel: 'info'
})
// test your application
"It doesn't touch your application" is the issue. I do want to test my application.
You can do it with both solutions.
That I am asking, as I can't get it to work. If I have the abstraction of the Hemera connection as in the first example and want some test coverage above 0% on that file, how would I get this to work, because it doesn't. hemera-testsuite
uses a different way to initialise nats (new Nats() instead of nats.connect()) and when proxying NATS, I get a connection refused.
Given the application code:
const logger = require('./log')
const Hemera = require('nats-hemera')
let nats = require('nats')
const DEFAULT_OPTS = {}
const CONFIG_OPTS = Object.assign(DEFAULT_OPTS, {
uri: process.env.NATS_URI
})
nats.connect(CONFIG_OPTS)
module.exports = new Hemera(nats, {
logger: logger
})
how do I test this as a unit test without NATS server and instead hemera-testsuite?
Thanks
Thanks, that's what I tried with:
const hemera = proxyquire('../hemera', {
nats: new Nats()
})
Not sure why it doesn't work, but I'll find another way to test.
I'm exporting
hemera
from an abstraction:To test this abstraction, I thought of replacing
nats
withhemera-testsuite/nats
:But I only get an error back:
NatsError: Could not connect to server: Error: connect ECONNREFUSED 127.0.0.1:4222
How does this work?