Skip to content

Commit

Permalink
fix: add egg to peerDependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Feb 4, 2025
1 parent 57e0715 commit a1c0078
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"utility": "^2.3.0"
},
"peerDependencies": {
"mocha": "^10 || ^11"
"mocha": "^10 || ^11",
"egg": "^4"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.1",
Expand All @@ -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": {
Expand Down
20 changes: 8 additions & 12 deletions src/app/extend/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');

Expand All @@ -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
Expand Down Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -346,7 +342,7 @@ export default abstract class ApplicationUnittest extends EggCore {
* get mock httpclient agent
* @function App#mockHttpclientAgent
*/
mockAgent() {
mockAgent(): MockAgent {
return getMockAgent(this);
}

Expand Down
3 changes: 2 additions & 1 deletion src/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/app_proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand Down Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/tegg-app-esm/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
enable: true,
},
tracer: {
package: 'egg-tracer',
package: '@eggjs/tracer',
enable: true,
},
};
2 changes: 1 addition & 1 deletion test/fixtures/tegg-app/config/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
enable: true,
},
tracer: {
package: 'egg-tracer',
package: '@eggjs/tracer',
enable: true,
},
};

0 comments on commit a1c0078

Please sign in to comment.