test(before) add connect

This commit is contained in:
coderaiser 2018-04-17 11:21:46 +03:00
parent c7f14d37e9
commit 7745fb0e9b
3 changed files with 22 additions and 16 deletions

View file

@ -7,6 +7,7 @@ const express = require('express');
const io = require('socket.io');
const writejson = require('writejson');
const readjson = require('readjson');
const {promisify} = require('es6-promisify');
process.env.NODE_ENV = 'development';
@ -16,7 +17,9 @@ const {assign} = Object;
const pathConfig = os.homedir() + '/.cloudcmd.json';
const currentConfig = readjson.sync.try(pathConfig);
module.exports = (options, fn = options) => {
module.exports = before;
function before(options, fn = options) {
if (fn === options) {
options = {};
}
@ -44,7 +47,13 @@ module.exports = (options, fn = options) => {
server.listen(() => {
fn(server.address().port, after);
});
};
}
module.exports.connect = promisify((options, fn = options) => {
before(options, (port, done) => {
fn(null, {port, done});
});
});
function defaultConfig() {
return {

View file

@ -12,7 +12,6 @@ const rootDir = '../..';
const routePath = `${rootDir}/server/route`;
const beforePath = '../before';
const configPath = `${rootDir}/server/config`;
const cloudcmdPath = `${rootDir}/server/cloudcmd`;
const {
_getIndexPath,
@ -20,6 +19,7 @@ const {
const route = require(routePath);
const before = require(beforePath);
const {connect} = before;
const clean = require('clear-module');
@ -305,7 +305,7 @@ test('cloudcmd: route: realpath: error', (t) => {
});
});
test('cloudcmd: route: sendIndex: error', (t) => {
test('cloudcmd: route: sendIndex: error', async (t) => {
const error = Error('index path error');
const {readFile} = fs;
const isDev = true;
@ -314,22 +314,19 @@ test('cloudcmd: route: sendIndex: error', (t) => {
fs.readFile = (name, options, fn = options) => {
if (name === indexPath) {
fn(error);
fs.readFile = readFile;;
fs.readFile = readFile;
return;
};
}
return readFile(name, options, fn);
};
const config = {};
const {port, done} = await connect();
const data = await getStr(`http://localhost:${port}`);
before({config}, (port, after) => {
getStr(`http://localhost:${port}`)
.then((data) => {
t.equal(data, error.message, 'should return error');
t.end();
after();
})
});
t.equal(data, error.message, 'should return error');
t.end();
done();
});