hemerajs / hemera-testsuite

Helper library to write tests for Hemera.
MIT License
2 stars 4 forks source link

ECONNREFUSED 127.0.0.1:4222 #11

Closed PatrickHeneise closed 6 years ago

PatrickHeneise commented 6 years ago

I'm exporting hemera from an abstraction:

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
})

To test this abstraction, I thought of replacing nats with hemera-testsuite/nats:

const test = require('tape')
const proxyquire = require('proxyquire')
const Nats = require('hemera-testsuite/nats')
const hemera = proxyquire('../hemera', {
  nats: new Nats()
})

test('hemera', function (assert) {
  const expected = 40

  hemera.ready(async (err) => {
    if (err) {
      console.error(err)
      assert.fail(err)
    }

    hemera.add({
      topic: 'math',
      cmd: 'add'
    }, async function (req) {
      return await 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()
  })
})

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?

StarpTech commented 6 years ago

Hi @PatrickHeneise for what do you need proxyquire ? Did you started the NATS server ?

PatrickHeneise commented 6 years ago

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.

StarpTech commented 6 years ago

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()
  })
})
PatrickHeneise commented 6 years ago

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.

StarpTech commented 6 years ago

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.

PatrickHeneise commented 6 years ago

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.

StarpTech commented 6 years ago

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
PatrickHeneise commented 6 years ago

"It doesn't touch your application" is the issue. I do want to test my application.

StarpTech commented 6 years ago

You can do it with both solutions.

PatrickHeneise commented 6 years ago

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

StarpTech commented 6 years ago

Look in the tests there you can see how do you pass nats to hemera.

PatrickHeneise commented 6 years ago

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.