diff --git a/__tests__/context/state.js b/__tests__/context/state.js index 478c73924..d90e38dc8 100644 --- a/__tests__/context/state.js +++ b/__tests__/context/state.js @@ -10,7 +10,7 @@ describe('ctx.state', () => { const app = new Koa() app.use(ctx => { - assert.deepStrictEqual(ctx.state, {}) + assert.deepStrictEqual(ctx.state, this.state || {}) }) const server = app.listen() diff --git a/lib/application.js b/lib/application.js index 31843032b..8b9c36140 100644 --- a/lib/application.js +++ b/lib/application.js @@ -208,7 +208,7 @@ module.exports = class Application extends Emitter { request.response = response response.request = request context.originalUrl = request.originalUrl = req.url - context.state = {} + context.state = Object.assign({}, context.state) return context } diff --git a/lib/context.js b/lib/context.js index a02270f2f..d3f9fad5c 100644 --- a/lib/context.js +++ b/lib/context.js @@ -50,6 +50,7 @@ const proto = module.exports = { request: this.request.toJSON(), response: this.response.toJSON(), app: this.app.toJSON(), + state: this.state, originalUrl: this.originalUrl, req: '', res: '',