From 540f3fe040abd856adf8ec85be15b91fbd99106a Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 1 Jan 2023 23:36:23 +0800 Subject: [PATCH] feat: upgrade dependencies and devDependencies (#256) BREAKING CHANGE: drop Node.js < 14.17.0 support --- .github/workflows/nodejs.yml | 2 +- README.md | 7 ++- index.js | 2 - lib/egg.js | 11 ++-- lib/loader/context_loader.js | 2 +- lib/loader/mixin/config.js | 3 +- lib/loader/mixin/middleware.js | 4 +- package.json | 26 ++++----- test/asyncLocalStorage.test.js | 7 +-- test/egg-ts.test.js | 2 - test/egg.test.js | 31 +++++----- .../beforestart-with-timeout-env/app.js | 4 +- test/fixtures/beforestart/app.js | 5 +- test/fixtures/boot-configDidLoad-error/app.js | 4 +- test/fixtures/boot-didLoad-error/app.js | 4 +- test/fixtures/boot-didReady-error/app.js | 4 +- test/fixtures/boot-serverDidLoad-error/app.js | 4 +- test/fixtures/boot-timeout/app.js | 4 +- test/fixtures/boot-willReady-error/app.js | 4 +- test/fixtures/boot/agent.js | 2 +- test/fixtures/boot/app.js | 2 +- .../boot/app/plugin/boot-plugin/agent.js | 3 +- .../boot/app/plugin/boot-plugin/app.js | 3 +- test/fixtures/configmeta/config/config.js | 8 +-- test/fixtures/egg-jest/__tests__/index.js | 15 ----- .../egg-jest/app/extend/application.js | 7 --- .../egg-jest/app/middleware/status.js | 12 ---- .../egg-jest/config/config.default.js | 13 ----- .../egg-jest/config/config.unittest.js | 5 -- test/fixtures/egg-jest/config/plugin.js | 15 ----- test/fixtures/egg-jest/index.js | 42 -------------- test/fixtures/egg-jest/package.json | 3 - .../egg-jest/plugins/hsfclient/package.json | 5 -- test/fixtures/egg/index.js | 5 +- .../node_modules/d}/package.json | 2 +- test/index.test.js | 2 - test/jest.test.js | 18 ------ test/lifecycle.test.js | 4 +- test/loader/context_loader.test.js | 2 - test/loader/egg_loader.test.js | 2 - test/loader/file_loader.test.js | 4 +- test/loader/get_app_info.test.js | 2 - test/loader/get_appname.test.js | 2 - test/loader/get_framework_paths.test.js | 8 +-- test/loader/get_load_units.test.js | 8 +-- test/loader/get_server_env.test.js | 20 +++---- test/loader/load_file.test.js | 6 +- test/loader/mixin/load_agent_extend.test.js | 12 ++-- .../mixin/load_application_extend.test.js | 12 ++-- test/loader/mixin/load_config.test.js | 10 ++-- test/loader/mixin/load_controller.test.js | 2 - test/loader/mixin/load_custom_agent.test.js | 12 ++-- test/loader/mixin/load_custom_loader.test.js | 8 +-- test/loader/mixin/load_extend.test.js | 20 +++---- test/loader/mixin/load_helper_extend.test.js | 6 +- test/loader/mixin/load_middleware.test.js | 24 +++----- test/loader/mixin/load_plugin.test.js | 56 +++++++++---------- test/loader/mixin/load_service.test.js | 10 ++-- test/utils.js | 5 ++ test/utils/index.test.js | 4 +- test/utils/router.test.js | 4 +- 61 files changed, 156 insertions(+), 379 deletions(-) delete mode 100644 test/fixtures/egg-jest/__tests__/index.js delete mode 100644 test/fixtures/egg-jest/app/extend/application.js delete mode 100644 test/fixtures/egg-jest/app/middleware/status.js delete mode 100644 test/fixtures/egg-jest/config/config.default.js delete mode 100644 test/fixtures/egg-jest/config/config.unittest.js delete mode 100644 test/fixtures/egg-jest/config/plugin.js delete mode 100644 test/fixtures/egg-jest/index.js delete mode 100644 test/fixtures/egg-jest/package.json delete mode 100644 test/fixtures/egg-jest/plugins/hsfclient/package.json rename test/fixtures/{egg-jest/node_modules/session => scope-env/node_modules/d}/package.json (53%) delete mode 100644 test/jest.test.js diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 172a9d6a..0c2841a5 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -14,4 +14,4 @@ jobs: name: Node.js uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 with: - version: '8, 10, 12, 14.17.0, 14, 16, 18' + version: '14.17.0, 14, 16, 18' diff --git a/README.md b/README.md index 65ed0bbe..6301c108 100644 --- a/README.md +++ b/README.md @@ -197,15 +197,16 @@ To load files from directory, and it will be bound the context. // define service in app/service/query.js module.exports = class Query { constructor(ctx) { + super(ctx); // get the ctx } - get() {} + async get() {} }; // use the service in app/controller/home.js -module.exports = function*() { - this.body = this.service.query.get(); +module.exports = async ctx => { + ctx.body = await ctx.service.query.get(); }; ``` diff --git a/index.js b/index.js index a7a21925..2d9cb181 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,3 @@ -'use strict'; - const EggCore = require('./lib/egg'); const EggLoader = require('./lib/loader/egg_loader'); const BaseContextClass = require('./lib/utils/base_context_class'); diff --git a/lib/egg.js b/lib/egg.js index af4709fe..4911773b 100644 --- a/lib/egg.js +++ b/lib/egg.js @@ -1,5 +1,3 @@ -'use strict'; - const assert = require('assert'); const fs = require('fs'); const KoaApplication = require('koa'); @@ -7,12 +5,11 @@ const EggConsoleLogger = require('egg-logger').EggConsoleLogger; const debug = require('debug')('egg-core'); const is = require('is-type-of'); const co = require('co'); +const Router = require('@eggjs/router').EggRouter; const BaseContextClass = require('./utils/base_context_class'); const utils = require('./utils'); -const Router = require('@eggjs/router').EggRouter; const Timing = require('./utils/timing'); const Lifecycle = require('./lifecycle'); -const enableAsyncLocalStorage = !!require('async_hooks').AsyncLocalStorage; const DEPRECATE = Symbol('EggCore#deprecate'); const ROUTER = Symbol('EggCore#router'); @@ -40,7 +37,7 @@ class EggCore extends KoaApplication { // enable asyncLocalStorage by default // https://github.com/koajs/koa/pull/1721 - super({ asyncLocalStorage: enableAsyncLocalStorage }); + super({ asyncLocalStorage: true }); this.timing = new Timing(); @@ -72,7 +69,7 @@ class EggCore extends KoaApplication { /** * Base controller to be extended by controller in `app.controller` * @class Controller - * @extends BaseContextClass + * @augments BaseContextClass * @example * class UserController extends app.Controller {} */ @@ -88,7 +85,7 @@ class EggCore extends KoaApplication { /** * Base service to be extended by services in `app.service` * @class Service - * @extends BaseContextClass + * @augments BaseContextClass * @example * class UserService extends app.Service {} */ diff --git a/lib/loader/context_loader.js b/lib/loader/context_loader.js index 9dff35ed..945d52de 100644 --- a/lib/loader/context_loader.js +++ b/lib/loader/context_loader.js @@ -35,7 +35,7 @@ class ClassLoader { /** * Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`. The exports will be lazy loaded, such as `ctx.group.repository`. - * @extends FileLoader + * @augments FileLoader * @since 1.0.0 */ class ContextLoader extends FileLoader { diff --git a/lib/loader/mixin/config.js b/lib/loader/mixin/config.js index 18e5d340..d63f44db 100644 --- a/lib/loader/mixin/config.js +++ b/lib/loader/mixin/config.js @@ -4,7 +4,6 @@ const debug = require('debug')('egg-core:config'); const path = require('path'); const extend = require('extend2'); const assert = require('assert'); -const { Console } = require('console'); module.exports = { @@ -120,7 +119,7 @@ function setConfig(obj, filepath) { for (const key of Object.keys(obj)) { const val = obj[key]; // ignore console - if (key === 'console' && val && typeof val.Console === 'function' && val.Console === Console) { + if (key === 'console' && val && typeof val.Console === 'function' && val.Console === console.Console) { obj[key] = filepath; continue; } diff --git a/lib/loader/mixin/middleware.js b/lib/loader/mixin/middleware.js index 4b0d84f1..361aeae5 100644 --- a/lib/loader/mixin/middleware.js +++ b/lib/loader/mixin/middleware.js @@ -23,8 +23,8 @@ module.exports = { * // app/middleware/status.js * module.exports = function(options, app) { * // options == app.config.status - * return function*(next) { - * yield next; + * return async next => { + * await next(); * } * } * ``` diff --git a/package.json b/package.json index 812a25b3..20955541 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,8 @@ "scripts": { "lint": "eslint .", "test": "npm run lint -- --fix && npm run test-local", - "test-local": "egg-bin test", - "cov": "egg-bin cov", + "test-local": "egg-bin test -p", + "cov": "egg-bin cov -p", "ci": "npm run lint && npm run cov", "contributor": "git-contributor" }, @@ -32,31 +32,29 @@ }, "homepage": "https://github.com/eggjs/egg-core#readme", "engines": { - "node": ">= 8.9.0" + "node": ">= 14.17.0" }, "devDependencies": { + "@types/depd": "^1.1.32", + "@types/koa": "^2.0.48", "await-event": "^2.1.0", "coffee": "^5.2.1", - "egg-bin": "^4.20.0", + "egg-bin": "^5.9.0", "egg-utils": "^2.4.1", - "eslint": "^5.16.0", - "eslint-config-egg": "^7.4.1", + "eslint": "^8.31.0", + "eslint-config-egg": "^12.1.0", "git-contributor": "^1.0.10", - "jest": "^24.8.0", "js-yaml": "^3.13.1", - "mm": "^2.5.0", - "mz-modules": "^2.1.0", + "mm": "^3.2.1", "pedding": "^1.1.0", - "rimraf": "^2.6.3", "spy": "^1.0.0", "supertest": "^4.0.2", - "ts-node": "^8.0.3", - "typescript": "^3.3.3333" + "ts-node": "^10.9.1", + "typescript": "^4.9.4", + "urllib": "^3.10.0" }, "dependencies": { "@eggjs/router": "^2.0.0", - "@types/depd": "^1.1.32", - "@types/koa": "^2.0.48", "co": "^4.6.0", "debug": "^4.1.1", "depd": "^2.0.0", diff --git a/test/asyncLocalStorage.test.js b/test/asyncLocalStorage.test.js index 87d334f3..23d85c65 100644 --- a/test/asyncLocalStorage.test.js +++ b/test/asyncLocalStorage.test.js @@ -1,9 +1,6 @@ -'use strict'; - const assert = require('assert'); const path = require('path'); const request = require('supertest'); -const enableAsyncLocalStorage = !!require('async_hooks').AsyncLocalStorage; const EggApplication = require('./fixtures/egg').Application; describe('test/asyncLocalStorage.test.js', () => { @@ -23,9 +20,7 @@ describe('test/asyncLocalStorage.test.js', () => { assert(res.status === 200); console.log(res.body); assert(res.body.sessionId === 'mock-session-id-123'); - if (enableAsyncLocalStorage) { - assert(res.body.traceId); - } + assert(res.body.traceId); assert(app.currentContext === undefined); }); }); diff --git a/test/egg-ts.test.js b/test/egg-ts.test.js index 6a82ff68..024bb5e3 100644 --- a/test/egg-ts.test.js +++ b/test/egg-ts.test.js @@ -1,5 +1,3 @@ -'use strict'; - const mm = require('mm'); const request = require('supertest'); const assert = require('assert'); diff --git a/test/egg.test.js b/test/egg.test.js index b5be7c41..a77a8bdb 100644 --- a/test/egg.test.js +++ b/test/egg.test.js @@ -1,18 +1,15 @@ -'use strict'; - const mm = require('mm'); const is = require('is-type-of'); const util = require('util'); const path = require('path'); const assert = require('assert'); const spy = require('spy'); -const sleep = require('mz-modules/sleep'); const request = require('supertest'); const coffee = require('coffee'); const utils = require('./utils'); const EggCore = require('..').EggCore; const awaitEvent = require('await-event'); -const fs = require('mz/fs'); +const fs = require('fs/promises'); describe('test/egg.test.js', () => { afterEach(mm.restore); @@ -323,7 +320,7 @@ describe('test/egg.test.js', () => { const first = spy(); const second = spy(); app = utils.createApp('close'); - app.beforeClose(() => sleep(200)); + app.beforeClose(() => utils.sleep(200)); app.close().then(first); app.close().then(second); setTimeout(() => { @@ -503,7 +500,7 @@ describe('test/egg.test.js', () => { // test/fixtures/egg/node_modules/session/app.js assert(json[12].name.startsWith('Require(6) ')); assert(json[13].name === 'Require(7) app.js'); - assert(json[14].name === 'Before Start in app.js:6:9'); + assert.equal(json[14].name, 'Before Start in app.js:9:7'); assert(json[15].name === 'Before Start in mock Block'); assert(json[16].name === 'readyCallback in mockReadyCallbackWithoutFunction'); @@ -530,13 +527,13 @@ describe('test/egg.test.js', () => { }); describe('agent', () => { - it('should get timing', function* () { + it('should get timing', async () => { app = utils.createApp('timing'); app.loader.loadPlugin(); app.loader.loadConfig(); app.loader.loadApplicationExtend(); app.loader.loadCustomAgent(); - yield app.ready(); + await app.ready(); const json = app.timing.toJSON(); assert(json.length === 14); @@ -560,7 +557,7 @@ describe('test/egg.test.js', () => { // loadCustomAgent assert(json[11].name === 'Load agent.js'); assert(json[12].name === 'Require(6) agent.js'); - assert(json[13].name === 'Before Start in agent.js:5:11'); + assert.equal(json[13].name, 'Before Start in agent.js:8:9'); }); }); @@ -607,7 +604,7 @@ describe('test/egg.test.js', () => { 'willReady', 'ready', ]); - await sleep(10); + await utils.sleep(10); assert.deepStrictEqual( app.bootLog, [ @@ -621,7 +618,7 @@ describe('test/egg.test.js', () => { 'didReady', ]); await app.lifecycle.triggerServerDidReady(); - await sleep(10); + await utils.sleep(10); assert.deepStrictEqual( app.bootLog, [ @@ -679,7 +676,7 @@ describe('test/egg.test.js', () => { 'willReady', 'ready', ]); - await sleep(10); + await utils.sleep(10); assert.deepStrictEqual( app.bootLog, [ @@ -693,7 +690,7 @@ describe('test/egg.test.js', () => { 'didReady', ]); await app.lifecycle.triggerServerDidReady(); - await sleep(10); + await utils.sleep(10); assert.deepStrictEqual( app.bootLog, [ @@ -753,7 +750,7 @@ describe('test/egg.test.js', () => { } assert.strictEqual(error.message, 'didLoad error'); assert.deepStrictEqual(app.bootLog, [ 'configDidLoad' ]); - await sleep(10); + await utils.sleep(10); assert.deepStrictEqual(app.bootLog, [ 'configDidLoad', 'didReady' ]); await app.close(); assert.deepStrictEqual( @@ -778,7 +775,7 @@ describe('test/egg.test.js', () => { } assert.deepStrictEqual(app.bootLog, [ 'configDidLoad', 'didLoad' ]); assert.strictEqual(error.message, 'willReady error'); - await sleep(10); + await utils.sleep(10); assert.deepStrictEqual(app.bootLog, [ 'configDidLoad', 'didLoad', 'didReady' ]); await app.close(); assert.deepStrictEqual( @@ -823,7 +820,7 @@ describe('test/egg.test.js', () => { const app = utils.createApp('boot-serverDidLoad-error'); app.loader.loadAll(); await app.ready(); - await sleep(10); + await utils.sleep(10); assert.deepStrictEqual(app.bootLog, [ 'configDidLoad', 'didLoad', @@ -860,7 +857,7 @@ describe('test/egg.test.js', () => { app.ready(() => { app.bootLog.push('readyFunction'); }); - await sleep(10); + await utils.sleep(10); assert.deepStrictEqual( app.bootLog, [ diff --git a/test/fixtures/beforestart-with-timeout-env/app.js b/test/fixtures/beforestart-with-timeout-env/app.js index 9a8c0648..cf925ab7 100644 --- a/test/fixtures/beforestart-with-timeout-env/app.js +++ b/test/fixtures/beforestart-with-timeout-env/app.js @@ -1,6 +1,4 @@ -'use strict'; - -const sleep = require('ko-sleep'); +const { sleep } = require('../../utils'); module.exports = function (app) { app.beforeStart(function* () { yield sleep(11000); diff --git a/test/fixtures/beforestart/app.js b/test/fixtures/beforestart/app.js index 19c77825..0ac5f788 100644 --- a/test/fixtures/beforestart/app.js +++ b/test/fixtures/beforestart/app.js @@ -1,4 +1,3 @@ -'use strict'; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } @@ -7,7 +6,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments)).next()); }); }; -const sleep = require('ko-sleep'); + +const { sleep } = require('../../utils'); + module.exports = function (app) { app.beforeStart(function() { app.beforeStartFunction = true; diff --git a/test/fixtures/boot-configDidLoad-error/app.js b/test/fixtures/boot-configDidLoad-error/app.js index 18961bc7..e2c38aa0 100644 --- a/test/fixtures/boot-configDidLoad-error/app.js +++ b/test/fixtures/boot-configDidLoad-error/app.js @@ -1,6 +1,4 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../utils'); module.exports = class { constructor(app) { diff --git a/test/fixtures/boot-didLoad-error/app.js b/test/fixtures/boot-didLoad-error/app.js index b15cd482..22c4347d 100644 --- a/test/fixtures/boot-didLoad-error/app.js +++ b/test/fixtures/boot-didLoad-error/app.js @@ -1,6 +1,4 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../utils'); module.exports = class { constructor(app) { diff --git a/test/fixtures/boot-didReady-error/app.js b/test/fixtures/boot-didReady-error/app.js index 581aed87..2c7b3219 100644 --- a/test/fixtures/boot-didReady-error/app.js +++ b/test/fixtures/boot-didReady-error/app.js @@ -1,6 +1,4 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../utils'); module.exports = class { constructor(app) { diff --git a/test/fixtures/boot-serverDidLoad-error/app.js b/test/fixtures/boot-serverDidLoad-error/app.js index 36060d05..4df5be3f 100644 --- a/test/fixtures/boot-serverDidLoad-error/app.js +++ b/test/fixtures/boot-serverDidLoad-error/app.js @@ -1,6 +1,4 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../utils'); module.exports = class { constructor(app) { diff --git a/test/fixtures/boot-timeout/app.js b/test/fixtures/boot-timeout/app.js index 8c875105..e51e01b6 100644 --- a/test/fixtures/boot-timeout/app.js +++ b/test/fixtures/boot-timeout/app.js @@ -1,6 +1,4 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../utils'); module.exports = class TimeoutHook { async didLoad() { diff --git a/test/fixtures/boot-willReady-error/app.js b/test/fixtures/boot-willReady-error/app.js index 8d53ce88..275c538f 100644 --- a/test/fixtures/boot-willReady-error/app.js +++ b/test/fixtures/boot-willReady-error/app.js @@ -1,6 +1,4 @@ -'use strict'; - -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../utils'); module.exports = class { constructor(app) { diff --git a/test/fixtures/boot/agent.js b/test/fixtures/boot/agent.js index 28a33d48..38ebe81b 100644 --- a/test/fixtures/boot/agent.js +++ b/test/fixtures/boot/agent.js @@ -1,6 +1,6 @@ 'use strict'; -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../utils'); module.exports = class { constructor(app) { diff --git a/test/fixtures/boot/app.js b/test/fixtures/boot/app.js index e8f57b1e..7d74439a 100644 --- a/test/fixtures/boot/app.js +++ b/test/fixtures/boot/app.js @@ -1,6 +1,6 @@ 'use strict'; -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../utils'); module.exports = class { constructor(app) { diff --git a/test/fixtures/boot/app/plugin/boot-plugin/agent.js b/test/fixtures/boot/app/plugin/boot-plugin/agent.js index 7f8e0e69..408dcf33 100644 --- a/test/fixtures/boot/app/plugin/boot-plugin/agent.js +++ b/test/fixtures/boot/app/plugin/boot-plugin/agent.js @@ -1,5 +1,4 @@ -'use strict'; -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../../../../../utils'); module.exports = agent => { agent.bootLog.push('agent.js in plugin'); diff --git a/test/fixtures/boot/app/plugin/boot-plugin/app.js b/test/fixtures/boot/app/plugin/boot-plugin/app.js index 6cd249a6..bd5fa423 100644 --- a/test/fixtures/boot/app/plugin/boot-plugin/app.js +++ b/test/fixtures/boot/app/plugin/boot-plugin/app.js @@ -1,6 +1,5 @@ -'use strict'; -const sleep = require('mz-modules/sleep'); const assert = require('assert'); +const { sleep } = require('../../../../../utils'); module.exports = app => { app.bootLog.push('app.js in plugin'); diff --git a/test/fixtures/configmeta/config/config.js b/test/fixtures/configmeta/config/config.js index b28f84da..95501929 100644 --- a/test/fixtures/configmeta/config/config.js +++ b/test/fixtures/configmeta/config/config.js @@ -1,7 +1,5 @@ -'use strict'; - -const HttpClient2 = require('urllib').HttpClient2; -const urllib = new HttpClient2(); +const HttpClient = require('urllib').HttpClient; +const urllib = new HttpClient(); exports.urllib = { keepAlive: false, @@ -12,7 +10,7 @@ exports.urllib = { httpclient: urllib, }; -exports.buffer = new Buffer('1234'); +exports.buffer = Buffer.from('1234'); exports.array = []; exports.console = console; diff --git a/test/fixtures/egg-jest/__tests__/index.js b/test/fixtures/egg-jest/__tests__/index.js deleted file mode 100644 index 5e40fcb8..00000000 --- a/test/fixtures/egg-jest/__tests__/index.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -const path = require('path'); -const assert = require('assert'); -const EggApplication = require('../').Application; - -test('should works', async () => { - const app = new EggApplication({ - baseDir: path.resolve(__dirname, '../'), - type: 'application', - }); - app.loader.loadAll(); - expect(!!app.Proxy).toBe(true); - expect(!!app.config.urllib.keepAlive).toBe(true); -}); diff --git a/test/fixtures/egg-jest/app/extend/application.js b/test/fixtures/egg-jest/app/extend/application.js deleted file mode 100644 index 341b1984..00000000 --- a/test/fixtures/egg-jest/app/extend/application.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -module.exports = { - get Proxy() { - return this.BaseContextClass; - }, -}; diff --git a/test/fixtures/egg-jest/app/middleware/status.js b/test/fixtures/egg-jest/app/middleware/status.js deleted file mode 100644 index b80c8ee9..00000000 --- a/test/fixtures/egg-jest/app/middleware/status.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -module.exports = function() { - return (ctx, next) => { - if (ctx.path === '/status') { - ctx.body = 'egg status'; - return; - } - - return next(); - }; -}; diff --git a/test/fixtures/egg-jest/config/config.default.js b/test/fixtures/egg-jest/config/config.default.js deleted file mode 100644 index 56ad861c..00000000 --- a/test/fixtures/egg-jest/config/config.default.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; - -module.exports = { - urllib: { - keepAlive: true, - keepAliveTimeout: 30000, - timeout: 30000, - maxSockets: Infinity, - maxFreeSockets: 256, - }, - - egg: 'egg', -}; diff --git a/test/fixtures/egg-jest/config/config.unittest.js b/test/fixtures/egg-jest/config/config.unittest.js deleted file mode 100644 index 0db03418..00000000 --- a/test/fixtures/egg-jest/config/config.unittest.js +++ /dev/null @@ -1,5 +0,0 @@ -'use strict'; - -module.exports = { - egg: 'egg-unittest', -}; diff --git a/test/fixtures/egg-jest/config/plugin.js b/test/fixtures/egg-jest/config/plugin.js deleted file mode 100644 index 234c39a5..00000000 --- a/test/fixtures/egg-jest/config/plugin.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -const path = require('path'); - -module.exports = { - session: { - enable: true, - path: path.join(__dirname, '../node_modules/session'), - }, - - hsfclient: { - enable: false, - path: path.join(__dirname, '../plugins/hsfclient'), - }, -}; diff --git a/test/fixtures/egg-jest/index.js b/test/fixtures/egg-jest/index.js deleted file mode 100644 index 1633da23..00000000 --- a/test/fixtures/egg-jest/index.js +++ /dev/null @@ -1,42 +0,0 @@ -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const rimraf = require('mz-modules/rimraf'); -const EggCore = require('../../../').EggCore; -const EggLoader = require('../../../').EggLoader; - -class AppLoader extends EggLoader { - loadAll() { - this.loadPlugin(); - this.loadConfig(); - this.loadApplicationExtend(); - this.loadContextExtend(); - this.loadRequestExtend(); - this.loadResponseExtend(); - this.loadCustomApp(); - this.loadMiddleware(); - this.loadService(); - this.loadController(); - this.loadRouter(); - } -} - -class EggApplication extends EggCore { - - constructor(options) { - super(options); - this.on('error', err => { - console.error(err); - }) - } - - get [Symbol.for('egg#eggPath')]() { - return __dirname; - } - get [Symbol.for('egg#loader')]() { - return AppLoader; - } -} - -module.exports.Application = EggApplication; diff --git a/test/fixtures/egg-jest/package.json b/test/fixtures/egg-jest/package.json deleted file mode 100644 index 6697ad3f..00000000 --- a/test/fixtures/egg-jest/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "name": "egg" -} diff --git a/test/fixtures/egg-jest/plugins/hsfclient/package.json b/test/fixtures/egg-jest/plugins/hsfclient/package.json deleted file mode 100644 index bab3f80b..00000000 --- a/test/fixtures/egg-jest/plugins/hsfclient/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "eggPlugin": { - "name": "hsfclient" - } -} diff --git a/test/fixtures/egg/index.js b/test/fixtures/egg/index.js index 81f6f435..44655ce4 100644 --- a/test/fixtures/egg/index.js +++ b/test/fixtures/egg/index.js @@ -1,11 +1,8 @@ -'use strict'; - const fs = require('fs'); const path = require('path'); -const rimraf = require('mz-modules/rimraf'); const eggPath = path.join(__dirname, 'node_modules/egg-core'); -rimraf.sync(eggPath); +fs.rmSync(eggPath, { force: true, recursive: true }); fs.symlinkSync( path.join(__dirname, '../../..'), eggPath, diff --git a/test/fixtures/egg-jest/node_modules/session/package.json b/test/fixtures/scope-env/node_modules/d/package.json similarity index 53% rename from test/fixtures/egg-jest/node_modules/session/package.json rename to test/fixtures/scope-env/node_modules/d/package.json index ba0ae57d..c95d527f 100644 --- a/test/fixtures/egg-jest/node_modules/session/package.json +++ b/test/fixtures/scope-env/node_modules/d/package.json @@ -1,5 +1,5 @@ { "eggPlugin": { - "name": "session" + "name": "d" } } diff --git a/test/index.test.js b/test/index.test.js index 6c9428b0..6a9a143b 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,5 +1,3 @@ -'use strict'; - const assert = require('assert'); const EggCore = require('..'); diff --git a/test/jest.test.js b/test/jest.test.js deleted file mode 100644 index 4fc6ed21..00000000 --- a/test/jest.test.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -const path = require('path'); -const assert = require('assert'); -const coffee = require('coffee'); - -describe('test/jest.test.js', () => { - it('should works without error with jest', async () => { - const { stdout, stderr } = await coffee - .fork(require.resolve('jest/bin/jest'), [], { - cwd: path.resolve(__dirname, './fixtures/egg-jest'), - }) - // .debug() - .end(); - - assert((stdout + stderr).includes('Test Suites: 1 passed, 1 total')); - }); -}); diff --git a/test/lifecycle.test.js b/test/lifecycle.test.js index 1db839d7..05b13618 100644 --- a/test/lifecycle.test.js +++ b/test/lifecycle.test.js @@ -1,9 +1,7 @@ -'use strict'; - const assert = require('assert'); - const Lifecycle = require('../lib/lifecycle.js'); const EggCore = require('..').EggCore; + describe('test/lifecycle.js', () => { it('should forbid adding hook atfter initialization', () => { const lifecycle = new Lifecycle({ diff --git a/test/loader/context_loader.test.js b/test/loader/context_loader.test.js index a0c8f78b..7e9e6602 100644 --- a/test/loader/context_loader.test.js +++ b/test/loader/context_loader.test.js @@ -1,5 +1,3 @@ -'use strict'; - const request = require('supertest'); const path = require('path'); const utils = require('../utils'); diff --git a/test/loader/egg_loader.test.js b/test/loader/egg_loader.test.js index fd99996e..7da8a8b5 100644 --- a/test/loader/egg_loader.test.js +++ b/test/loader/egg_loader.test.js @@ -1,5 +1,3 @@ -'use strict'; - const assert = require('assert'); const os = require('os'); const mm = require('mm'); diff --git a/test/loader/file_loader.test.js b/test/loader/file_loader.test.js index 47bfe89d..475d327c 100644 --- a/test/loader/file_loader.test.js +++ b/test/loader/file_loader.test.js @@ -1,5 +1,3 @@ -'use strict'; - const assert = require('assert'); const pedding = require('pedding'); const path = require('path'); @@ -215,7 +213,7 @@ describe('test/loader/file_loader.test.js', () => { directory: path.join(dirBase, 'syntax_error'), target: app.model, }).load(); - }, /Parse Error:/); + }, /error: Unexpected identifier/); }); it('should throw when directory contains dot', () => { diff --git a/test/loader/get_app_info.test.js b/test/loader/get_app_info.test.js index bf6e6c71..1f80aac0 100644 --- a/test/loader/get_app_info.test.js +++ b/test/loader/get_app_info.test.js @@ -1,5 +1,3 @@ -'use strict'; - const path = require('path'); const mm = require('mm'); const assert = require('assert'); diff --git a/test/loader/get_appname.test.js b/test/loader/get_appname.test.js index f9907743..d345bc11 100644 --- a/test/loader/get_appname.test.js +++ b/test/loader/get_appname.test.js @@ -1,5 +1,3 @@ -'use strict'; - const mm = require('mm'); const assert = require('assert'); const utils = require('../utils'); diff --git a/test/loader/get_framework_paths.test.js b/test/loader/get_framework_paths.test.js index 0e97c077..35da4cbc 100644 --- a/test/loader/get_framework_paths.test.js +++ b/test/loader/get_framework_paths.test.js @@ -1,5 +1,3 @@ -'use strict'; - const mm = require('mm'); const assert = require('assert'); const utils = require('../utils'); @@ -19,17 +17,17 @@ class Application { close() {} } -describe('test/loader/get_framework_paths.test.js', function() { +describe('test/loader/get_framework_paths.test.js', () => { let app; afterEach(mm.restore); afterEach(() => app && app.close()); - it('should get from paramter', function() { + it('should get from paramter', () => { app = utils.createApp('eggpath'); assert.deepEqual(app.loader.eggPaths, [ utils.getFilepath('egg') ]); }); - it('should get from framework using symbol', function() { + it('should get from framework using symbol', () => { app = utils.createApp('eggpath', { Application: require(utils.getFilepath('framework-symbol')), }); diff --git a/test/loader/get_load_units.test.js b/test/loader/get_load_units.test.js index fc13cf51..ab52337f 100644 --- a/test/loader/get_load_units.test.js +++ b/test/loader/get_load_units.test.js @@ -1,15 +1,13 @@ -'use strict'; - const mm = require('mm'); const assert = require('assert'); const utils = require('../utils'); -describe('test/get_load_units.test.js', function() { +describe('test/get_load_units.test.js', () => { let app; afterEach(mm.restore); afterEach(() => app.close()); - it('should get plugin dir', function() { + it('should get plugin dir', () => { app = utils.createApp('plugin'); app.loader.loadPlugin(); // delete cache @@ -22,7 +20,7 @@ describe('test/get_load_units.test.js', function() { assert(units[11].path === utils.getFilepath('plugin')); }); - it('should not get plugin dir', function() { + it('should not get plugin dir', () => { app = utils.createApp('plugin'); const units = app.loader.getLoadUnits(); assert(units.length === 2); diff --git a/test/loader/get_server_env.test.js b/test/loader/get_server_env.test.js index fcda9ac5..21e859b8 100644 --- a/test/loader/get_server_env.test.js +++ b/test/loader/get_server_env.test.js @@ -1,55 +1,53 @@ -'use strict'; - const mm = require('mm'); const assert = require('assert'); const utils = require('../utils'); -describe('test/loader/get_server_env.test.js', function() { +describe('test/loader/get_server_env.test.js', () => { let app; afterEach(mm.restore); afterEach(() => app.close()); - it('should get from env EGG_SERVER_ENV', function() { + it('should get from env EGG_SERVER_ENV', () => { mm(process.env, 'EGG_SERVER_ENV', 'prod'); app = utils.createApp('serverenv'); assert(app.loader.serverEnv === 'prod'); }); - it('should use test when EGG_SERVER_ENV = "test "', function() { + it('should use test when EGG_SERVER_ENV = "test "', () => { mm(process.env, 'EGG_SERVER_ENV', 'test '); app = utils.createApp('serverenv'); assert(app.loader.serverEnv === 'test'); }); - it('should use unittest when NODE_ENV = test', function() { + it('should use unittest when NODE_ENV = test', () => { mm(process.env, 'NODE_ENV', 'test'); app = utils.createApp('serverenv'); assert(app.loader.serverEnv === 'unittest'); }); - it('should use prod when NODE_ENV = production', function() { + it('should use prod when NODE_ENV = production', () => { mm(process.env, 'NODE_ENV', 'production'); app = utils.createApp('serverenv'); assert(app.loader.serverEnv === 'prod'); }); - it('should use local when NODE_ENV is other', function() { + it('should use local when NODE_ENV is other', () => { mm(process.env, 'NODE_ENV', 'development'); app = utils.createApp('serverenv'); assert(app.loader.serverEnv === 'local'); }); - it('should get from config/env', function() { + it('should get from config/env', () => { mm(process.env, 'NODE_ENV', 'production'); mm(process.env, 'EGG_SERVER_ENV', 'test'); app = utils.createApp('serverenv-file'); assert(app.loader.serverEnv === 'prod'); }); - it('should get from options.env', function() { + it('should get from options.env', () => { app = utils.createApp('serverenv', { env: 'prod' }); assert(app.loader.serverEnv === 'prod'); }); - it('should use options.env first', function() { + it('should use options.env first', () => { mm(process.env, 'EGG_SERVER_ENV', 'test'); app = utils.createApp('serverenv-file', { env: 'development' }); assert(app.loader.serverEnv === 'development'); diff --git a/test/loader/load_file.test.js b/test/loader/load_file.test.js index 2d2a740e..ee6084ea 100644 --- a/test/loader/load_file.test.js +++ b/test/loader/load_file.test.js @@ -1,10 +1,8 @@ -'use strict'; - const mm = require('mm'); const assert = require('assert'); const utils = require('../utils'); -describe('test/load_file.test.js', () => { +describe('test/loader/load_file.test.js', () => { let app; afterEach(mm.restore); afterEach(() => app.close()); @@ -25,7 +23,7 @@ describe('test/load_file.test.js', () => { assert.throws(() => { app = utils.createApp('syntaxerror'); app.loader.loadCustomApp(); - }, /Parse Error: Unexpected token/); + }, /error: Unexpected end of input/); }); it('should load custom file', () => { diff --git a/test/loader/mixin/load_agent_extend.test.js b/test/loader/mixin/load_agent_extend.test.js index c9214078..8788ceba 100644 --- a/test/loader/mixin/load_agent_extend.test.js +++ b/test/loader/mixin/load_agent_extend.test.js @@ -1,11 +1,9 @@ -'use strict'; - const assert = require('assert'); const utils = require('../../utils'); -describe('test/loader/mixin/load_agent_extend.test.js', function() { +describe('test/loader/mixin/load_agent_extend.test.js', () => { let agent; - before(function() { + before(() => { agent = utils.createApp('agent'); agent.loader.loadPlugin(); agent.loader.loadConfig(); @@ -13,7 +11,7 @@ describe('test/loader/mixin/load_agent_extend.test.js', function() { }); after(() => agent.close()); - it('should load extend from chair, plugin and agent', function() { + it('should load extend from chair, plugin and agent', () => { assert(agent.poweredBy); assert(agent.a); assert(agent.b); @@ -21,13 +19,13 @@ describe('test/loader/mixin/load_agent_extend.test.js', function() { assert(agent.bar); }); - it('should override chair by plugin', function() { + it('should override chair by plugin', () => { assert(agent.a === 'plugin a'); assert(agent.b === 'plugin b'); assert(agent.poweredBy === 'plugin a'); }); - it('should override plugin by agent', function() { + it('should override plugin by agent', () => { assert(agent.foo === 'agent bar'); assert(agent.bar === 'foo'); }); diff --git a/test/loader/mixin/load_application_extend.test.js b/test/loader/mixin/load_application_extend.test.js index 78c2998f..6ab943b3 100644 --- a/test/loader/mixin/load_application_extend.test.js +++ b/test/loader/mixin/load_application_extend.test.js @@ -1,11 +1,9 @@ -'use strict'; - const assert = require('assert'); const utils = require('../../utils'); -describe('test/loader/mixin/load_application_extend.test.js', function() { +describe('test/loader/mixin/load_application_extend.test.js', () => { let app; - before(function() { + before(() => { app = utils.createApp('application'); app.loader.loadPlugin(); app.loader.loadConfig(); @@ -13,7 +11,7 @@ describe('test/loader/mixin/load_application_extend.test.js', function() { }); after(() => app.close()); - it('should load extend from chair, plugin and application', function() { + it('should load extend from chair, plugin and application', () => { assert(app.poweredBy); assert(app.a); assert(app.b); @@ -21,13 +19,13 @@ describe('test/loader/mixin/load_application_extend.test.js', function() { assert(app.bar); }); - it('should override chair by plugin', function() { + it('should override chair by plugin', () => { assert(app.a === 'plugin a'); assert(app.b === 'plugin b'); assert(app.poweredBy === 'plugin a'); }); - it('should override plugin by app', function() { + it('should override plugin by app', () => { assert(app.foo === 'app bar'); assert(app.bar === 'foo'); }); diff --git a/test/loader/mixin/load_config.test.js b/test/loader/mixin/load_config.test.js index d26d7da1..821fe1f9 100644 --- a/test/loader/mixin/load_config.test.js +++ b/test/loader/mixin/load_config.test.js @@ -1,5 +1,3 @@ -'use strict'; - const path = require('path'); const assert = require('assert'); const mm = require('mm'); @@ -177,7 +175,7 @@ describe('test/loader/mixin/load_config.test.js', () => { }); describe('get config with scope', () => { - it('should return without scope when env = default', async () => { + it('should return without scope when env = default', () => { mm(process.env, 'EGG_SERVER_ENV', 'default'); app = utils.createApp('scope-env'); const loader = app.loader; @@ -186,7 +184,7 @@ describe('test/loader/mixin/load_config.test.js', () => { assert(loader.config.from === 'default'); }); - it('should return without scope when env = prod', async () => { + it('should return without scope when env = prod', () => { mm(process.env, 'EGG_SERVER_ENV', 'prod'); app = utils.createApp('scope-env'); const loader = app.loader; @@ -195,7 +193,7 @@ describe('test/loader/mixin/load_config.test.js', () => { assert(loader.config.from === 'prod'); }); - it('should return with scope when env = default', async () => { + it('should return with scope when env = default', () => { mm(process.env, 'EGG_SERVER_ENV', 'default'); mm(process.env, 'EGG_SERVER_SCOPE', 'en'); app = utils.createApp('scope-env'); @@ -205,7 +203,7 @@ describe('test/loader/mixin/load_config.test.js', () => { assert(loader.config.from === 'en'); }); - it('should return with scope when env = prod', async () => { + it('should return with scope when env = prod', () => { mm(process.env, 'EGG_SERVER_ENV', 'prod'); mm(process.env, 'EGG_SERVER_SCOPE', 'en'); app = utils.createApp('scope-env'); diff --git a/test/loader/mixin/load_controller.test.js b/test/loader/mixin/load_controller.test.js index e97f0aa4..7ae38e06 100644 --- a/test/loader/mixin/load_controller.test.js +++ b/test/loader/mixin/load_controller.test.js @@ -1,5 +1,3 @@ -'use strict'; - const is = require('is-type-of'); const assert = require('assert'); const request = require('supertest'); diff --git a/test/loader/mixin/load_custom_agent.test.js b/test/loader/mixin/load_custom_agent.test.js index 9ecf88c3..f35d1907 100644 --- a/test/loader/mixin/load_custom_agent.test.js +++ b/test/loader/mixin/load_custom_agent.test.js @@ -1,11 +1,9 @@ -'use strict'; - const assert = require('assert'); const utils = require('../../utils'); -describe('test/loader/mixin/load_custom_agent.test.js', function() { +describe('test/loader/mixin/load_custom_agent.test.js', () => { let agent; - before(function() { + before(() => { agent = utils.createApp('plugin'); agent.loader.loadPlugin(); agent.loader.loadConfig(); @@ -13,18 +11,18 @@ describe('test/loader/mixin/load_custom_agent.test.js', function() { }); after(() => agent.close()); - it('should load agent.js', function() { + it('should load agent.js', () => { assert(agent.b === 'plugin b'); assert(agent.c === 'plugin c'); assert(agent.agent === 'agent'); }); - it('should agent.js of plugin before application\'s', function() { + it('should agent.js of plugin before application\'s', () => { assert(agent.dateB <= agent.date); assert(agent.dateC <= agent.date); }); - it('should not load plugin that is disabled', function() { + it('should not load plugin that is disabled', () => { assert(!agent.a); }); }); diff --git a/test/loader/mixin/load_custom_loader.test.js b/test/loader/mixin/load_custom_loader.test.js index 7ee468ba..0e021f7c 100644 --- a/test/loader/mixin/load_custom_loader.test.js +++ b/test/loader/mixin/load_custom_loader.test.js @@ -1,12 +1,10 @@ -'use strict'; - const assert = require('assert'); const request = require('supertest'); const utils = require('../../utils'); -describe('test/loader/mixin/load_custom_loader.test.js', function() { +describe('test/loader/mixin/load_custom_loader.test.js', () => { let app; - before(function() { + before(() => { app = utils.createApp('custom-loader'); app.loader.loadPlugin(); app.loader.loadConfig(); @@ -40,7 +38,7 @@ describe('test/loader/mixin/load_custom_loader.test.js', function() { .expect(200); }); - it('should support loadunit', async () => { + it('should support loadunit', () => { let name = app.plugin.a.getName(); assert(name === 'plugina'); name = app.plugin.b.getName(); diff --git a/test/loader/mixin/load_extend.test.js b/test/loader/mixin/load_extend.test.js index 72dfa4ce..6f76c82e 100644 --- a/test/loader/mixin/load_extend.test.js +++ b/test/loader/mixin/load_extend.test.js @@ -1,5 +1,3 @@ -'use strict'; - const request = require('supertest'); const mm = require('mm'); const assert = require('assert'); @@ -7,7 +5,7 @@ const utils = require('../../utils'); describe('test/loader/mixin/load_extend.test.js', () => { let app; - before(function() { + before(() => { app = utils.createApp('extend'); app.loader.loadPlugin(); app.loader.loadConfig(); @@ -79,27 +77,27 @@ describe('test/loader/mixin/load_extend.test.js', () => { .expect(200); }); - it('should throw when no deps', function() { + it('should throw when no deps', () => { assert.throws(() => { const app = utils.createApp('load_context_error'); app.loader.loadContextExtend(); }, /Cannot find module 'this is a pen'/); }); - it('should throw when syntax error', function() { + it('should throw when syntax error', () => { assert.throws(() => { const app = utils.createApp('load_context_syntax_error'); app.loader.loadContextExtend(); - }, /Parse Error: Unexpected token/); + }, /error: Unexpected end of input/); }); - it('should extend symbol', function() { + it('should extend symbol', () => { const app = utils.createApp('extend-symbol'); app.loader.loadApplicationExtend(); assert.equal(app[utils.symbol.view], 'view'); }); - it('should load application by custom env', function() { + it('should load application by custom env', () => { mm(process.env, 'EGG_SERVER_ENV', 'custom'); const app = utils.createApp('extend-env'); app.loader.loadPlugin(); @@ -111,7 +109,7 @@ describe('test/loader/mixin/load_extend.test.js', () => { assert(app.b === 'b1'); }); - it('should not load extend that returned function', function() { + it('should not load extend that returned function', () => { const proto = {}; app.loader.loadExtend('call', proto); assert(proto.call === undefined); @@ -121,7 +119,7 @@ describe('test/loader/mixin/load_extend.test.js', () => { let app; after(() => app.close()); - it('should load unittext.js when unittest', async () => { + it('should load unittext.js when unittest', () => { app = utils.createApp('load-plugin-unittest'); app.loader.loadPlugin(); app.loader.loadApplicationExtend(); @@ -129,7 +127,7 @@ describe('test/loader/mixin/load_extend.test.js', () => { assert(app.local !== true); }); - it('should load unittext.js when mm.env(default)', async () => { + it('should load unittext.js when mm.env(default)', () => { mm(process.env, 'EGG_SERVER_ENV', 'local'); mm(process.env, 'EGG_MOCK_SERVER_ENV', 'local'); app = utils.createApp('load-plugin-unittest'); diff --git a/test/loader/mixin/load_helper_extend.test.js b/test/loader/mixin/load_helper_extend.test.js index 02fbf1f4..8110ef6b 100644 --- a/test/loader/mixin/load_helper_extend.test.js +++ b/test/loader/mixin/load_helper_extend.test.js @@ -1,12 +1,10 @@ -'use strict'; - const request = require('supertest'); const utils = require('../../utils'); -describe('test/loader/mixin/load_helper_extend.test.js', function() { +describe('test/loader/mixin/load_helper_extend.test.js', () => { describe('helper', () => { let app; - before(function() { + before(() => { app = utils.createApp('helper'); app.loader.loadPlugin(); app.loader.loadConfig(); diff --git a/test/loader/mixin/load_middleware.test.js b/test/loader/mixin/load_middleware.test.js index 8b650aa1..2c5bd1b6 100644 --- a/test/loader/mixin/load_middleware.test.js +++ b/test/loader/mixin/load_middleware.test.js @@ -1,14 +1,11 @@ -'use strict'; - const path = require('path'); const assert = require('assert'); const request = require('supertest'); -const enableAsyncLocalStorage = !!require('async_hooks').AsyncLocalStorage; const utils = require('../../utils'); -describe('test/loader/mixin/load_middleware.test.js', function() { +describe('test/loader/mixin/load_middleware.test.js', () => { let app; - before(function() { + before(() => { app = utils.createApp('middleware-override'); app.loader.loadPlugin(); app.loader.loadConfig(); @@ -19,7 +16,7 @@ describe('test/loader/mixin/load_middleware.test.js', function() { }); after(() => app.close()); - it('should load application, plugin, and default middlewares', function() { + it('should load application, plugin, and default middlewares', () => { assert('static' in app.middlewares); assert('status' in app.middlewares); assert('custom' in app.middlewares); @@ -27,7 +24,7 @@ describe('test/loader/mixin/load_middleware.test.js', function() { assert(!('a' in app.middlewares)); }); - it('should also support app.middleware', function() { + it('should also support app.middleware', () => { assert('static' in app.middleware); assert('status' in app.middleware); assert('custom' in app.middleware); @@ -38,12 +35,7 @@ describe('test/loader/mixin/load_middleware.test.js', function() { for (const mw of app.middleware) { assert(typeof mw === 'function'); } - if (enableAsyncLocalStorage) { - // the first middleware is asyncCtxStorage - assert(Object.keys(app.middleware).length === 4); - } else { - assert(Object.keys(app.middleware).length === 3); - } + assert(Object.keys(app.middleware).length === 4); }); it('should override middlewares of plugin by framework', async () => { @@ -64,7 +56,7 @@ describe('test/loader/mixin/load_middleware.test.js', function() { .expect('static'); }); - it('should throw when middleware return no-generator', function() { + it('should throw when middleware return no-generator', () => { const app = utils.createApp('custom_session_invaild'); assert.throws(() => { app.loader.loadPlugin(); @@ -74,7 +66,7 @@ describe('test/loader/mixin/load_middleware.test.js', function() { }, /Middleware session must be a function, but actual is {}/); }); - it('should throw when not load that is not configured', function() { + it('should throw when not load that is not configured', () => { const app = utils.createApp('no-middleware'); assert.throws(() => { app.loader.loadPlugin(); @@ -84,7 +76,7 @@ describe('test/loader/mixin/load_middleware.test.js', function() { }, /Middleware a not found/); }); - it('should throw when middleware name redefined', function() { + it('should throw when middleware name redefined', () => { const app = utils.createApp('middleware-redefined'); assert.throws(() => { app.loader.loadPlugin(); diff --git a/test/loader/mixin/load_plugin.test.js b/test/loader/mixin/load_plugin.test.js index 78e4cff0..fc4c696c 100644 --- a/test/loader/mixin/load_plugin.test.js +++ b/test/loader/mixin/load_plugin.test.js @@ -1,18 +1,14 @@ -'use strict'; - const path = require('path'); const fs = require('fs'); const mm = require('mm'); const assert = require('assert'); -const rimraf = require('rimraf'); const spy = require('spy'); const pedding = require('pedding'); const utils = require('../../utils'); const EggCore = require('../../..').EggCore; const EggLoader = require('../../..').EggLoader; - -describe('test/load_plugin.test.js', function() { +describe('test/loader/mixin/load_plugin.test.js', () => { let app; afterEach(() => { @@ -31,7 +27,7 @@ describe('test/load_plugin.test.js', function() { assert('eggPlugins' in loader); }); - it('should loadConfig all plugins', function() { + it('should loadConfig all plugins', () => { const baseDir = utils.getFilepath('plugin'); app = utils.createApp('plugin'); const loader = app.loader; @@ -67,7 +63,7 @@ describe('test/load_plugin.test.js', function() { assert(loader.orderPlugins instanceof Array); }); - it('should loadPlugin with order', function() { + it('should loadPlugin with order', () => { app = utils.createApp('plugin'); const loader = app.loader; const loaderOrders = []; @@ -90,7 +86,7 @@ describe('test/load_plugin.test.js', function() { ]); }); - it('should follow the search order,node_modules of application > node_modules of framework', function() { + it('should follow the search order,node_modules of application > node_modules of framework', () => { const baseDir = utils.getFilepath('plugin'); app = utils.createApp('plugin'); const loader = app.loader; @@ -153,7 +149,7 @@ describe('test/load_plugin.test.js', function() { assert(loader.config.b === 'b'); }); - it('should support alias', function() { + it('should support alias', () => { const baseDir = utils.getFilepath('plugin'); app = utils.createApp('plugin'); const loader = app.loader; @@ -173,7 +169,7 @@ describe('test/load_plugin.test.js', function() { assert(!loader.plugins.d); }); - it('should support config in package.json', function() { + it('should support config in package.json', () => { const baseDir = utils.getFilepath('plugin'); app = utils.createApp('plugin'); const loader = app.loader; @@ -192,7 +188,7 @@ describe('test/load_plugin.test.js', function() { }); }); - it('should warn when the name of plugin is not same', function() { + it('should warn when the name of plugin is not same', () => { let message; app = utils.createApp('plugin'); mm(app.console, 'warn', function(m) { @@ -207,7 +203,7 @@ describe('test/load_plugin.test.js', function() { assert(message === '[egg:loader] pluginName(e) is different from pluginConfigName(wrong-name)'); }); - it('should not warn when the config.strict is false', function() { + it('should not warn when the config.strict is false', () => { let message; app = utils.createApp('plugin-strict'); mm(app.console, 'warn', function(m) { @@ -218,7 +214,7 @@ describe('test/load_plugin.test.js', function() { assert(!message); }); - it('should loadConfig plugins with custom plugins config', function() { + it('should loadConfig plugins with custom plugins config', () => { const baseDir = utils.getFilepath('plugin'); const plugins = { foo: { @@ -255,7 +251,7 @@ describe('test/load_plugin.test.js', function() { assert(!loader.plugins.d); }); - it('should custom plugins with EGG_PLUGINS', function() { + it('should custom plugins with EGG_PLUGINS', () => { const baseDir = utils.getFilepath('plugin'); const plugins = { b: false, @@ -275,7 +271,7 @@ describe('test/load_plugin.test.js', function() { assert(loader.allPlugins.h.path === path.join(baseDir, 'node_modules/h')); }); - it('should ignore when EGG_PLUGINS parse error', function() { + it('should ignore when EGG_PLUGINS parse error', () => { mm(process.env, 'EGG_PLUGINS', '{h:1}'); app = utils.createApp('plugin'); const loader = app.loader; @@ -284,7 +280,7 @@ describe('test/load_plugin.test.js', function() { assert(!loader.allPlugins.h); }); - it('should validate plugin.package', function() { + it('should validate plugin.package', () => { assert.throws(() => { app = utils.createApp('plugin', { plugins: { foo: { package: '../' }, bar: { package: 'c:\\' } } }); const loader = app.loader; @@ -307,7 +303,7 @@ describe('test/load_plugin.test.js', function() { }, /plugin foo invalid, use 'path' instead of package/); }); - it('should throw when plugin not exist', function() { + it('should throw when plugin not exist', () => { assert.throws(() => { app = utils.createApp('plugin-noexist'); const loader = app.loader; @@ -316,7 +312,7 @@ describe('test/load_plugin.test.js', function() { }, /Can not find plugin noexist in /); }); - it('should throw when the dependent plugin is disabled', function() { + it('should throw when the dependent plugin is disabled', () => { assert.throws(() => { app = utils.createApp('no-dep-plugin'); const loader = app.loader; @@ -325,7 +321,7 @@ describe('test/load_plugin.test.js', function() { }, /Can not find plugin @ali\/b in /); }); - it('should make order', function() { + it('should make order', () => { mm(process.env, 'NODE_ENV', 'development'); app = utils.createApp('plugin-dep'); const loader = app.loader; @@ -346,7 +342,7 @@ describe('test/load_plugin.test.js', function() { ]); }); - it('should throw when plugin is recursive', function() { + it('should throw when plugin is recursive', () => { assert.throws(() => { app = utils.createApp('plugin-dep-recursive'); const loader = app.loader; @@ -355,7 +351,7 @@ describe('test/load_plugin.test.js', function() { }, /sequencify plugins has problem, missing: \[], recursive: \[a,b,c,a]/); }); - it('should throw when the dependent plugin not exist', function() { + it('should throw when the dependent plugin not exist', () => { assert.throws(() => { app = utils.createApp('plugin-dep-missing'); const loader = app.loader; @@ -419,7 +415,7 @@ describe('test/load_plugin.test.js', function() { assert(!loader.plugins.e); }); - it('should enable when not match env', function() { + it('should enable when not match env', () => { app = utils.createApp('dont-load-plugin'); const loader = app.loader; loader.loadPlugin(); @@ -431,7 +427,7 @@ describe('test/load_plugin.test.js', function() { assert(!plugins.includes('testMe')); }); - it('should complement infomation by config/plugin.js from plugin', function() { + it('should complement infomation by config/plugin.js from plugin', () => { const baseDir = utils.getFilepath('plugin'); mm(process.env, 'NODE_ENV', 'test'); @@ -467,7 +463,7 @@ describe('test/load_plugin.test.js', function() { }); }); - it('should load when all plugins are disabled', function() { + it('should load when all plugins are disabled', () => { app = utils.createApp('noplugin'); const loader = app.loader; loader.loadPlugin(); @@ -475,7 +471,7 @@ describe('test/load_plugin.test.js', function() { assert(loader.orderPlugins.length === 0); }); - it('should throw when the dependent plugin is disabled', function() { + it('should throw when the dependent plugin is disabled', () => { assert.throws(() => { mm(process.env, 'EGG_SERVER_ENV', 'prod'); app = utils.createApp('env-disable'); @@ -485,7 +481,7 @@ describe('test/load_plugin.test.js', function() { }, /sequencify plugins has problem, missing: \[b], recursive: \[]\n\t>> Plugin \[b] is disabled or missed, but is required by \[a]/); }); - it('should pick path or package when override config', function() { + it('should pick path or package when override config', () => { app = utils.createApp('plugin-path-package'); const loader = app.loader; loader.loadPlugin(); @@ -497,7 +493,7 @@ describe('test/load_plugin.test.js', function() { }); it('should resolve the realpath of plugin path', () => { - rimraf.sync(utils.getFilepath('realpath/node_modules/a')); + fs.rmSync(utils.getFilepath('realpath/node_modules/a'), { force: true, recursive: true }); fs.symlinkSync('../a', utils.getFilepath('realpath/node_modules/a'), 'dir'); app = utils.createApp('realpath'); const loader = app.loader; @@ -525,7 +521,7 @@ describe('test/load_plugin.test.js', function() { assert(loader.plugins.b.from === utils.getFilepath('plugin-from/framework/config/plugin.js')); }); - it('should load plugin.unittest.js override default', function() { + it('should load plugin.unittest.js override default', () => { mm(process.env, 'EGG_SERVER_ENV', 'unittest'); app = utils.createApp('load-plugin-by-env'); const loader = app.loader; @@ -534,7 +530,7 @@ describe('test/load_plugin.test.js', function() { assert(loader.allPlugins.b.enable === true); }); - it('should load plugin.custom.js when env is custom', function() { + it('should load plugin.custom.js when env is custom', () => { mm(process.env, 'EGG_SERVER_ENV', 'custom'); app = utils.createApp('load-plugin-by-env'); const loader = app.loader; @@ -544,7 +540,7 @@ describe('test/load_plugin.test.js', function() { assert(loader.allPlugins.c.enable === true); }); - it('should not load plugin.js when plugin.default.js exist', function() { + it('should not load plugin.js when plugin.default.js exist', () => { mm(process.env, 'EGG_SERVER_ENV', 'unittest'); app = utils.createApp('load-plugin-default'); const loader = app.loader; diff --git a/test/loader/mixin/load_service.test.js b/test/loader/mixin/load_service.test.js index cfb1acb8..f6155602 100644 --- a/test/loader/mixin/load_service.test.js +++ b/test/loader/mixin/load_service.test.js @@ -1,12 +1,10 @@ -'use strict'; - const path = require('path'); const request = require('supertest'); const mm = require('mm'); const assert = require('assert'); const utils = require('../../utils'); -describe('test/loader/mixin/load_service.test.js', function() { +describe('test/loader/mixin/load_service.test.js', () => { let app; afterEach(mm.restore); afterEach(() => app.close()); @@ -39,7 +37,7 @@ describe('test/loader/mixin/load_service.test.js', function() { .expect(200); }); - it('should throw when dulplicate', function() { + it('should throw when dulplicate', () => { assert.throws(() => { app = utils.createApp('service-override'); app.loader.loadPlugin(); @@ -48,7 +46,7 @@ describe('test/loader/mixin/load_service.test.js', function() { }, /can't overwrite property 'foo'/); }); - it('should check es6', function() { + it('should check es6', () => { app = utils.createApp('services_loader_verify'); app.loader.loadPlugin(); app.loader.loadConfig(); @@ -99,7 +97,7 @@ describe('test/loader/mixin/load_service.test.js', function() { .expect(200); }); - describe('subdir', function() { + describe('subdir', () => { it('should load 2 level dir', async () => { mm(process.env, 'NO_DEPRECATION', '*'); app = utils.createApp('subdir-services'); diff --git a/test/utils.js b/test/utils.js index ef29343d..ae9250d9 100644 --- a/test/utils.js +++ b/test/utils.js @@ -27,4 +27,9 @@ module.exports = { view: Symbol('view'), }, + sleep(ms) { + return new Promise(resolve => { + setTimeout(resolve, ms); + }); + }, }; diff --git a/test/utils/index.test.js b/test/utils/index.test.js index 98728eac..368d0108 100644 --- a/test/utils/index.test.js +++ b/test/utils/index.test.js @@ -1,9 +1,7 @@ -'use strict'; - const mm = require('mm'); const path = require('path'); const assert = require('assert'); -const sleep = require('mz-modules/sleep'); +const { sleep } = require('../utils'); const utils = require('../../lib/utils'); describe('test/utils/index.test.js', () => { diff --git a/test/utils/router.test.js b/test/utils/router.test.js index db97eace..028b6f81 100644 --- a/test/utils/router.test.js +++ b/test/utils/router.test.js @@ -1,5 +1,3 @@ -'use strict'; - const assert = require('assert'); const request = require('supertest'); const utils = require('../utils'); @@ -140,7 +138,7 @@ describe('test/utils/router.test.js', () => { }); }); - describe('no name', function() { + describe('no name', () => { it('should GET /comments', () => { return request(app.callback()) .get('/comments')