mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
add test case
This commit is contained in:
parent
3427aad0b8
commit
3d0cc4abd6
@ -2,7 +2,6 @@ import test from 'ava';
|
||||
const mock = require('mock-require');
|
||||
const mockie = require('../lib/mockie');
|
||||
const utils = require('../lib/utils');
|
||||
const thinkCluster = require('think-cluster');
|
||||
const path = require('path');
|
||||
|
||||
function getApplication() {
|
||||
@ -18,7 +17,6 @@ test.afterEach.always(() => {
|
||||
mockie.stop();
|
||||
});
|
||||
|
||||
|
||||
test.serial('normal test', t => {
|
||||
mockie.mockThinkMockHttp();
|
||||
const App = getApplication();
|
||||
@ -29,12 +27,13 @@ test.serial('normal test', t => {
|
||||
|
||||
test.serial('runInMaster', t => {
|
||||
mockie.mockCluster(true);
|
||||
mockie.mockThinkCluster();
|
||||
mockie.mockThinkMockHttp();
|
||||
const App = getApplication();
|
||||
let app = new App(defaultOption);
|
||||
app.parseArgv = ()=>{return {}}
|
||||
app.run();
|
||||
t.is(app.masterInstance instanceof thinkCluster.Master,true)
|
||||
t.is(app.masterInstance instanceof require('think-cluster').Master,true)
|
||||
})
|
||||
|
||||
test.serial('runInAgent', t => {
|
||||
@ -71,6 +70,17 @@ test.serial('runInWorker with createServerFn ', async t => {
|
||||
t.is(require('think-cluster').capturedEvents,true)
|
||||
})
|
||||
|
||||
test.serial('runInWorker with non-first worker', async t => {
|
||||
mockie.mockCluster(false);
|
||||
mockie.mockThinkCluster({isFirstWorker:()=>{return false}});
|
||||
const App = getApplication();
|
||||
let app = new App(defaultOption);
|
||||
app.parseArgv = ()=>{return {port:8361}};
|
||||
app.run();
|
||||
await utils.sleep(1000)
|
||||
t.is(require('think-cluster').capturedEvents,true)
|
||||
})
|
||||
|
||||
test.serial('watcher', t => {
|
||||
mockie.mockThinkMockHttp();
|
||||
const App = getApplication();
|
||||
@ -88,4 +98,117 @@ test.serial('watcher', t => {
|
||||
let app = new App(option);
|
||||
app.run();
|
||||
t.is(think.isCli, true)
|
||||
})
|
||||
})
|
||||
|
||||
test.serial('_watcherCallBack', t => {
|
||||
const App = getApplication();
|
||||
const babel = (options)=> {};
|
||||
const option = Object.assign({}, defaultOption, {
|
||||
transpiler: [babel,{
|
||||
presets: ['think-node']
|
||||
}],
|
||||
});
|
||||
let app = new App(option);
|
||||
app._watcherCallBack({path:'/',file:'tests.js'});
|
||||
})
|
||||
|
||||
test.serial('_watcherCallBack with non-logger', t => {
|
||||
const App = getApplication();
|
||||
const babel = (options)=> {};
|
||||
think.logger = null;
|
||||
const option = Object.assign({}, defaultOption, {
|
||||
transpiler: [babel,{
|
||||
presets: ['think-node']
|
||||
}],
|
||||
});
|
||||
let app = new App(option);
|
||||
app._watcherCallBack({path:'/',file:'tests.js'});
|
||||
})
|
||||
|
||||
test.serial('_watcherCallBack with non-array transpiler and throw error', t => {
|
||||
const App = getApplication();
|
||||
const babel = (options)=> {
|
||||
return new Error('test error')
|
||||
};
|
||||
const option = Object.assign({}, defaultOption, {
|
||||
transpiler: babel
|
||||
});
|
||||
let app = new App(option);
|
||||
let result = app._watcherCallBack({path: '/', file: 'tests.js'});
|
||||
|
||||
t.is(result, false);
|
||||
})
|
||||
|
||||
test.serial('notifier', t => {
|
||||
const App = getApplication();
|
||||
let result = null;
|
||||
let construct = (data)=>{
|
||||
result = data;
|
||||
}
|
||||
const option = Object.assign({}, defaultOption, {
|
||||
notifier:[
|
||||
construct,
|
||||
{
|
||||
sound:true
|
||||
}
|
||||
]
|
||||
});
|
||||
let app = new App(option);
|
||||
const err = new Error('test error');
|
||||
app.notifier(err);
|
||||
t.is(result.message,'test error');
|
||||
})
|
||||
|
||||
test.serial('notifier with non-array options', t => {
|
||||
const App = getApplication();
|
||||
let result = null;
|
||||
let construct = (data)=>{
|
||||
result = data;
|
||||
}
|
||||
const option = Object.assign({}, defaultOption, {
|
||||
notifier:construct
|
||||
});
|
||||
let app = new App(option);
|
||||
const err = new Error('test error');
|
||||
app.notifier(err);
|
||||
t.is(result.message,'test error');
|
||||
})
|
||||
|
||||
test.serial('_watcherCallBack with masterInstance', t => {
|
||||
mockie.mockCluster(true);
|
||||
mockie.mockThinkCluster();
|
||||
mockie.mockThinkMockHttp();
|
||||
const App = getApplication();
|
||||
const babel = (options)=> {};
|
||||
const option = Object.assign({}, defaultOption, {
|
||||
transpiler: babel
|
||||
});
|
||||
let app = new App(option);
|
||||
app.parseArgv = ()=>{return {}};
|
||||
app.run();
|
||||
app._watcherCallBack({path:'/',file:'tests.js'});
|
||||
t.is(app.masterInstance instanceof require('think-cluster').Master,true);
|
||||
})
|
||||
|
||||
test.serial('run with pm2 cluster mode', t => {
|
||||
mockie.mockThinkPm2({isClusterMode:true});
|
||||
const App = getApplication();
|
||||
const app = new App(defaultOption);
|
||||
let err = null;
|
||||
try{
|
||||
app.run();
|
||||
}catch(e){
|
||||
err = e;
|
||||
}
|
||||
t.is(err instanceof Error,true);
|
||||
})
|
||||
|
||||
test.serial('run with exception', t => {
|
||||
mockie.mockCluster(false);
|
||||
mockie.mockThinkCluster({agent:true,isAgent:undefined});
|
||||
mockie.mockThinkMockHttp();
|
||||
const App = getApplication();
|
||||
let app = new App(defaultOption);
|
||||
app.parseArgv = ()=>{return {}};
|
||||
app.run();
|
||||
})
|
||||
|
||||
@ -59,13 +59,18 @@ function mockCluster(isMaster) {
|
||||
})
|
||||
}
|
||||
|
||||
function mockThinkCluster(args) {
|
||||
function mockThinkCluster(args = {}) {
|
||||
let {agent} = args;
|
||||
let obj = Object.assign({
|
||||
isAgent(){
|
||||
return agent
|
||||
},
|
||||
Master: class Master {
|
||||
forkWorkers(){
|
||||
return Promise.resolve();
|
||||
}
|
||||
forceReloadWorkers(){
|
||||
}
|
||||
},
|
||||
Worker: class Worker {
|
||||
constructor(options = {}){
|
||||
@ -86,6 +91,10 @@ function mockThinkCluster(args) {
|
||||
mock('think-cluster', obj);
|
||||
}
|
||||
|
||||
function mockThinkPm2(args = {}) {
|
||||
mock('think-pm2', args);
|
||||
}
|
||||
|
||||
function stop(name) {
|
||||
if (!name) {
|
||||
mock.stopAll()
|
||||
@ -98,5 +107,6 @@ module.exports = {
|
||||
mockThinkMockHttp,
|
||||
mockCluster,
|
||||
mockThinkCluster,
|
||||
mockThinkPm2,
|
||||
stop
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user