javascript - Ensuring server app runs before mocha tests start -
this similar ensuring express app running before each mocha test , specified solution still isnt working + i'm using websocket server
in short , i'm using websocket framework called socketcluster , server file
import {socketcluster} 'socketcluster'; const socketcluster = new socketcluster({ workers:1, brokers:1, port: 3000, appname:null, initcontroller: __dirname + '/init.js', workercontroller: __dirname + '/worker.js', brokercontroller: __dirname + '/broker.js', socketchannellimit: 1000, crashworkeronerror: true }) export default socketcluster
running node server.js
starts worker process specified in worker.js
export const run = (worker) => { console.log(' >> worker pid: ',process.pid); const app = express(); const httpserver = worker.httpserver; const scserver = worker.scserver; app.use(cookieparser()) httpserver.on('request', app); app.get('/',(req,res) => { console.log('recieved') res.send('hello world') }) }
i want test server , tests finishing (and failing) way before server starts. there way can force server load before going ahead tests? have far
describe('express server',() =>{ beforeeach((done) => { require('../../server/server') done() }) it('should return "hello world"',(done) => { http.get('http://127.0.0.1:3000',(res) => { expect(res).to.contain('wtf world') done() }) }) })
the above doesnt seem work. server doesnt load in before block despite providing done() call well.
edit - i've tried splitting server.js file invoke different server based on how imported.
const main = () => { console.log('main server') new socketcluster({ workers:1, brokers:1, port: 3000, appname:null, initcontroller: __dirname + '/init.js', workercontroller: __dirname + '/worker.js', brokercontroller: __dirname + '/broker.js', socketchannellimit: 1000, crashworkeronerror: true }) } export const test = (port,done) => { console.log('testing server') new socketcluster({ workers:1, brokers:1, port: port, appname:null, initcontroller: __dirname + '/init.js', workercontroller: __dirname + '/worker.js', brokercontroller: __dirname + '/broker.js', socketchannellimit: 1000, crashworkeronerror: true }) done() } if (require.main === module){ main() }
and in test.js , - still doesnt seem work though
import {expect} 'chai'; import {test} '../../server/server' describe('express server',() =>{ before(function(done){ test(3000,done) }) it('should return "hello world"',(done) => { http.get('http://127.0.0.1:3000',(res) => { expect(res).to.contain('world') done() }) }) })
edit:2 - trie way returning promise server.js file. still doesnt work
export const test = (port) => { console.log('testing server') return promise.resolve(new socketcluster({ workers:1, brokers:1, port: port, appname:null, initcontroller: __dirname + '/init.js', workercontroller: __dirname + '/worker.js', brokercontroller: __dirname + '/broker.js', socketchannellimit: 1000, crashworkeronerror: true })) }
and in before hook
before(function(done,port){ test(3000).then(function(){ console.log('arguments: ',arguments) done() }) })
your server
module doesn't have callback, not ready when call done()
in beforeeach
method.
first, export app
in server
module.
then, like:
const app = require('../../server/server').app; let server; before(done => { server = app.listen(3000, done); }); /** ... tests here ... **/ /** and, if need close server after test **/ after(done => { server.close(done); });
this way, done()
called in listen
callback, in tests server listening correctly. then, remember close after tests end (useful if server required in 1 or more test suites).
Comments
Post a Comment