GithubHelp home page GithubHelp logo

hemera-testsuite's People

Contributors

keks0r avatar starptech avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

hemera-testsuite's Issues

How to stub functions inside actions?

Hi,

I added bcrypt module in existing repo:

https://github.com/vforv/hemera-error

And error will occure again inside the second action... Problem is because the second action receive hash from bcrypt function...

So how can I stub bcrypt module and if is it possible to read values of properties from actions?
Also is possible to action receive input dynamically from bcrypt?

ECONNREFUSED 127.0.0.1:4222

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?

hemera test fails for async/await

Versions
hemera-testsuite: 1.2.0
Node: 8.4.0
npm: 5.2 v
nats-hemera: 1.5.13

Sample example

'use strict';

const Hp = require('hemera-plugin');

exports.plugin = Hp(function hemeraTestPlugin(options, next) {
    const hemera = this;
    const topic = 'testPlugin';
    this.log.info('received options (%j)', options);
    hemera.add({
        topic,
        cmd: 'add',
    }, async function (req) {
        const result = await Promise.resolve({
            result: req.a + req.b,
        });
        return result;
    });

    next();
});

exports.options = {};

exports.attributes = {
    pkg: require('../../package.json'),
};

test case

'use strict';

const Hemera = require('nats-hemera');
const Plugin1 = require('./example-async');
const HemeraTestsuite = require('hemera-testsuite');


// prevent warning message of too much listeners
process.setMaxListeners(0);

describe('hemera-testPlugin', function () {
    const PORT = 4222;
    const topic = 'testPlugin';
    let server;

    // Start up our own nats-server
    beforeAll(function (done) {
        server = HemeraTestsuite.start_server(PORT, done);
    });

    // Shutdown our server after we are done
    afterAll(function (done) {
        server.kill();
        done();
    });

    it('Should be able to add two numbers', function (done) {
        const nats = require('nats').connect();
        const options = { a: 1 };
        const hemera = new Hemera(nats, {
            logLevel: 'info',
        });
        hemera.use(Plugin1, options);

        hemera.ready(() => {
            hemera.ext('onServerPreRequest', async function (ctx, req, res) {
                await Promise.resolve();
            });

            hemera.act({
                topic,
                cmd: 'add',
                a: 1,
                b: 20,
            }, async function (err, resp) {
                try {
                    await resp;
                    expect(err).toBeNull();
                    expect(resp.result).toEqual(21);
                    hemera.close(done);
                } catch (err) {
                    hemera.close(done.fail);
                }

            });
        });

    });
});

Testing with real NATS Server connection

Is it possible to connect to nats server even if it is not installed in localhost?
If not that would be nice to have...

Ex.

var PORT = 6242
var authUrl = 'nats://REMOTE_IP:' + PORT

const nats = require('nats').connect(authUrl)

If we don't have this it will use more space if we have for each service nats server...

Hapi hemera testsuite Timeout error

When I try use hemera with server.inject it doesn't work I am getting Timeout error, it looks like add is never called by act....

here is example:

L.test('Create general info', (done) => {
        const nats = new Nats()
        const hemera = new Hemera(nats, {
            logLevel: 'info'
        });

        hemera.ready(() => {
            hemera.add({
                topic: 'kycc',
                individual: false,
                create: 'general-info'
            }, (msg: any, done: any) => {
                console.log("TEST")
                done(null, "mock")
            })
        });


        const options = {
            method: "POST",
            url: "/v1/route",
            payload: {
            }
        };
        server.inject(options, (response: any) => {
            expect(JSON.parse(response.payload).statusCode).to.equal(200);
            done();
        });
    });

Act call will be called inside /v1/route route... What can be problem?

2.0.3 version not working for me

here is my test:

import * as lab from 'lab';
const L = exports.lab = lab.script();

const Hemera = require('nats-hemera')
const Nats = require('hemera-testsuite/nats')

const nats = new Nats()
const hemera = new Hemera(nats)

L.experiment('Basic test', () => {

L.test('Math', (done) => {

    hemera.ready(function () {
        hemera.add(
            {
                topic: 'math',
                cmd: 'add'
            },
            (req: any) => req.a + req.b
        )
        hemera.act(`topic:math,cmd:add,a:1,b:2`, (err: any, resp: any) => {
            console.log(err, resp)
            done();
        })
    })
});

});

I am getting Timeout error

Fatal error after test run

I was trying to run basic example but it not working for me.

I get this error:
https://files.gitter.im/vforv/DrUf/hemeratest.png

Here is the code:

import * as lab from 'lab';
import * as code from 'code';
import * as sinon from 'sinon';
import { PingLogic } from '../logic/ping.logic';
import * as Moment from 'moment';
import * as Hemera from 'nats-hemera';
const HemeraTestsuite = require('hemera-testsuite');

const ping = new PingLogic

const L = exports.lab = lab.script();
const expect = code.expect;

const PORT = 4222
const noAuthUrl = 'nats://localhost:' + PORT
let server: any;



L.experiment('Ping action', () => {
    L.before((done) => {
        server = HemeraTestsuite.start_server(PORT, done);
    });


    // Shutdown our server after we are done
    L.after(function () {
        server.kill();
    })

    L.test('Ping acction test timestamp', (done) => {

        const nats = require('nats').connect(noAuthUrl)

        const hemera = new Hemera(nats, { logLevel: 'info' })

        hemera.ready(() => {

            hemera.add({
                topic: 'math',
                cmd: 'add'
            }, function (resp, cb) {
                cb(null, resp.a + resp.b)
            })


            hemera.act({
                topic: 'math',
                cmd: 'add',
                a: 1,
                b: 2
            }, (err, resp) => {
                expect(err).not.to.be.exists()
                expect(resp.result).to.be.equals(3)

                hemera.close()
                done()
            })

        });

    });
});

How to mock actions inside other actions?

I have this mongo actions inside user-save action:

//Here are 2 actions one inside another
hemera.act({
                    topic: 'mongo-store',
                    cmd: 'find',
                    collection: 'users',
                    query: { email: userPeyload.email }
                }, (err: Error, resp: any) => {

                    if (err) {
                        done(err);
                    }
                    
                    
                    if (resp.result.length === 0) {
                        hemera.act({
                            topic: 'mongo-store',
                            cmd: 'create',
                            collection: 'users',
                            data: user
                        }, (err: Error, resp: any) => {
                            if (err) {
                                done(null, err);
                            }

                            //TODO: call mail service here
                            done(null, 'We sent you activation link to the mail.');
                        })

                    } else {
                        const UnauthorizedError = hemera.createError("Unauthorized");
                        const mess = new UnauthorizedError("User already exists.");
                        done(mess);
                    }
                })

Stub for first works but for second inside not...

actStub.stub({
                topic: 'mongo-store',
                cmd: 'find',
                collection: 'users',
                query: { email: "[email protected]" }
            }, null, {result: []});


actStub.stub({
                topic: 'mongo-store',
                cmd: 'create',
                collection: 'users',
                data: userData
            }, null, 50);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.