mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
test(before) add connect
This commit is contained in:
parent
c7f14d37e9
commit
7745fb0e9b
3 changed files with 22 additions and 16 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue