diff --git a/.eslintrc b/.eslintrc index c799fe5327..7364c13643 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - "extends": "eslint-config-egg" + "extends": "eslint-config-egg", + "parserOptions": { + "ecmaVersion": 2017 + } } diff --git a/test/async/_async.js b/test/async/_async.js new file mode 100644 index 0000000000..ef60969e21 --- /dev/null +++ b/test/async/_async.js @@ -0,0 +1,28 @@ +'use strict'; + +const assert = require('assert'); +const request = require('supertest'); +const mm = require('egg-mock'); +const utils = require('../utils'); + +describe('test/async.test.js', () => { + afterEach(mm.restore); + let app; + before(async () => { + app = utils.app('apps/async-app'); + await app.ready(); + assert(app.beforeStartExectuted); + assert(app.scheduleExecuted); + }); + after(async () => { + await app.close(); + assert(app.beforeCloseExecuted); + }); + + it('middleware, controller and service support async functions', async () => { + await request(app.callback()) + .get('/api') + .expect(200) + .expect([ 'service', 'controller', 'router', 'middleware' ]); + }); +}); diff --git a/test/async/index.test.js b/test/async/index.test.js new file mode 100644 index 0000000000..79e55a46ed --- /dev/null +++ b/test/async/index.test.js @@ -0,0 +1,7 @@ +'use strict'; + +const nodeVersion = Number(process.version.match(/^v(\d+\.\d)+\./)[1]); +// only node >= 7.6 support async function without flags +if (nodeVersion >= 7.6) { + require('./_async'); +} diff --git a/test/fixtures/apps/async-app/app.js b/test/fixtures/apps/async-app/app.js new file mode 100644 index 0000000000..83f0c700ab --- /dev/null +++ b/test/fixtures/apps/async-app/app.js @@ -0,0 +1,14 @@ +'use strict'; + +module.exports = app => { + app.beforeStart(async () => { + await Promise.resolve(); + await app.runSchedule('async'); + app.beforeStartExectuted = true; + }); + + app.beforeClose(async () => { + await Promise.resolve(); + app.beforeCloseExecuted = true; + }); +}; diff --git a/test/fixtures/apps/async-app/app/controller/api.js b/test/fixtures/apps/async-app/app/controller/api.js new file mode 100644 index 0000000000..1106a2e0ee --- /dev/null +++ b/test/fixtures/apps/async-app/app/controller/api.js @@ -0,0 +1,11 @@ +'use strict'; + +module.exports = app => { + return class ApiController extends app.Controller { + async index() { + const result = await this.service.api.getName(); + this.ctx.body.push(result); + this.ctx.body.push('controller'); + } + }; +}; diff --git a/test/fixtures/apps/async-app/app/middleware/async.js b/test/fixtures/apps/async-app/app/middleware/async.js new file mode 100644 index 0000000000..e8f33d7064 --- /dev/null +++ b/test/fixtures/apps/async-app/app/middleware/async.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = () => { + return async (ctx, next) => { + ctx.body = []; + await next(); + ctx.body.push('middleware'); + }; +}; diff --git a/test/fixtures/apps/async-app/app/middleware/router.js b/test/fixtures/apps/async-app/app/middleware/router.js new file mode 100644 index 0000000000..d94c88d950 --- /dev/null +++ b/test/fixtures/apps/async-app/app/middleware/router.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = () => { + return async (ctx, next) => { + ctx.body = []; + await next(); + ctx.body.push('router'); + }; +}; diff --git a/test/fixtures/apps/async-app/app/router.js b/test/fixtures/apps/async-app/app/router.js new file mode 100644 index 0000000000..fd7b5af97c --- /dev/null +++ b/test/fixtures/apps/async-app/app/router.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = app => { + app.get('/api', app.middlewares.router(), 'api.index'); +}; diff --git a/test/fixtures/apps/async-app/app/schedule/async.js b/test/fixtures/apps/async-app/app/schedule/async.js new file mode 100644 index 0000000000..33509e8fd8 --- /dev/null +++ b/test/fixtures/apps/async-app/app/schedule/async.js @@ -0,0 +1,11 @@ +'use strict'; + +exports.schedule = { + type: 'worker', + interval: 1000000, +}; + +exports.task = async (ctx) => { + await Promise.resolve(); + ctx.app.scheduleExecuted = true; +}; diff --git a/test/fixtures/apps/async-app/app/service/api.js b/test/fixtures/apps/async-app/app/service/api.js new file mode 100644 index 0000000000..7b99b5c2ed --- /dev/null +++ b/test/fixtures/apps/async-app/app/service/api.js @@ -0,0 +1,14 @@ +'use strict'; + +module.exports = app => { + return class ApiService extends app.Service { + async getName() { + await sleep(100); + return 'service'; + } + }; +}; + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} diff --git a/test/fixtures/apps/async-app/config/config.default.js b/test/fixtures/apps/async-app/config/config.default.js new file mode 100644 index 0000000000..da85340680 --- /dev/null +++ b/test/fixtures/apps/async-app/config/config.default.js @@ -0,0 +1,4 @@ +'use strict'; + +exports.keys = 'key'; +exports.middleware = [ 'async' ]; diff --git a/test/fixtures/apps/async-app/package.json b/test/fixtures/apps/async-app/package.json new file mode 100644 index 0000000000..5260b45ea7 --- /dev/null +++ b/test/fixtures/apps/async-app/package.json @@ -0,0 +1,3 @@ +{ + "name": "async-app" +}