From f4051d03e45f2a4a55e5294a74e6b254200260e9 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 2 Jan 2025 23:10:20 +0800 Subject: [PATCH] fix: remove ContextDelegation (#183) ## Summary by CodeRabbit - **Dependencies** - Updated `@eggjs/core` dependency from version 6.2.5 to 6.2.11 - **Refactor** - Replaced `ContextDelegation` with `Context` across multiple files - Updated logging types from `Logger` to `EggLogger` - Modified type declarations in test files and application code - **Tests** - Updated type assertions in test files to reflect new context and logging types --- package.json | 2 +- src/app/extend/application.ts | 28 +++++++++---------- src/app/middleware/cluster_app_mock.ts | 9 +++--- test/fixtures/tegg-app-esm/test/hooks.test.ts | 26 ++++++++--------- .../tegg-app-esm/test/tegg_context.test.ts | 4 +-- test/fixtures/tegg-app/test/hooks.test.ts | 26 ++++++++--------- .../tegg-app/test/tegg_context.test.ts | 4 +-- test/index.test-d.ts | 6 ++-- 8 files changed, 53 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index e1bb6e3..5165196 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "node": ">= 18.19.0" }, "dependencies": { - "@eggjs/core": "^6.2.5", + "@eggjs/core": "^6.2.11", "@eggjs/supertest": "^8.1.0", "@eggjs/utils": "^4.0.3", "coffee": "^5.2.1", diff --git a/src/app/extend/application.ts b/src/app/extend/application.ts index e386488..8dc80c6 100644 --- a/src/app/extend/application.ts +++ b/src/app/extend/application.ts @@ -6,8 +6,8 @@ import mergeDescriptors from 'merge-descriptors'; import { isAsyncFunction, isObject } from 'is-type-of'; import { mock, restore } from 'mm'; import type { HttpClient } from 'urllib'; -import { Transport, Logger, LoggerLevel, LoggerMeta } from 'egg-logger'; -import { EggCore, EggCoreOptions, ContextDelegation } from '@eggjs/core'; +import { Transport, EggLogger, LoggerLevel, LoggerMeta } from 'egg-logger'; +import { EggCore, EggCoreOptions, Context } from '@eggjs/core'; import { getMockAgent, restoreMockAgent } from '../../lib/mock_agent.js'; import { createMockHttpClient, MockResultFunction, @@ -39,7 +39,7 @@ export interface MockContextData { [key: string]: any; } -export interface MockContextDelegation extends ContextDelegation { +export interface MockContext extends Context { service: any; } @@ -47,9 +47,9 @@ export default abstract class ApplicationUnittest extends EggCore { [key: string]: any; declare options: MockOptions & EggCoreOptions; _mockHttpClient?: MockHttpClientMethod; - declare logger: Logger; - declare coreLogger: Logger; - abstract getLogger(name: string): Logger; + declare logger: EggLogger; + declare coreLogger: EggLogger; + abstract getLogger(name: string): EggLogger; declare httpClient: HttpClient; declare httpclient: HttpClient; @@ -73,7 +73,7 @@ export default abstract class ApplicationUnittest extends EggCore { * }; * ``` */ - mockContext(data?: MockContextData, options?: MockContextOptions): MockContextDelegation { + mockContext(data?: MockContextData, options?: MockContextOptions): MockContext { data = data ?? {}; function mockRequest(req: IncomingMessage) { for (const key in data?.headers) { @@ -102,17 +102,17 @@ export default abstract class ApplicationUnittest extends EggCore { if (this.currentContext && !this.currentContext[REUSED_CTX]) { mockRequest(this.currentContext.request.req); this.currentContext[REUSED_CTX] = true; - return this.currentContext as MockContextDelegation; + return this.currentContext as MockContext; } } const ctx = this.createContext(req, res); if (options.mockCtxStorage) { mock(this.ctxStorage, 'getStore', () => ctx); } - return ctx as MockContextDelegation; + return ctx as MockContext; } - async mockContextScope(fn: (ctx?: MockContextDelegation) => Promise, data?: MockContextData) { + async mockContextScope(fn: (ctx?: MockContext) => Promise, data?: MockContextData) { const ctx = this.mockContext(data, { mockCtxStorage: false, reuseCtxStorage: false, @@ -404,7 +404,7 @@ export default abstract class ApplicationUnittest extends EggCore { * @param {String|Logger} [logger] - logger instance, default is `app.logger` * @function App#mockLog */ - mockLog(logger?: string | Logger) { + mockLog(logger?: string | EggLogger) { logger = logger ?? this.logger; if (typeof logger === 'string') { logger = this.getLogger(logger); @@ -424,7 +424,7 @@ export default abstract class ApplicationUnittest extends EggCore { }); } - __checkExpectLog(expectOrNot: boolean, str: string | RegExp, logger?: string | Logger) { + __checkExpectLog(expectOrNot: boolean, str: string | RegExp, logger?: string | EggLogger) { logger = logger || this.logger; if (typeof logger === 'string') { logger = this.getLogger(logger); @@ -460,7 +460,7 @@ export default abstract class ApplicationUnittest extends EggCore { * @param {String|Logger} [logger] - logger instance, default is `ctx.logger` * @function App#expectLog */ - expectLog(str: string | RegExp, logger?: string | Logger) { + expectLog(str: string | RegExp, logger?: string | EggLogger) { this.__checkExpectLog(true, str, logger); } @@ -470,7 +470,7 @@ export default abstract class ApplicationUnittest extends EggCore { * @param {String|Logger} [logger] - logger instance, default is `ctx.logger` * @function App#notExpectLog */ - notExpectLog(str: string | RegExp, logger?: string | Logger) { + notExpectLog(str: string | RegExp, logger?: string | EggLogger) { this.__checkExpectLog(false, str, logger); } diff --git a/src/app/middleware/cluster_app_mock.ts b/src/app/middleware/cluster_app_mock.ts index d9b17ef..b59a248 100644 --- a/src/app/middleware/cluster_app_mock.ts +++ b/src/app/middleware/cluster_app_mock.ts @@ -1,16 +1,17 @@ import { debuglog } from 'node:util'; -import { ContextDelegation, Next } from '@eggjs/core'; +import { Context, Next } from '@eggjs/core'; const debug = debuglog('@eggjs/mock/app/middleware/cluster_app_mock'); export default () => { - return async function clusterAppMock(ctx: ContextDelegation, next: Next) { + return async function clusterAppMock(ctx: Context, next: Next) { // use originalUrl to make sure other middlewares can't change request url if (ctx.originalUrl !== '/__egg_mock_call_function') { return next(); } - debug('%s %s, body: %j', ctx.method, ctx.url, ctx.request.body); - const { method, property, args, needResult } = ctx.request.body; + const body = (ctx.request as any).body; + debug('%s %s, body: %j', ctx.method, ctx.url, body); + const { method, property, args, needResult } = body; if (!method) { ctx.status = 422; ctx.body = { diff --git a/test/fixtures/tegg-app-esm/test/hooks.test.ts b/test/fixtures/tegg-app-esm/test/hooks.test.ts index 8886687..b83c83e 100644 --- a/test/fixtures/tegg-app-esm/test/hooks.test.ts +++ b/test/fixtures/tegg-app-esm/test/hooks.test.ts @@ -1,14 +1,14 @@ import assert from 'node:assert'; -import { ContextDelegation } from 'egg'; +import { Context } from 'egg'; // import { app } from '../../../../src/bootstrap.js'; import { app } from '../../../../dist/esm/bootstrap.js'; describe('test/hooks.test.ts', () => { let beforeCtx; let afterCtx; - const beforeEachCtxList: Record = {}; - const afterEachCtxList: Record = {}; - const itCtxList: Record = {}; + const beforeEachCtxList: Record = {}; + const afterEachCtxList: Record = {}; + const itCtxList: Record = {}; before(async () => { beforeCtx = app.currentContext; @@ -26,41 +26,41 @@ describe('test/hooks.test.ts', () => { describe('foo', () => { beforeEach(() => { - beforeEachCtxList.foo = app.currentContext as ContextDelegation; + beforeEachCtxList.foo = app.currentContext as Context; }); it('should work', () => { - itCtxList.foo = app.currentContext as ContextDelegation; + itCtxList.foo = app.currentContext as Context; }); afterEach(() => { - afterEachCtxList.foo = app.currentContext as ContextDelegation; + afterEachCtxList.foo = app.currentContext as Context; }); }); describe('bar', () => { beforeEach(() => { - beforeEachCtxList.bar = app.currentContext as ContextDelegation; + beforeEachCtxList.bar = app.currentContext as Context; }); it('should work', () => { - itCtxList.bar = app.currentContext as ContextDelegation; + itCtxList.bar = app.currentContext as Context; }); afterEach(() => { - afterEachCtxList.bar = app.currentContext as ContextDelegation; + afterEachCtxList.bar = app.currentContext as Context; }); }); describe('multi it', () => { - const itCtxList: Array = []; + const itCtxList: Array = []; it('should work 1', () => { - itCtxList.push(app.currentContext as ContextDelegation); + itCtxList.push(app.currentContext as Context); }); it('should work 2', () => { - itCtxList.push(app.currentContext as ContextDelegation); + itCtxList.push(app.currentContext as Context); }); after(() => { diff --git a/test/fixtures/tegg-app-esm/test/tegg_context.test.ts b/test/fixtures/tegg-app-esm/test/tegg_context.test.ts index ab23ccd..99d743b 100644 --- a/test/fixtures/tegg-app-esm/test/tegg_context.test.ts +++ b/test/fixtures/tegg-app-esm/test/tegg_context.test.ts @@ -1,11 +1,11 @@ import { strict as assert } from 'node:assert'; -import { ContextDelegation } from 'egg'; +import { Context } from 'egg'; // import { app, mm } from '../../../../src/bootstrap.js'; import { app, mm } from '../../../../dist/commonjs/bootstrap'; import { LogService } from '../app/modules/foo/LogService'; describe('test/tegg_context.test.ts', () => { - let ctx: ContextDelegation; + let ctx: Context; let logService: LogService; before(async () => { logService = await app.getEggObject(LogService); diff --git a/test/fixtures/tegg-app/test/hooks.test.ts b/test/fixtures/tegg-app/test/hooks.test.ts index 6de64db..973e6e9 100644 --- a/test/fixtures/tegg-app/test/hooks.test.ts +++ b/test/fixtures/tegg-app/test/hooks.test.ts @@ -1,14 +1,14 @@ import assert from 'node:assert'; -import { ContextDelegation } from 'egg'; +import { Context } from 'egg'; // import { app } from '../../../../src/bootstrap.js'; import { app } from '../../../../dist/commonjs/bootstrap'; describe('test/hooks.test.ts', () => { let beforeCtx; let afterCtx; - const beforeEachCtxList: Record = {}; - const afterEachCtxList: Record = {}; - const itCtxList: Record = {}; + const beforeEachCtxList: Record = {}; + const afterEachCtxList: Record = {}; + const itCtxList: Record = {}; before(async () => { beforeCtx = app.currentContext; @@ -26,41 +26,41 @@ describe('test/hooks.test.ts', () => { describe('foo', () => { beforeEach(() => { - beforeEachCtxList.foo = app.currentContext as ContextDelegation; + beforeEachCtxList.foo = app.currentContext as Context; }); it('should work', () => { - itCtxList.foo = app.currentContext as ContextDelegation; + itCtxList.foo = app.currentContext as Context; }); afterEach(() => { - afterEachCtxList.foo = app.currentContext as ContextDelegation; + afterEachCtxList.foo = app.currentContext as Context; }); }); describe('bar', () => { beforeEach(() => { - beforeEachCtxList.bar = app.currentContext as ContextDelegation; + beforeEachCtxList.bar = app.currentContext as Context; }); it('should work', () => { - itCtxList.bar = app.currentContext as ContextDelegation; + itCtxList.bar = app.currentContext as Context; }); afterEach(() => { - afterEachCtxList.bar = app.currentContext as ContextDelegation; + afterEachCtxList.bar = app.currentContext as Context; }); }); describe('multi it', () => { - const itCtxList: Array = []; + const itCtxList: Array = []; it('should work 1', () => { - itCtxList.push(app.currentContext as ContextDelegation); + itCtxList.push(app.currentContext as Context); }); it('should work 2', () => { - itCtxList.push(app.currentContext as ContextDelegation); + itCtxList.push(app.currentContext as Context); }); after(() => { diff --git a/test/fixtures/tegg-app/test/tegg_context.test.ts b/test/fixtures/tegg-app/test/tegg_context.test.ts index ab23ccd..99d743b 100644 --- a/test/fixtures/tegg-app/test/tegg_context.test.ts +++ b/test/fixtures/tegg-app/test/tegg_context.test.ts @@ -1,11 +1,11 @@ import { strict as assert } from 'node:assert'; -import { ContextDelegation } from 'egg'; +import { Context } from 'egg'; // import { app, mm } from '../../../../src/bootstrap.js'; import { app, mm } from '../../../../dist/commonjs/bootstrap'; import { LogService } from '../app/modules/foo/LogService'; describe('test/tegg_context.test.ts', () => { - let ctx: ContextDelegation; + let ctx: Context; let logService: LogService; before(async () => { logService = await app.getEggObject(LogService); diff --git a/test/index.test-d.ts b/test/index.test-d.ts index b7942d3..f9177ba 100644 --- a/test/index.test-d.ts +++ b/test/index.test-d.ts @@ -1,12 +1,12 @@ import { expectType } from 'tsd'; -import { ContextDelegation } from 'egg'; +import { Context } from 'egg'; import { MockApplication, MockAgent, ResultObject } from '../src/index.js'; import { getBootstrapApp, mock, mm } from '../src/bootstrap.js'; const app = getBootstrapApp(); expectType(app); -expectType(app.currentContext as ContextDelegation); -expectType(app.ctxStorage.getStore() as ContextDelegation); +expectType(app.currentContext as Context); +expectType(app.ctxStorage.getStore() as Context); expectType(mock.app()); expectType(mm.app());