franzip / generator-hapi-api-stack

An Hapi API generator with ORM, services, job queue, authentication, caching and deployment out of the box
MIT License
50 stars 12 forks source link

how to write test for chairo plugin services register on server object. #5

Closed saurabhghewari closed 7 years ago

saurabhghewari commented 7 years ago

HI Franzip, I am nodejs developer and using this boilerplate for one of my project. I am happy to use this for all the future projects.

But right now, i am not getting how to write test case for the following code.

server.seneca.add({role : 'someRole', cmd : 'someCommand'}, (message, next) => { // bussiness logic which i need to test });

seneca.test.js

    'use strict';

    const Lab = require('lab');
    const Code = require('code');
    const Hapi = require('hapi');
    const Config = require('../config');
    const Dogwater = require('dogwater');

    const lab = exports.lab = Lab.script();
    const describe = lab.describe;
    const it = lab.it;
    const before = lab.before;
    const beforeEach = lab.beforeEach;
    const afterEach = lab.afterEach;
    const after = lab.after;
    const expect = Code.expect;

    describe('seneca', () => {

        let server;

        before((done) => {

            server = new Hapi.Server();
            server.connection();

            server.settings.app = Config;

            server.register([{
                register: Dogwater,
                options : {
                    connections: {
                      tmc_backend: {
                        adapter: 'sails-mongo',
                        host: 'mongo_test',
                        port: 27017,
                        database: 'billwise'
                      }
                    },
                    adapters: {
                      'sails-mongo': require('sails-mongo')
                    },
                    models: require('path').resolve('../src', 'models')
                }
            },
            { register: require('chairo'),
                options: {
                  timeout: 999999999
                }
            },
            { register : require('../lib') }
            ], (err) => {

                if (err) {
                    return done(err);
                }

                server.initialize(done);
            });
        });

        after((done) => {
            server = null;
            done();
        });

        it('should execute the defined action', (done) => {
            server.seneca.act({ role : 'add', cmd : 'addingNumber'}, (err, result) => {
                expect(err).to.not.exist();
                done();
            });
        });
    });

the above test stops at "expect(err).to.not.exist()" even though err occurs or result is received.

So, i am confused whether i am going in wrong direction? Any help would appreciate.

Please Note:- I am not using Labbable server.

franzip commented 7 years ago

Hi @saurabhghewari

Unfortunately I could not reproduce your issue.

If you are willing to stick to the opinionated conventions that this project follows, testing seneca with Labbable is as easy as the adding the following to your tests;

it('has working services', (done) => {
  server.seneca.act({ generate: 'id' }, (err, result) => {
    expect(err).to.not.exist();
    expect(result.id).to.equal(1);
    done();
  });
});
saurabhghewari commented 6 years ago

Sorry for late response. Thank you for the answer. I will give it a try.

saurabhghewari commented 6 years ago

Are you planing to migrate this project to Hapi 17 from Hapi 16?

Here you can find the documentation for latest hapi api. Hapi17