diff --git a/package.json b/package.json index ec41af4..4c6de60 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "utility": "^2.3.0" }, "peerDependencies": { - "mocha": "^10 || ^11" + "mocha": "^10 || ^11", + "egg": "^4" }, "devDependencies": { "@arethetypeswrong/cli": "^0.17.1", @@ -55,21 +56,21 @@ "@eggjs/tegg-config": "^3.2.2", "@eggjs/tegg-controller-plugin": "^3.2.2", "@eggjs/tegg-plugin": "^3.2.2", + "@eggjs/tracer": "^3.0.0", "@eggjs/tsconfig": "1", "@types/methods": "^1.1.4", "@types/mocha": "10", "@types/node": "22", - "egg": "beta", + "egg": "^4.0.8", "egg-errors": "^2.2.1", - "egg-tracer": "^2.0.0", "eslint": "8", "eslint-config-egg": "14", - "mocha": "^11.0.1", - "pedding": "^2.0.0", + "mocha": "11", + "pedding": "2", "rimraf": "6", "tsd": "^0.31.2", "tshy": "3", - "tshy-after": "^1.3.1", + "tshy-after": "1", "typescript": "5" }, "scripts": { diff --git a/src/app/extend/application.ts b/src/app/extend/application.ts index 8dc80c6..21eccf3 100644 --- a/src/app/extend/application.ts +++ b/src/app/extend/application.ts @@ -5,9 +5,9 @@ import assert from 'node:assert'; import mergeDescriptors from 'merge-descriptors'; import { isAsyncFunction, isObject } from 'is-type-of'; import { mock, restore } from 'mm'; -import type { HttpClient } from 'urllib'; import { Transport, EggLogger, LoggerLevel, LoggerMeta } from 'egg-logger'; -import { EggCore, EggCoreOptions, Context } from '@eggjs/core'; +import { Application, type Context, type Agent } from 'egg'; +import type { MockAgent } from 'urllib'; import { getMockAgent, restoreMockAgent } from '../../lib/mock_agent.js'; import { createMockHttpClient, MockResultFunction, @@ -16,6 +16,7 @@ import { } from '../../lib/mock_httpclient.js'; import { request as supertestRequest, EggTestRequest } from '../../lib/supertest.js'; import { MockOptions } from '../../lib/types.js'; +import type AgentUnittest from './agent.js'; const debug = debuglog('@eggjs/mock/app/extend/application'); @@ -40,18 +41,13 @@ export interface MockContextData { } export interface MockContext extends Context { - service: any; } -export default abstract class ApplicationUnittest extends EggCore { +export default abstract class ApplicationUnittest extends Application { [key: string]: any; - declare options: MockOptions & EggCoreOptions; + declare options: Application['options'] & MockOptions; _mockHttpClient?: MockHttpClientMethod; - declare logger: EggLogger; - declare coreLogger: EggLogger; - abstract getLogger(name: string): EggLogger; - declare httpClient: HttpClient; - declare httpclient: HttpClient; + declare agent: AgentUnittest & Agent; /** * mock Context @@ -117,7 +113,7 @@ export default abstract class ApplicationUnittest extends EggCore { mockCtxStorage: false, reuseCtxStorage: false, }); - return await this.ctxStorage.run(ctx as any, async () => { + return await this.ctxStorage.run(ctx, async () => { return await fn(ctx); }); } @@ -346,7 +342,7 @@ export default abstract class ApplicationUnittest extends EggCore { * get mock httpclient agent * @function App#mockHttpclientAgent */ - mockAgent() { + mockAgent(): MockAgent { return getMockAgent(this); } diff --git a/src/lib/app.ts b/src/lib/app.ts index d4cc750..64fb5e2 100644 --- a/src/lib/app.ts +++ b/src/lib/app.ts @@ -5,6 +5,7 @@ import path from 'node:path'; import { Base } from 'sdk-base'; import { detectPort } from 'detect-port'; import { importModule } from '@eggjs/utils'; +import { type Agent } from 'egg'; import { sleep, rimraf, getProperty, getSourceDirname } from './utils.js'; import { formatOptions } from './format_options.js'; import { context } from './context.js'; @@ -93,7 +94,7 @@ class MockApplicationWorker extends Base { // https://github.com/eggjs/egg/blob/8bb7c7e7d59d6aeca4b2ed1eb580368dcb731a4d/lib/egg.js#L125 // egg single mode mount this at start(), so egg-mock should impel it. - app.agent = agent; + app.agent = agent as AgentUnittest & Agent; Reflect.set(agent, 'app', app); // egg-mock plugin need to override egg context diff --git a/test/app_proxy.test.ts b/test/app_proxy.test.ts index c660b56..db63be8 100644 --- a/test/app_proxy.test.ts +++ b/test/app_proxy.test.ts @@ -30,7 +30,7 @@ describe('test/app_proxy.test.ts', () => { it('should not set property', async () => { assert.throws(() => { - app.curl = function* mockCurl() { + (app as any).curl = async function mockCurl() { return 'mock'; }; }, /can't set curl before ready/); @@ -100,11 +100,11 @@ describe('test/app_proxy.test.ts', () => { after(() => app.close()); it('should override property with setter', async () => { - app.curl = async function mockCurl() { + (app as any).curl = async function mockCurl() { return 'mock'; }; - const data = await app.curl(); - assert(data === 'mock'); + const data = await app.curl('http://127.0.0.1:7001'); + assert.equal(data, 'mock'); }); it('should ignore when set property on MockApplication', async () => { diff --git a/test/fixtures/tegg-app-esm/config/plugin.js b/test/fixtures/tegg-app-esm/config/plugin.js index 4275126..548f166 100644 --- a/test/fixtures/tegg-app-esm/config/plugin.js +++ b/test/fixtures/tegg-app-esm/config/plugin.js @@ -12,7 +12,7 @@ export default { enable: true, }, tracer: { - package: 'egg-tracer', + package: '@eggjs/tracer', enable: true, }, }; diff --git a/test/fixtures/tegg-app/config/plugin.js b/test/fixtures/tegg-app/config/plugin.js index 6745be6..1d2e5b4 100644 --- a/test/fixtures/tegg-app/config/plugin.js +++ b/test/fixtures/tegg-app/config/plugin.js @@ -12,7 +12,7 @@ module.exports = { enable: true, }, tracer: { - package: 'egg-tracer', + package: '@eggjs/tracer', enable: true, }, };