diff --git a/app/extend/helper.js b/app/extend/helper.js index 92d2af88de..dbbf709105 100644 --- a/app/extend/helper.js +++ b/app/extend/helper.js @@ -1,6 +1,7 @@ 'use strict'; -const path = require('path'); +const url = require('url'); + module.exports = { @@ -36,7 +37,7 @@ module.exports = { * @return {String} 含有域名的完整 URL */ urlFor(name, params) { - return this.ctx.protocol + '://' + this.ctx.host + path.join('/', this.app.router.url(name, params)); + return this.ctx.protocol + '://' + this.ctx.host + url.resolve('/', this.app.router.url(name, params)); }, }; diff --git a/examples/schedule/test/index.test.js b/examples/schedule/test/index.test.js index 1aa1121468..2bfc007642 100644 --- a/examples/schedule/test/index.test.js +++ b/examples/schedule/test/index.test.js @@ -24,7 +24,7 @@ describe.skip('egg schedule example', () => { countLine(log, 'worker&&cron').should.equal(1); countLine(log, 'worker&&interval').should.equal(1); - app.close(); + yield app.close(); }); }); diff --git a/lib/agent.js b/lib/agent.js index 2e9d7b80fb..808d115606 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -94,8 +94,11 @@ class Agent extends EggApplication { } close() { - process.removeListener('uncaughtException', this._uncaughtExceptionHandler); - super.close(); + return super + .close() + .then(() => { + process.removeListener('uncaughtException', this._uncaughtExceptionHandler); + }); } } diff --git a/lib/egg.js b/lib/egg.js index 06bbc76493..dfa29b3469 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -225,15 +225,21 @@ class EggApplication extends EggCore { this.ready(() => clearTimeout(startTimeoutTimer)); } - /** * 关闭 app 上的所有事件监听 * @public + * @return {Promise} promise */ close() { - super.close(); - this.messenger.close(); - process.removeListener('unhandledRejection', this._unhandledRejectionHandler); + return super + .close() + .then(() => { + for (const logger of this.loggers.values()) { + logger.end(); + } + this.messenger.close(); + process.removeListener('unhandledRejection', this._unhandledRejectionHandler); + }); } } diff --git a/package.json b/package.json index ec2bfad9bd..72fd3fc82f 100644 --- a/package.json +++ b/package.json @@ -18,18 +18,18 @@ "co": "^4.6.0", "debug": "^2.2.0", "delegates": "^1.0.0", - "egg-cluster": "^0.1.0", + "egg-cluster": "^1.0.0", "egg-cookies": "^1.0.0", - "egg-core": "^0.2.1", + "egg-core": "^0.5.0", "egg-cors": "^0.0.2", - "egg-development": "^1.0.1", + "egg-development": "^1.0.2", "egg-i18n": "^1.0.2", - "egg-logger": "^1.2.0", - "egg-logrotator": "^2.1.0", + "egg-logger": "^1.3.0", + "egg-logrotator": "^2.2.0", "egg-multipart": "^1.0.0", - "egg-onerror": "^0.0.3", - "egg-rest": "^1.0.1", - "egg-schedule": "^2.0.0", + "egg-onerror": "^1.0.0", + "egg-rest": "^1.1.0", + "egg-schedule": "^2.2.0", "egg-security": "^1.2.1", "egg-session": "^0.0.2", "egg-static": "^0.1.0", @@ -48,7 +48,7 @@ "scmp": "^1.0.0", "sdk-base": "^2.0.1", "sendmessage": "^1.0.5", - "urllib": "^2.14.0" + "urllib": "^2.17.0" }, "devDependencies": { "autod": "^2.7.1", @@ -56,34 +56,34 @@ "beautify-benchmark": "^0.2.4", "benchmark": "^2.1.0", "co-sleep": "^0.0.1", - "coffee": "^3.2.5", + "coffee": "^3.3.0", "cross-env": "^2.0.1", "egg-alinode": "^1.0.3", "egg-bin": "^1.3.0", "egg-ci": "^1.0.3", - "egg-mock": "^0.0.4", + "egg-mock": "^0.0.7", "egg-plugin-puml": "1", "egg-view-nunjucks": "^0.5.0", "eslint": "^3.0.0", "eslint-config-egg": "^3.1.0", "estraverse": "^4.1.1", "formstream": "^1.0.0", - "glob": "^7.0.6", + "glob": "^7.1.1", "koa": "^1.2.4", "koa-router": "^5.4.0", "merge-descriptors": "^1.0.1", - "moment": "^2.15.0", + "moment": "^2.15.2", "npminstall": "^2.1.1", "nunjucks": "^2.5.2", "once": "^1.3.3", "pedding": "^1.0.0", "rds": "^0.1.0", "rimraf": "^2.5.4", - "should": "^11.1.0", + "should": "^11.1.1", "stream-wormhole": "^1.0.0", - "supertest": "^2.0.0", - "toa": "^2.1.0", - "toa-router": "^1.5.1" + "supertest": "^2.0.1", + "toa": "^2.3.1", + "toa-router": "^1.5.2" }, "main": "index.js", "files": [ diff --git a/test/app/extend/agent.test.js b/test/app/extend/agent.test.js index e31c8b0742..f14e5883b5 100644 --- a/test/app/extend/agent.test.js +++ b/test/app/extend/agent.test.js @@ -28,27 +28,28 @@ describe('test/app/extend/agent.test.js', () => { }); describe('agent.instrument()', () => { + let app; + afterEach(() => app.close()); + it.skip('should not log in unittest env', function* () { mm.env('unittest'); - const app = utils.app('apps/agent-instrument'); + app = utils.app('apps/agent-instrument'); yield app.ready(); yield sleep(1000); // TODO: why egg-agent.log not exists? const log = fs.readFileSync( utils.getFilepath('apps/agent-instrument/logs/agent-instrument/egg-agent.log'), 'utf8'); log.should.not.match(/\[http\] \/hello/); - app.close(); }); it('should log in local env', function* () { mm.env('local'); - const app = utils.app('apps/agent-instrument', { cache: false }); + app = utils.app('apps/agent-instrument', { cache: false }); yield app.ready(); yield sleep(1000); const log = fs.readFileSync( utils.getFilepath('apps/agent-instrument/logs/agent-instrument/egg-agent.log'), 'utf8'); log.should.match(/\[http\] \/hello/); - app.close(); }); }); }); diff --git a/test/app/extend/application.test.js b/test/app/extend/application.test.js index a7fc0fa04f..6e09eadb3f 100644 --- a/test/app/extend/application.test.js +++ b/test/app/extend/application.test.js @@ -19,19 +19,21 @@ describe('test/app/extend/application.test.js', () => { }); describe('app.inspect()', () => { - it('should inspect app properties', done => { - const app = utils.app('apps/demo'); - app.ready(() => { - app.inspect().should.have.properties([ - 'name', 'baseDir', - 'env', 'subdomainOffset', - 'controller', 'middlewares', 'serviceClasses', - 'config', 'urllib', 'loggers', - ]); - app.inspect().name.should.equal('demo'); - app.close(); - done(); - }); + let app; + before(() => { + app = utils.app('apps/demo'); + return app.ready(); + }); + after(() => app.close()); + + it('should inspect app properties', () => { + app.inspect().should.have.properties([ + 'name', 'baseDir', + 'env', 'subdomainOffset', + 'controller', 'middlewares', 'serviceClasses', + 'config', 'urllib', 'loggers', + ]); + app.inspect().name.should.equal('demo'); }); }); diff --git a/test/app/extend/context.test.js b/test/app/extend/context.test.js index 4bbe6d78b4..56cfc9c7ea 100644 --- a/test/app/extend/context.test.js +++ b/test/app/extend/context.test.js @@ -260,36 +260,61 @@ describe('test/app/extend/context.test.js', () => { }); describe('ctx.roleFailureHandler()', () => { - it('should detect ajax', function* () { - const context = yield utils.createContext({ isAjax: true }); + let app; + before(() => { + app = utils.app('apps/demo'); + return app.ready(); + }); + after(() => app.close()); + afterEach(mm.restore); + + it('should detect ajax', () => { + const context = app.mockContext({ isAjax: true }); context.roleFailureHandler('admin'); context.body.should.eql({ message: 'Forbidden, required role: admin', stat: 'deny' }); }); it('should response message when is not ajax', function* () { - const context = yield utils.createContext(); + const context = app.mockContext(); context.roleFailureHandler('admin'); context.body.should.equal('Forbidden, required role: admin'); }); }); describe('ctx.curl()', () => { + let app; + before(() => { + app = utils.app('apps/demo'); + return app.ready(); + }); + after(() => app.close()); + afterEach(mm.restore); + it('should curl ok', function* () { - const context = yield utils.createContext(); + this.timeout(10000); + const context = app.mockContext(); const res = yield context.curl('https://a.alipayobjects.com/aliBridge/1.0.0/aliBridge.min.js'); res.status.should.equal(200); }); }); describe('ctx.realStatus', () => { - it('should get from status ok', function* () { - const context = yield utils.createContext(); + let app; + beforeEach(() => { + app = utils.app('apps/demo'); + return app.ready(); + }); + afterEach(() => app.close()); + afterEach(mm.restore); + + it('should get from status ok', () => { + const context = app.mockContext(); context.status = 200; context.realStatus.should.equal(200); }); it('should get from realStatus ok', () => { - const context = utils.createContext(); + const context = app.mockContext(); context.status = 302; context.realStatus = 500; context.realStatus.should.equal(500); @@ -297,8 +322,16 @@ describe('test/app/extend/context.test.js', () => { }); describe('ctx.state', () => { - it('should delegate ctx.locals', function* () { - const context = yield utils.createContext(); + let app; + beforeEach(() => { + app = utils.app('apps/demo'); + return app.ready(); + }); + afterEach(() => app.close()); + afterEach(mm.restore); + + it('should delegate ctx.locals', () => { + const context = app.mockContext(); context.locals = { a: 'a', b: 'b' }; context.state = { a: 'aa', c: 'cc' }; context.state.should.eql({ a: 'aa', b: 'b', c: 'cc' }); diff --git a/test/app/extend/helper.test.js b/test/app/extend/helper.test.js index 6e958e7d4b..4e72f99818 100644 --- a/test/app/extend/helper.test.js +++ b/test/app/extend/helper.test.js @@ -9,6 +9,7 @@ describe('test/app/extend/helper.test.js', () => { app = utils.app('apps/helper'); return app.ready(); }); + after(() => app.close()); describe('pathFor()', () => { it('should get home path url', () => { diff --git a/test/app/extend/request.test.js b/test/app/extend/request.test.js index db4ca34f67..ddcd69a786 100644 --- a/test/app/extend/request.test.js +++ b/test/app/extend/request.test.js @@ -1,32 +1,40 @@ 'use strict'; const mm = require('egg-mock'); -const should = require('should'); -const merge = require('merge-descriptors'); const urllib = require('urllib'); const request = require('supertest'); const utils = require('../../utils'); -const requestExt = require('../../../app/extend/request'); + describe('test/app/extend/request.test.js', () => { + + let app; + let ctx; + let req; + beforeEach(() => { + app = utils.app('apps/demo'); + return app.ready(); + }); + beforeEach(() => { + ctx = app.mockContext(); + req = ctx.request; + }); + afterEach(() => app.close()); afterEach(mm.restore); describe('req.host', () => { it('should return host with port', function* () { - const req = yield utils.createRequest(); - req.header.host = 'foo.com:3000'; + mm(req.header, 'host', 'foo.com:3000'); req.hostname.should.equal('foo.com'); }); it('should return "localhost" when no host present', function* () { - const req = yield utils.createRequest(); req.host.should.be.a.String; req.host.should.equal('localhost'); }); it('should return host from X-Forwarded-Host header', function* () { - const req = yield utils.createRequest(); - req.header['x-forwarded-host'] = 'foo.com'; + mm(req.header, 'x-forwarded-host', 'foo.com'); req.host.should.be.a.String; req.host.should.equal('foo.com'); }); @@ -34,13 +42,11 @@ describe('test/app/extend/request.test.js', () => { describe('req.hostname', () => { it('should return hostname with port', function* () { - const req = yield utils.createRequest(); - req.header.host = 'foo.com:3000'; + mm(req.header, 'host', 'foo.com:3000'); req.hostname.should.equal('foo.com'); }); it('should return "localhost" when no host present', function* () { - const req = yield utils.createRequest(); req.hostname.should.be.a.String; req.hostname.should.equal('localhost'); }); @@ -48,8 +54,7 @@ describe('test/app/extend/request.test.js', () => { describe('req.ip', () => { it('should return ipv4', function* () { - const req = yield utils.createRequest(); - req.socket.remoteAddress = '::ffff:127.0.0.1'; + mm(req.socket, 'remoteAddress', '::ffff:127.0.0.1'); req.ip.should.equal('127.0.0.1'); req.ip.should.equal('127.0.0.1'); }); @@ -57,33 +62,24 @@ describe('test/app/extend/request.test.js', () => { describe('req.ips', () => { it('should used x-forwarded-for', function* () { - const req = yield utils.createRequest(); - req.header['x-forwarded-for'] = '127.0.0.1,127.0.0.2'; + mm(req.header, 'x-forwarded-for', '127.0.0.1,127.0.0.2'); req.ips.should.eql([ '127.0.0.1', '127.0.0.2' ]); }); it('should used x-real-ip', function* () { - const req = yield utils.createRequest(); - req.header['x-forwarded-for'] = ''; - req.header['x-real-ip'] = '127.0.0.1,127.0.0.2'; + mm(req.header, 'x-forwarded-for', ''); + mm(req.header, 'x-real-ip', '127.0.0.1,127.0.0.2'); req.ips.should.eql([ '127.0.0.1', '127.0.0.2' ]); }); it('should return []', function* () { - const req = yield utils.createRequest(); - req.header['x-forwarded-for'] = ''; - req.header['x-real-ip'] = ''; + mm(req.header, 'x-forwarded-for', ''); + mm(req.header, 'x-real-ip', ''); req.ips.should.eql([]); }); }); describe('req.protocol', () => { - let app; - beforeEach(() => { - app = utils.app('apps/demo'); - return app.ready(); - }); - afterEach(() => app.close()); it('should return http when it not config and no protocol header', () => { mm(app.config, 'protocl', null); @@ -132,103 +128,111 @@ describe('test/app/extend/request.test.js', () => { }); describe('this.query[key] => String', () => { + function expectQuery(querystring, query) { + mm(req, 'querystring', querystring); + req.query.should.eql(query); + mm.restore(); + } + it('should get string value', () => { - createRequest('a=b').query.should.eql({ a: 'b' }); - createRequest('a=&').query.should.eql({ a: '' }); - createRequest('a=b&').query.should.eql({ a: 'b' }); - createRequest('a.=b').query.should.eql({ 'a.': 'b' }); - createRequest('a=b&a=c').query.should.eql({ a: 'b' }); - createRequest('a=&a=c').query.should.eql({ a: '' }); - createRequest('a=c&a=b').query.should.eql({ a: 'c' }); - createRequest('a=c&a=b&b=bb').query.should.eql({ a: 'c', b: 'bb' }); - createRequest('a[=c&a[=b').query.should.eql({ 'a[': 'c' }); - createRequest('a{=c&a{=b').query.should.eql({ 'a{': 'c' }); - createRequest('a[]=c&a[]=b').query.should.eql({ 'a[]': 'c' }); - createRequest('a[]=&a[]=b').query.should.eql({ 'a[]': '' }); - createRequest('a[foo]=c').query.should.eql({ 'a[foo]': 'c' }); - createRequest('a[foo][bar]=c').query.should.eql({ 'a[foo][bar]': 'c' }); - createRequest('a=').query.should.eql({ a: '' }); - createRequest('a[]=a&a=b&a=c').query.should.eql({ 'a[]': 'a', a: 'b' }); + expectQuery('a=b', { a: 'b' }); + expectQuery('a=&', { a: '' }); + expectQuery('a=b&', { a: 'b' }); + expectQuery('a.=b', { 'a.': 'b' }); + expectQuery('a=b&a=c', { a: 'b' }); + expectQuery('a=&a=c', { a: '' }); + expectQuery('a=c&a=b', { a: 'c' }); + expectQuery('a=c&a=b&b=bb', { a: 'c', b: 'bb' }); + expectQuery('a[=c&a[=b', { 'a[': 'c' }); + expectQuery('a{=c&a{=b', { 'a{': 'c' }); + expectQuery('a[]=c&a[]=b', { 'a[]': 'c' }); + expectQuery('a[]=&a[]=b', { 'a[]': '' }); + expectQuery('a[foo]=c', { 'a[foo]': 'c' }); + expectQuery('a[foo][bar]=c', { 'a[foo][bar]': 'c' }); + expectQuery('a=', { a: '' }); + expectQuery('a[]=a&a=b&a=c', { 'a[]': 'a', a: 'b' }); }); it('should get undefined when key not exists', () => { - const request = createRequest('a=b'); - request.query.should.eql({ a: 'b' }); - should.not.exist(request.query.foo); + expectQuery('a=b', { a: 'b' }); }); }); describe('this.queries[key] => Array', () => { + function expectQueries(querystring, query) { + mm(req, 'querystring', querystring); + req.queries.should.eql(query); + mm.restore(); + } + it('should get array value', () => { - createRequest('').queries.should.eql({ }); - createRequest('a=').queries.should.eql({ a: [ '' ] }); - createRequest('a=&').queries.should.eql({ a: [ '' ] }); - createRequest('a=b&').queries.should.eql({ a: [ 'b' ] }); - createRequest('a.=').queries.should.eql({ 'a.': [ '' ] }); - createRequest('a=&a=&a=&a=&a=&a=&a=&a=').queries.should.eql({ a: [ '', '', '', '', '', '', '', '' ] }); - createRequest('a=&a=&a=&a=&a=&a=&a=&a=&').queries.should.eql({ a: [ '', '', '', '', '', '', '', '' ] }); - createRequest('a=&a=&a=&a=&a=&a=&a=&a=&&&&').queries.should.eql({ a: [ '', '', '', '', '', '', '', '' ] }); - createRequest('a=b').queries.should.eql({ a: [ 'b' ] }); - createRequest('a={}').queries.should.eql({ a: [ '{}' ] }); - createRequest('a=[]').queries.should.eql({ a: [ '[]' ] }); - createRequest('a[]=[]').queries.should.eql({ 'a[]': [ '[]' ], a: [ '[]' ] }); - createRequest('a[]=&a[]=').queries.should.eql({ 'a[]': [ '', '' ], a: [ '', '' ] }); - createRequest('a[]=[]&a[]=[]').queries.should.eql({ 'a[]': [ '[]', '[]' ], a: [ '[]', '[]' ] }); - createRequest('a=b&a=c').queries.should.eql({ a: [ 'b', 'c' ] }); - createRequest('a=&a=c').queries.should.eql({ a: [ '', 'c' ] }); - createRequest('a=c&a=b').queries.should.eql({ a: [ 'c', 'b' ] }); - createRequest('a=c&a=b&b=bb').queries.should.eql({ a: [ 'c', 'b' ], b: [ 'bb' ] }); - createRequest('a[=c&a[=b').queries.should.eql({ 'a[': [ 'c', 'b' ] }); - createRequest('a{=c&a{=b').queries.should.eql({ 'a{': [ 'c', 'b' ] }); - createRequest('a[]=c&a[]=b').queries.should.eql({ 'a[]': [ 'c', 'b' ], a: [ 'c', 'b' ] }); - createRequest('a[]=&a[]=b').queries.should.eql({ 'a[]': [ '', 'b' ], a: [ '', 'b' ] }); - createRequest('a[]=&a[]=b&a=foo').queries.should.eql({ 'a[]': [ '', 'b' ], a: [ 'foo' ] }); - createRequest('a=bar&a[]=&a[]=b&a=foo').queries.should.eql({ 'a[]': [ '', 'b' ], a: [ 'bar', 'foo' ] }); + expectQueries('', { }); + expectQueries('a=', { a: [ '' ] }); + expectQueries('a=&', { a: [ '' ] }); + expectQueries('a=b&', { a: [ 'b' ] }); + expectQueries('a.=', { 'a.': [ '' ] }); + expectQueries('a=&a=&a=&a=&a=&a=&a=&a=', { a: [ '', '', '', '', '', '', '', '' ] }); + expectQueries('a=&a=&a=&a=&a=&a=&a=&a=&', { a: [ '', '', '', '', '', '', '', '' ] }); + expectQueries('a=&a=&a=&a=&a=&a=&a=&a=&&&&', { a: [ '', '', '', '', '', '', '', '' ] }); + expectQueries('a=b', { a: [ 'b' ] }); + expectQueries('a={}', { a: [ '{}' ] }); + expectQueries('a=[]', { a: [ '[]' ] }); + expectQueries('a[]=[]', { 'a[]': [ '[]' ], a: [ '[]' ] }); + expectQueries('a[]=&a[]=', { 'a[]': [ '', '' ], a: [ '', '' ] }); + expectQueries('a[]=[]&a[]=[]', { 'a[]': [ '[]', '[]' ], a: [ '[]', '[]' ] }); + expectQueries('a=b&a=c', { a: [ 'b', 'c' ] }); + expectQueries('a=&a=c', { a: [ '', 'c' ] }); + expectQueries('a=c&a=b', { a: [ 'c', 'b' ] }); + expectQueries('a=c&a=b&b=bb', { a: [ 'c', 'b' ], b: [ 'bb' ] }); + expectQueries('a[=c&a[=b', { 'a[': [ 'c', 'b' ] }); + expectQueries('a{=c&a{=b', { 'a{': [ 'c', 'b' ] }); + expectQueries('a[]=c&a[]=b', { 'a[]': [ 'c', 'b' ], a: [ 'c', 'b' ] }); + expectQueries('a[]=&a[]=b', { 'a[]': [ '', 'b' ], a: [ '', 'b' ] }); + expectQueries('a[]=&a[]=b&a=foo', { 'a[]': [ '', 'b' ], a: [ 'foo' ] }); + expectQueries('a=bar&a[]=&a[]=b&a=foo', { 'a[]': [ '', 'b' ], a: [ 'bar', 'foo' ] }); // a[][] 这种不支持自动变化为 a - createRequest('a[][]=&a[][]=b').queries.should.eql({ 'a[][]': [ '', 'b' ] }); - createRequest('a][]=&a][]=b').queries.should.eql({ 'a][]': [ '', 'b' ] }); - createRequest('a[[]=&a[[]=b').queries.should.eql({ 'a[[]': [ '', 'b' ] }); - createRequest('[]=&[]=b').queries.should.eql({ '[]': [ '', 'b' ] }); + expectQueries('a[][]=&a[][]=b', { 'a[][]': [ '', 'b' ] }); + expectQueries('a][]=&a][]=b', { 'a][]': [ '', 'b' ] }); + expectQueries('a[[]=&a[[]=b', { 'a[[]': [ '', 'b' ] }); + expectQueries('[]=&[]=b', { '[]': [ '', 'b' ] }); // a[], a 混搭的时候,只返回最后一个 a 的值 - createRequest('a[]=a&a=b&a=c').queries.should.eql({ 'a[]': [ 'a' ], a: [ 'b', 'c' ] }); + expectQueries('a[]=a&a=b&a=c', { 'a[]': [ 'a' ], a: [ 'b', 'c' ] }); // object - createRequest('a[foo]=c').queries.should.eql({ 'a[foo]': [ 'c' ] }); - createRequest('a[foo]=c&a=b').queries.should.eql({ 'a[foo]': [ 'c' ], a: [ 'b' ] }); - createRequest('a[foo]=c&a=b&b=bb&d=d1&d=d2').queries.should.eql({ + expectQueries('a[foo]=c', { 'a[foo]': [ 'c' ] }); + expectQueries('a[foo]=c&a=b', { 'a[foo]': [ 'c' ], a: [ 'b' ] }); + expectQueries('a[foo]=c&a=b&b=bb&d=d1&d=d2', { 'a[foo]': [ 'c' ], a: [ 'b' ], b: [ 'bb' ], d: [ 'd1', 'd2' ], }); - createRequest('a[foo]=c&a[]=b&a[]=d').queries.should.eql({ + expectQueries('a[foo]=c&a[]=b&a[]=d', { 'a[foo]': [ 'c' ], 'a[]': [ 'b', 'd' ], a: [ 'b', 'd' ], }); - createRequest('a[foo]=c&a[]=b&a[]=d&c=cc&c=c2&c=').queries.should.eql({ + expectQueries('a[foo]=c&a[]=b&a[]=d&c=cc&c=c2&c=', { 'a[foo]': [ 'c' ], 'a[]': [ 'b', 'd' ], a: [ 'b', 'd' ], c: [ 'cc', 'c2', '' ], }); - createRequest('a[foo][bar]=c').queries.should.eql({ + expectQueries('a[foo][bar]=c', { 'a[foo][bar]': [ 'c' ], }); }); it('should get undefined when key not exists', () => { - const request = createRequest('a=b'); - request.queries.should.eql({ a: [ 'b' ] }); - should.not.exist(request.queries.foo); + expectQueries('a=b', { a: [ 'b' ] }); }); }); describe('this.query = obj', () => { it('should set query with object', () => { - const req = createRequest('a=c'); + mm(req, 'querystring', 'a=c'); req.query.should.eql({ a: 'c' }); req.query = {}; req.query.should.eql({}); @@ -246,13 +250,12 @@ describe('test/app/extend/request.test.js', () => { describe('request.acceptJSON', () => { it('should true when isAjax', function* () { - const req = yield utils.createRequest(); mm(req.req.headers, 'x-requested-with', 'XMLHttpRequest'); req.acceptJSON.should.equal(true); }); it('should true when response is json', function* () { - const context = yield utils.createContext({ + const context = app.mockContext({ headers: { accept: 'text/html', }, @@ -263,7 +266,7 @@ describe('test/app/extend/request.test.js', () => { }); it('should true when accept json', function* () { - const context = yield utils.createContext({ + const context = app.mockContext({ headers: { accept: 'application/json', }, @@ -273,12 +276,13 @@ describe('test/app/extend/request.test.js', () => { }); it('should false when do not accept json', function* () { - const request = yield utils.createRequest({ + const context = app.mockContext({ headers: { accept: 'text/html', }, url: '/', }); + const request = context.request; request.acceptJSON.should.equal(false); }); }); @@ -319,14 +323,4 @@ describe('test/app/extend/request.test.js', () => { }); }); - function createRequest(querystring) { - const app = { - context: {}, - request: { - querystring, - }, - }; - merge(app.request, requestExt); - return app.request; - } }); diff --git a/test/app/middleware/site_file.test.js b/test/app/middleware/site_file.test.js index 317b3079e2..dd2366d00a 100644 --- a/test/app/middleware/site_file.test.js +++ b/test/app/middleware/site_file.test.js @@ -2,6 +2,7 @@ const should = require('should'); const request = require('supertest'); +const assert = require('assert'); const utils = require('../../utils'); describe('test/app/middleware/site_file.test.js', () => { @@ -47,7 +48,8 @@ describe('test/app/middleware/site_file.test.js', () => { return request(app.callback()) .head('/robots.txt') .expect('content-length', '72') - .expect('') // body must be empty for HEAD + // body must be empty for HEAD + .expect(res => assert.equal(res.text, undefined)) .expect(200); }); diff --git a/test/fixtures/apps/context-config-app/app/context.js b/test/fixtures/apps/context-config-app/app/extend/context.js similarity index 100% rename from test/fixtures/apps/context-config-app/app/context.js rename to test/fixtures/apps/context-config-app/app/extend/context.js diff --git a/test/fixtures/apps/view/app/helper.js b/test/fixtures/apps/view/app/extend/helper.js similarity index 100% rename from test/fixtures/apps/view/app/helper.js rename to test/fixtures/apps/view/app/extend/helper.js diff --git a/test/lib/agent.test.js b/test/lib/agent.test.js index c1b91dcf5c..347606f310 100644 --- a/test/lib/agent.test.js +++ b/test/lib/agent.test.js @@ -25,23 +25,15 @@ describe('test/lib/agent.test.js', () => { }); }); - describe('require agent', () => { - it('should exit normal', () => { - execSync(`${process.execPath} -e "require('./lib/agent')"`, { - timeout: 3000, - }); - }); - }); - describe('close()', () => { - it('should close all listeners', function() { + it('should close all listeners', function* () { const baseDir = path.join(__dirname, '../fixtures/apps/demo'); const agent = new Agent({ baseDir, }); process.listeners('unhandledRejection') .indexOf(agent._unhandledRejectionHandler).should.not.equal(-1); - agent.close(); + yield agent.close(); process.listeners('unhandledRejection') .indexOf(agent._unhandledRejectionHandler).should.equal(-1); }); @@ -85,4 +77,14 @@ describe('test/lib/agent.test.js', () => { }); }); }); + + if (process.platform !== 'win32') { + describe('require agent', () => { + it('should exit normal', () => { + execSync(`${process.execPath} -e "require('./lib/agent')"`, { + timeout: 3000, + }); + }); + }); + } }); diff --git a/test/lib/application.test.js b/test/lib/application.test.js index a6fe43d081..8cee7e74c9 100644 --- a/test/lib/application.test.js +++ b/test/lib/application.test.js @@ -60,14 +60,14 @@ describe('test/lib/application.test.js', () => { }); describe('close()', () => { - it('should close all listeners', () => { + it('should close all listeners', function* () { const baseDir = path.join(__dirname, '../fixtures/apps/demo'); const application = new Application({ baseDir, }); process.listeners('unhandledRejection') .indexOf(application._unhandledRejectionHandler).should.not.equal(-1); - application.close(); + yield application.close(); process.listeners('unhandledRejection') .indexOf(application._unhandledRejectionHandler).should.equal(-1); }); diff --git a/test/lib/cluster/master.test.js b/test/lib/cluster/master.test.js index 8544cd9d7a..1d3be32d85 100644 --- a/test/lib/cluster/master.test.js +++ b/test/lib/cluster/master.test.js @@ -160,7 +160,7 @@ describe('test/lib/cluster/master.test.js', () => { describe('spawn start', () => { let app; - after(() => { + afterEach(() => { // make sure process exit app.proc.kill('SIGTERM'); }); diff --git a/test/lib/core/agent_worker_client.test.js b/test/lib/core/agent_worker_client.test.js index 65c042d7fc..0226801ced 100644 --- a/test/lib/core/agent_worker_client.test.js +++ b/test/lib/core/agent_worker_client.test.js @@ -13,6 +13,7 @@ const fs = require('fs'); const fixtures = path.join(__dirname, '../../fixtures'); describe('test/lib/core/agent_worker_client.test.js', () => { + describe('single process', () => { let agent; let client; @@ -80,7 +81,7 @@ describe('test/lib/core/agent_worker_client.test.js', () => { client.ready(done); realClient.ready(true); }); - + after(() => agent.close()); afterEach(mm.restore); it('should ready', done => { @@ -91,19 +92,6 @@ describe('test/lib/core/agent_worker_client.test.js', () => { }, 100); }); - it('should not exit when dumpConfig error', () => { - const writeFileSync = fs.writeFileSync; - mm(fs, 'writeFileSync', function() { - if (arguments[0] && arguments[0].endsWith('config.json')) { - throw new Error('mock error'); - } - writeFileSync.apply(fs, arguments); - }); - new Agent({ - baseDir: path.join(fixtures, 'apps/demo'), - }); - }); - it('should subscribe well', done => { mm(client, '_broadcast', (command, info) => { command.should.equal('mock_subscribe_changed'); @@ -205,6 +193,25 @@ describe('test/lib/core/agent_worker_client.test.js', () => { }); }); + describe('when dumpConfig error', () => { + let agent; + after(() => agent.close()); + + it('should not exit ', done => { + const writeFileSync = fs.writeFileSync; + mm(fs, 'writeFileSync', function() { + if (arguments[0] && arguments[0].endsWith('config.json')) { + throw new Error('mock error'); + } + writeFileSync.apply(fs, arguments); + }); + agent = new Agent({ + baseDir: path.join(fixtures, 'apps/demo'), + }); + agent.ready(done); + }); + }); + describe('cluster', () => { let app; @@ -212,7 +219,6 @@ describe('test/lib/core/agent_worker_client.test.js', () => { app = utils.cluster('apps/agent-app'); return app.ready(); }); - after(() => app.close()); it('should request ok', () => { @@ -270,7 +276,6 @@ describe('test/lib/core/agent_worker_client.test.js', () => { app.expect('code', 0); return app.ready(); }); - before(done => { app.process.send({ action: 'die', diff --git a/test/lib/core/app_worker_client.test.js b/test/lib/core/app_worker_client.test.js index 92fbd90e1d..d3dd41a6a4 100644 --- a/test/lib/core/app_worker_client.test.js +++ b/test/lib/core/app_worker_client.test.js @@ -1,5 +1,6 @@ 'use strict'; +const fs = require('fs'); const should = require('should'); const mm = require('egg-mock'); const pedding = require('pedding'); @@ -13,6 +14,9 @@ describe('test/lib/core/app_worker_client.test.js', () => { let client; before(() => { + const dir = utils.getFilepath('apps/demo/logs/demo'); + console.log(dir, fs.existsSync(dir)); + console.log(fs.readdirSync(dir)); app = utils.app('apps/demo'); return app.ready(); }); @@ -49,6 +53,7 @@ describe('test/lib/core/app_worker_client.test.js', () => { client.ready(done); }); + after(() => app.close()); afterEach(mm.restore); @@ -213,12 +218,10 @@ describe('test/lib/core/app_worker_client.test.js', () => { app = utils.cluster('apps/agent-client-app'); yield app.ready(); }); + after(() => app.close()); - beforeEach(mm.restore); + afterEach(mm.restore); - after(() => { - app.close(); - }); it('should subscribe second time ok', done => { request(app.callback()) diff --git a/test/lib/core/cookies.test.js b/test/lib/core/cookies.test.js index 1d615b59cb..2e5650b841 100644 --- a/test/lib/core/cookies.test.js +++ b/test/lib/core/cookies.test.js @@ -14,7 +14,6 @@ describe('test/lib/core/cookies.test.js', () => { app = utils.app('apps/secure-app'); return app.ready(); }); - after(() => app.close()); it('should throw TypeError when set secure on not secure request', () => { @@ -167,8 +166,14 @@ describe('test/lib/core/cookies.test.js', () => { }); describe('secure = false', () => { + let app; + before(() => { + app = utils.app('apps/demo'); + return app.ready(); + }); + after(() => app.close()); + it('should set secure:false cookie', done => { - const app = utils.app('apps/demo'); request(app.callback()) .get('/hello') .set('Host', 'demo.eggjs.org') @@ -190,7 +195,6 @@ describe('test/lib/core/cookies.test.js', () => { app = utils.app('apps/encrypt-cookies'); return app.ready(); }); - after(() => app.close()); it('should get encrypt cookie', done => { diff --git a/test/lib/core/loader/load_plugin.test.js b/test/lib/core/loader/load_plugin.test.js index 68cce13ef8..428dff2e73 100644 --- a/test/lib/core/loader/load_plugin.test.js +++ b/test/lib/core/loader/load_plugin.test.js @@ -9,13 +9,14 @@ const utils = require('../../../utils'); const EGG_BASE = path.join(__dirname, '../../../../'); -describe('test/lib/core/loader.test.js', () => { +describe('test/lib/core/loader/load_plugin.test.js', () => { let app; const logger = console; before(() => { app = utils.app('apps/empty'); return app.ready(); }); + after(() => app.close()); afterEach(mm.restore); it('should loadConfig all plugins', () => { @@ -32,6 +33,7 @@ describe('test/lib/core/loader.test.js', () => { dep: [], env: [], path: path.join(baseDir, 'node_modules/b'), + from: path.join(baseDir, 'config/plugin.js'), }); appLoader.plugins.c.should.eql({ enable: true, @@ -39,6 +41,7 @@ describe('test/lib/core/loader.test.js', () => { dep: [], env: [], path: path.join(baseDir, 'node_modules/c'), + from: path.join(baseDir, 'config/plugin.js'), }); appLoader.plugins.e.should.eql({ enable: true, @@ -46,6 +49,7 @@ describe('test/lib/core/loader.test.js', () => { dep: [ 'f' ], env: [], path: path.join(baseDir, 'plugins/e'), + from: path.join(baseDir, 'config/plugin.js'), }); appLoader.plugins.onerror.path.should.equal(fs.realpathSync(path.join(EGG_BASE, 'node_modules/egg-onerror'))); appLoader.plugins.onerror.package.should.equal('egg-onerror'); @@ -69,6 +73,7 @@ describe('test/lib/core/loader.test.js', () => { env: [], package: 'rds', path: path.join(baseDir, 'node_modules/rds'), + from: path.join(baseDir, 'config/plugin.js'), }); }); @@ -87,6 +92,7 @@ describe('test/lib/core/loader.test.js', () => { dep: [], env: [], path: path.join(baseDir, 'node_modules/d'), + from: path.join(baseDir, 'config/plugin.js'), }); should.not.exists(appLoader.plugins.d); }); @@ -105,6 +111,7 @@ describe('test/lib/core/loader.test.js', () => { dep: [ 'f' ], env: [], path: path.join(baseDir, 'plugins/g'), + from: path.join(baseDir, 'config/plugin.js'), }); }); @@ -152,6 +159,7 @@ describe('test/lib/core/loader.test.js', () => { dep: [], env: [ 'unittest' ], path: path.join(baseDir, 'node_modules/d'), + from: path.join(baseDir, 'config/plugin.js'), }); appLoader.plugins.foo.should.eql({ enable: true, @@ -277,6 +285,7 @@ describe('test/lib/core/loader.test.js', () => { dep: [ 'd1' ], env: [ 'local', 'prod' ], path: path.join(baseDir, 'node_modules/a1'), + from: path.join(baseDir, 'config/plugin.js'), }); }); }); diff --git a/test/lib/core/loader/load_service.test.js b/test/lib/core/loader/load_service.test.js index c227f54dc5..3b9ad2df47 100644 --- a/test/lib/core/loader/load_service.test.js +++ b/test/lib/core/loader/load_service.test.js @@ -7,70 +7,64 @@ const utils = require('../../../utils'); describe('test/lib/core/loader/load_service.test.js', () => { let app; - before(() => { - app = utils.app('apps/loader-plugin'); - return app.ready(); - }); - after(() => app.close()); + afterEach(() => app.close()); afterEach(mm.restore); - it('should load app and plugin services', done => { - app.ready(() => { - should.exists(app.serviceClasses.foo); - should.exists(app.serviceClasses.foo2); - should.not.exists(app.serviceClasses.bar1); - should.exists(app.serviceClasses.bar2); - should.exists(app.serviceClasses.foo4); + it('should load app and plugin services', function* () { + app = utils.app('apps/loader-plugin'); + yield app.ready(); + should.exists(app.serviceClasses.foo); + should.exists(app.serviceClasses.foo2); + should.not.exists(app.serviceClasses.bar1); + should.exists(app.serviceClasses.bar2); + should.exists(app.serviceClasses.foo4); - const ctx = app.mockContext(); - should.exists(ctx.service.fooDir.foo5); - should.exists(ctx.service.foo); - should.exists(ctx.service.foo2); - should.exists(ctx.service.bar2); - should.exists(ctx.service.foo4); + const ctx = app.mockContext(); + should.exists(ctx.service.fooDir.foo5); + should.exists(ctx.service.foo); + should.exists(ctx.service.foo2); + should.exists(ctx.service.bar2); + should.exists(ctx.service.foo4); - request(app.callback()) - .get('/') - .expect({ - foo2: 'foo2', - foo3: 'foo3', - }) - .expect(200, err => { - app.close(); - done(err); - }); - }); + yield request(app.callback()) + .get('/') + .expect({ + foo2: 'foo2', + foo3: 'foo3', + }) + .expect(200); }); it('should service support es6', function* () { - const app = utils.app('apps/services_loader_verify'); + app = utils.app('apps/services_loader_verify'); yield app.ready(); app.serviceClasses.should.have.property('foo'); app.serviceClasses.foo.should.have.properties('bar', 'bar1', 'aa'); - app.close(); }); - it('should support extend app.Service class', done => { - const app = utils.app('apps/service-app'); - app.ready(() => { - request(app.callback()) - .get('/user') - .expect(res => { - should.exists(res.body.user); - res.body.user.userId.should.equal('123mock'); - }) - .expect(200, err => { - app.close(); - done(err); - }); - }); + it('should support extend app.Service class', function* () { + app = utils.app('apps/service-app'); + yield app.ready(); + + yield request(app.callback()) + .get('/user') + .expect(res => { + should.exists(res.body.user); + res.body.user.userId.should.equal('123mock'); + }) + .expect(200); }); describe('sub dir', () => { - it('should support top 1 and 2 dirs, ignore others', done => { - mm(process.env, 'NO_DEPRECATION', '*'); - const app = utils.app('apps/subdir-services'); - request(app.callback()) + let app; + afterEach(() => app.close()); + afterEach(mm.restore); + + it('should support top 1 and 2 dirs, ignore others', function* () { + app = utils.app('apps/subdir-services'); + yield app.ready(); + + yield request(app.callback()) .get('/') .expect({ user: { @@ -106,10 +100,7 @@ describe('test/lib/core/loader/load_service.test.js', () => { serviceIsSame: true, oldStyle: '/', }) - .expect(200, err => { - app.close(); - done(err); - }); + .expect(200); }); }); }); diff --git a/test/lib/core/logger.test.js b/test/lib/core/logger.test.js index bb1ad4188f..67e9e47cbd 100644 --- a/test/lib/core/logger.test.js +++ b/test/lib/core/logger.test.js @@ -11,74 +11,76 @@ const utils = require('../../utils'); const Agent = require('../../..').Agent; describe('test/lib/core/logger.test.js', () => { + + let app; afterEach(mm.restore); + afterEach(() => app.close()); it('should got right default config on prod env', () => { mm.env('prod'); mm(process.env, 'EGG_LOG', ''); mm(process.env, 'HOME', utils.getFilepath('apps/mock-production-app/config')); - const app = utils.app('apps/mock-production-app'); + app = utils.app('apps/mock-production-app'); // 生产环境默认 _level = info app.logger.get('file').options.level.should.equal(Logger.INFO); // stdout 默认 INFO app.logger.get('console').options.level.should.equal(Logger.INFO); app.coreLogger.get('file').options.level.should.equal(Logger.INFO); app.coreLogger.get('console').options.level.should.equal(Logger.INFO); - app.close(); + return app.ready(); }); it('should got right level on local env', () => { mm.env('local'); mm(process.env, 'EGG_LOG', ''); - const app = utils.app('apps/mock-dev-app'); + app = utils.app('apps/mock-dev-app'); app.logger.get('file').options.level.should.equal(Logger.DEBUG); app.logger.get('console').options.level.should.equal(Logger.INFO); app.coreLogger.get('file').options.level.should.equal(Logger.DEBUG); app.coreLogger.get('console').options.level.should.equal(Logger.WARN); - app.close(); + return app.ready(); }); it('should set EGG_LOG level on local env', () => { mm.env('local'); mm(process.env, 'EGG_LOG', 'ERROR'); - const app = utils.app('apps/mock-dev-app'); + app = utils.app('apps/mock-dev-app'); app.logger.get('file').options.level.should.equal(Logger.DEBUG); app.logger.get('console').options.level.should.equal(Logger.ERROR); app.coreLogger.get('file').options.level.should.equal(Logger.DEBUG); app.coreLogger.get('console').options.level.should.equal(Logger.ERROR); - app.close(); + return app.ready(); }); it('should got right config on unittest env', () => { mm.env('unittest'); mm(process.env, 'EGG_LOG', ''); - const app = utils.app('apps/mock-dev-app'); + app = utils.app('apps/mock-dev-app'); app.logger.get('file').options.level.should.equal(Logger.INFO); app.logger.get('console').options.level.should.equal(Logger.WARN); app.coreLogger.get('file').options.level.should.equal(Logger.INFO); app.coreLogger.get('console').options.level.should.equal(Logger.WARN); - app.close(); + return app.ready(); }); it('should set log.consoleLevel to env.EGG_LOG', () => { mm(process.env, 'EGG_LOG', 'ERROR'); - const app = utils.app('apps/mock-dev-app'); + app = utils.app('apps/mock-dev-app'); app.logger.get('file').options.level.should.equal(Logger.INFO); app.logger.get('console').options.level.should.equal(Logger.ERROR); - app.close(); + return app.ready(); }); it('log buffer disable cache on local and unittest env', done => { mm(process.env, 'EGG_LOG', 'NONE'); - const app = utils.app('apps/nobuffer-logger'); + app = utils.app('apps/nobuffer-logger'); app.ready(() => { const ctx = app.mockContext(); const logfile = path.join(app.config.logger.dir, 'common-error.log'); // app.config.logger.buffer.should.equal(false); ctx.logger.error(new Error('mock nobuffer error')); setTimeout(() => { - app.close(); fs.readFileSync(logfile, 'utf8').should.containEql('nodejs.Error: mock nobuffer error\n'); done(); }, 1000); @@ -89,14 +91,13 @@ describe('test/lib/core/logger.test.js', () => { mm(process.env, 'EGG_LOG', 'none'); mm.env('prod'); mm(process.env, 'HOME', utils.getFilepath('apps/mock-production-app/config')); - const app = utils.app('apps/mock-production-app'); + app = utils.app('apps/mock-production-app'); app.ready(() => { const ctx = app.mockContext(); const logfile = path.join(app.config.logger.dir, 'common-error.log'); // app.config.logger.buffer.should.equal(true); ctx.logger.error(new Error('mock enable buffer error')); setTimeout(() => { - app.close(); fs.readFileSync(logfile, 'utf8').should.containEql(''); done(); }, 1000); @@ -106,13 +107,12 @@ describe('test/lib/core/logger.test.js', () => { it('output .json format log', done => { mm(process.env, 'EGG_LOG', 'none'); mm.env('local'); - const app = utils.app('apps/logger-output-json'); + app = utils.app('apps/logger-output-json'); app.ready(() => { const ctx = app.mockContext(); const logfile = path.join(app.config.logger.dir, 'logger-output-json-web.json.log'); ctx.logger.info('json format'); setTimeout(() => { - app.close(); fs.existsSync(logfile).should.be.true; fs.readFileSync(logfile, 'utf8').should.containEql('"message":"json format"'); done(); @@ -122,7 +122,7 @@ describe('test/lib/core/logger.test.js', () => { it('dont output to console after app ready', done => { mm.env('default'); - const app = utils.cluster('apps/logger'); + app = utils.cluster('apps/logger'); app .debug(false) .coverage(false) @@ -131,16 +131,12 @@ describe('test/lib/core/logger.test.js', () => { .notExpect('stdout', /app info after ready/) .expect('stderr', /nodejs.Error: agent error/) .expect('stderr', /nodejs.Error: app error/) - .end(err => { - app.close(); - should.not.exists(err); - done(); - }); + .end(done); }); it('should still output to console after app ready on local env', done => { mm.env('local'); - const app = utils.cluster('apps/logger'); + app = utils.cluster('apps/logger'); app // .debug() .coverage(false) @@ -149,24 +145,19 @@ describe('test/lib/core/logger.test.js', () => { .expect('stdout', /app info after ready/) .expect('stderr', /nodejs.Error: agent error/) .expect('stderr', /nodejs.Error: app error/) - .end(err => { - app.close(); - should.not.exists(err); - done(); - }); + .end(done); }); it('agent and app error should output to common-error.log', done => { const baseDir = utils.getFilepath('apps/logger'); mm.env('default'); mm(process.env, 'EGG_LOG', 'none'); - mm(process.env, 'HOME', baseDir); - const app = utils.cluster('apps/logger'); + mm(process.env, 'EGG_HOME', baseDir); + app = utils.cluster('apps/logger'); app // .debug() .coverage(false) .end(err => { - app.close(); should.not.exists(err); const content = fs.readFileSync(path.join(baseDir, 'logs/logger/common-error.log'), 'utf8'); content.should.containEql('nodejs.Error: agent error'); @@ -176,7 +167,7 @@ describe('test/lib/core/logger.test.js', () => { }); it('all loggers error should redirect to errorLogger', done => { - const app = utils.app('apps/logger'); + app = utils.app('apps/logger'); app.ready(() => { app.logger.error(new Error('logger error')); app.coreLogger.error(new Error('coreLogger error')); @@ -184,7 +175,6 @@ describe('test/lib/core/logger.test.js', () => { app.loggers.customLogger.error(new Error('customLogger error')); setTimeout(() => { - app.close(); const content = fs.readFileSync(path.join(app.baseDir, 'logs/logger/common-error.log'), 'utf8'); content.should.containEql('nodejs.Error: logger error'); content.should.containEql('nodejs.Error: coreLogger error'); @@ -196,11 +186,11 @@ describe('test/lib/core/logger.test.js', () => { }); it('agent\'s logger is same as coreLogger', done => { - const agent = new Agent({ + app = new Agent({ baseDir: utils.getFilepath('apps/logger'), }); - agent.logger.options.file.should.equal(agent.coreLogger.options.file); - agent.ready(done); + app.logger.options.file.should.equal(app.coreLogger.options.file); + app.ready(done); }); describe.skip('logger.reload()', () => { diff --git a/test/lib/plugins/depd.test.js b/test/lib/plugins/depd.test.js index 4f309d9f00..2d39506c44 100644 --- a/test/lib/plugins/depd.test.js +++ b/test/lib/plugins/depd.test.js @@ -8,7 +8,6 @@ describe('test/lib/plugins/depd.test.js', () => { let app; before(() => { - mm(process.env, 'NO_DEPRECATION', '*'); app = utils.app('apps/demo'); return app.ready(); }); diff --git a/test/lib/plugins/development.test.js b/test/lib/plugins/development.test.js index 3c73ab6318..4f5e279d4b 100644 --- a/test/lib/plugins/development.test.js +++ b/test/lib/plugins/development.test.js @@ -79,9 +79,9 @@ describe('test/lib/plugins/development.test.js', () => { app.debug(); return app.ready(); }); + after(() => app.close()); after(() => { fs.writeFileSync(filepath, body); - app.close(); }); it('should reload when file changed', done => { diff --git a/test/lib/plugins/schedule.test.js b/test/lib/plugins/schedule.test.js index d6f86e347a..ff5752f3f9 100644 --- a/test/lib/plugins/schedule.test.js +++ b/test/lib/plugins/schedule.test.js @@ -11,7 +11,7 @@ describe('test/lib/plugins/schedule.test.js', () => { }); yield app.ready(); yield sleep(5000); - app.close(); + yield app.close(); const log = getLogContent('schedule'); contains(log, 'cron').should.within(1, 2); }); diff --git a/test/lib/plugins/watcher.test.js b/test/lib/plugins/watcher.test.js index af0f9b9d49..b74006a9af 100644 --- a/test/lib/plugins/watcher.test.js +++ b/test/lib/plugins/watcher.test.js @@ -17,10 +17,8 @@ describe('test/lib/plugins/watcher.test.js', () => { return app.ready(); }); - afterEach(() => { - app.close(); - mm.restore(); - }); + afterEach(() => app.close()); + afterEach(mm.restore); it('should app watcher work', done => { const server = app.callback(); diff --git a/test/utils.js b/test/utils.js index a70ec6c989..af84201dd7 100644 --- a/test/utils.js +++ b/test/utils.js @@ -31,46 +31,6 @@ exports.getJSON = name => { return JSON.parse(fs.readFileSync(exports.getFilepath(name))); }; -// context helper, come from https://github.com/koajs/koa/blob/master/test/context.js -exports.createContext = (ctx, cb) => { - const app = exports.app('apps/demo'); - return new Promise(function(resolve, reject) { - app.ready(() => { - const mockCtx = app.mockContext(ctx); - if (cb) cb(mockCtx); - resolve(mockCtx); - }); - - app.on('error', err => { - reject(err); - }); - }); -}; - -exports.createRequest = function(ctx, cb) { - return new Promise(function(resolve, reject) { - exports.createContext(ctx).then(mockCtx => { - const req = mockCtx.request; - if (cb) cb(req); - resolve(req); - }, err => { - reject(err); - }); - }); -}; - -exports.createResponse = function(ctx, cb) { - return new Promise(function(resolve, reject) { - exports.createContext(ctx).then(mockCtx => { - const res = mockCtx.response; - if (cb) cb(res); - resolve(res); - }, err => { - reject(err); - }); - }); -}; - function formatOptions(name, options) { let baseDir; if (typeof name === 'string') {