Mustapha
Mustapha

Reputation: 87

Keep nodeJs server running while executing the rest of tests

im new to nodeJS and webdriveIO writing some webdriveIO tests i have two functions: smsServer(); and startTest();

1- smsServer();

function smsServer(){
// parse application/x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json 
app.use(bodyParser.json())
app.get('/',function(req,res){
    console.log(req.query.text);
});
app.post('/', function(req,res){
    console.log("Post");
    console.log(req.body);
    res.sendStatus(200);
});
app.listen(3000, function(req, res){
console.log('App listening on localhost:3000');
console.log("req: ",req);
console.log("res: ",res);
});
}

2- startTest(); just some Test WDIO

i want to keep my server running to recieve some verification codes while the second function (tests) executing any help will be appreciated before my boss kills me

Upvotes: 2

Views: 66

Answers (2)

Mustapha
Mustapha

Reputation: 87

i found a solution that worked for me for this by using event-emitter so my server code will be:

'use strict';

const express           = require('express');
const bodyParser        = require('body-parser');

const app               = express();

// parse application/x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json 
app.use(bodyParser.json())

app.get('/',function(req,res){
    console.log('received sms %s', req.query.text);
    process.emit('sms', req.query.text);
    res.send(200);
});

app.post('/', function(req,res){
    console.log('received sms %s', req.query.text);
    process.emit('sms', req.query.text);
    res.send(200);
});

module.exports = app;

so the rest of the app would be

// decribe('some description', function(){
// some test code ...
return when.promise((resolve, reject)=>{
            process.on('sms', (sms)=>{
                // some work here

                resolve();
            });

            // some other code

        });
});

Upvotes: 1

magikbyte
magikbyte

Reputation: 46

lunch your app without kill a process or use forever. But, I think when you lunch your app the server must still running,

npm install forever 

and after

forever start yourapp 

Upvotes: 1

Related Questions