senecajs / seneca-web

Http route mapping for Seneca microservices.
MIT License
76 stars 44 forks source link

Supertest with seneca-web does not work #124

Closed kasongoyo closed 7 years ago

kasongoyo commented 7 years ago

`

//dependencies
const Seneca = require('seneca');
const path = require('path');
const Web = require('seneca-web');
const Express = require('express');
const bodyParser = require('body-parser');
const config = require('config');

let app = Express();

//parsing body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

// Define all necessary routes to REST API
const routes = [
    {
        pin: 'route:user,action:*',
        map: {
            'create': {
                POST: true,
                alias: '/api/users'
            },
            'get': {
                GET: true,
                alias: '/api/users'
            },
            'update': {
                PUT: true,
                alias: '/api/users/:id'
            }
        }
    }
];

const senecaWebConfig = {
    adapter: require('seneca-web-adapter-express'),
    options: { parseBody: false },
    context: app
}

const seneca = Seneca();
//Enable to pass error as first argument without fatal error
seneca.fixedargs.fatal$ = false;

seneca
    .use('seneca-mongoose', config.get('mongoose'))
    .use('user-plugin')
    .use('./api/user_api')
    .use('mesh', {
        isbase: true,
        pin: "role:api"
    })
    .use(Web, senecaWebConfig);

seneca.act('role:web', { routes: routes }, (err, reply) => {
    //https://github.com/senecajs/seneca-web/issues/56
    //https://github.com/senecajs/seneca-web-adapter-express/blob/master/test/express.test.js
    app.use((err, req, res, next) => {
        if (res.headersSend) { return next(err) }
        res.status(500).send({ message: err.message });
    });
});

module.exports = app;

`

Attached is my application.js that hold the app code. I have exported express instance so that I can be able to plug in the instance to supertest the way I use to run test when I don't using seneca-web, but no routes seems to be found when I run actually test? What difference does the seneca web add that prevent express instance app to have routes.

kasongoyo commented 7 years ago

I have figured it out. The test got executed before seneca initialization process complete so I had to increase test timeout and problem has disappeared.

dgonzalez commented 7 years ago

You might be interested in the ready callback: http://senecajs.org/api/#method-ready Not sure if it solves your problem but it is the way to execute actions when seneca is ready.

Thanks

On 22 March 2017 at 11:04, kaso notifications@github.com wrote:

Closed #124 https://github.com/senecajs/seneca-web/issues/124.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/senecajs/seneca-web/issues/124#event-1010236755, or mute the thread https://github.com/notifications/unsubscribe-auth/AAHkOhE-_M2xv9VWWpupi_v1trX5kywXks5roQA2gaJpZM4Mk_wq .

kasongoyo commented 7 years ago

Thanks @dgonzalez.