Skip to content

Commit

Permalink
fix: loadCustomLoader should be run before loadCustomApp (#3652)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored Apr 28, 2019
1 parent 7cc8aab commit d3b1cb5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/loader/app_worker_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class AppWorkerLoader extends EggLoader {
this.loadContextExtend();
this.loadHelperExtend();

this.loadCustomLoader();

// app > plugin
this.loadCustomApp();
// app > plugin
Expand All @@ -39,8 +41,6 @@ class AppWorkerLoader extends EggLoader {
this.loadController();
// app
this.loadRouter(); // Dependent on controllers

this.loadCustomLoader();
}

}
Expand Down
1 change: 1 addition & 0 deletions test/doc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const runscript = require('runscript');
const utils = require('./utils');

describe('test/doc.test.js', () => {
if (process.platform === 'win32') return;

let app;
before(async () => {
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/apps/custom-loader/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = class {
constructor(app) {
this.app = app;
}

async didLoad() {
const ctx = this.app.createAnonymousContext();
this.app.beforeLoad = await ctx.repository.user.beforeLoad();
}
};
4 changes: 4 additions & 0 deletions test/fixtures/apps/custom-loader/app/controller/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class UserController {
repository: await this.ctx.repository.user.get(),
};
}

async beforeLoad() {
this.ctx.body = this.app.beforeLoad;
}
}

module.exports = UserController;
4 changes: 4 additions & 0 deletions test/fixtures/apps/custom-loader/app/repository/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class UserRepository {
return this.ctx.params.name;
}

async beforeLoad() {
return 'beforeLoad';
}

}

module.exports = UserRepository;
1 change: 1 addition & 0 deletions test/fixtures/apps/custom-loader/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

module.exports = app => {
app.router.get('/users/:name', app.controller.user.get);
app.router.get('/beforeLoad', app.controller.user.beforeLoad);
};
9 changes: 8 additions & 1 deletion test/lib/core/custom_loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('test/lib/core/custom_loader.test.js', () => {
});
after(() => app.close());

it('should', async () => {
it('should support customLoader', async () => {
await app.httpRequest()
.get('/users/popomore')
.expect({
Expand All @@ -23,4 +23,11 @@ describe('test/lib/core/custom_loader.test.js', () => {
.expect(200);
});

it('should loadCustomLoader before loadCustomApp', async () => {
await app.httpRequest()
.get('/beforeLoad')
.expect('beforeLoad')
.expect(200);
});

});

0 comments on commit d3b1cb5

Please sign in to comment.