Skip to content

Commit

Permalink
fix: remove ContextDelegation
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 2, 2025
1 parent b3d83d8 commit 191ddf5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 14 additions & 14 deletions src/app/extend/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -39,17 +39,17 @@ export interface MockContextData {
[key: string]: any;
}

export interface MockContextDelegation extends ContextDelegation {
export interface MockContext extends Context {
service: any;
}

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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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<any>, data?: MockContextData) {
async mockContextScope(fn: (ctx?: MockContext) => Promise<any>, data?: MockContextData) {
const ctx = this.mockContext(data, {
mockCtxStorage: false,
reuseCtxStorage: false,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
9 changes: 5 additions & 4 deletions src/app/middleware/cluster_app_mock.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
26 changes: 13 additions & 13 deletions test/fixtures/tegg-app-esm/test/hooks.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, ContextDelegation> = {};
const afterEachCtxList: Record<string, ContextDelegation> = {};
const itCtxList: Record<string, ContextDelegation> = {};
const beforeEachCtxList: Record<string, Context> = {};
const afterEachCtxList: Record<string, Context> = {};
const itCtxList: Record<string, Context> = {};

before(async () => {
beforeCtx = app.currentContext;
Expand All @@ -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<ContextDelegation> = [];
const itCtxList: Array<Context> = [];

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(() => {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/tegg-app-esm/test/tegg_context.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
26 changes: 13 additions & 13 deletions test/fixtures/tegg-app/test/hooks.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, ContextDelegation> = {};
const afterEachCtxList: Record<string, ContextDelegation> = {};
const itCtxList: Record<string, ContextDelegation> = {};
const beforeEachCtxList: Record<string, Context> = {};
const afterEachCtxList: Record<string, Context> = {};
const itCtxList: Record<string, Context> = {};

before(async () => {
beforeCtx = app.currentContext;
Expand All @@ -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<ContextDelegation> = [];
const itCtxList: Array<Context> = [];

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(() => {
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/tegg-app/test/tegg_context.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
6 changes: 3 additions & 3 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<MockApplication>(app);
expectType<ContextDelegation | undefined>(app.currentContext as ContextDelegation);
expectType<ContextDelegation | undefined>(app.ctxStorage.getStore() as ContextDelegation);
expectType<Context | undefined>(app.currentContext as Context);
expectType<Context | undefined>(app.ctxStorage.getStore() as Context);
expectType<MockApplication>(mock.app());
expectType<MockApplication>(mm.app());

Expand Down

0 comments on commit 191ddf5

Please sign in to comment.