Skip to content

Commit a1c0078

Browse files
committed
fix: add egg to peerDependencies
1 parent 57e0715 commit a1c0078

File tree

6 files changed

+23
-25
lines changed

6 files changed

+23
-25
lines changed

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"utility": "^2.3.0"
4747
},
4848
"peerDependencies": {
49-
"mocha": "^10 || ^11"
49+
"mocha": "^10 || ^11",
50+
"egg": "^4"
5051
},
5152
"devDependencies": {
5253
"@arethetypeswrong/cli": "^0.17.1",
@@ -55,21 +56,21 @@
5556
"@eggjs/tegg-config": "^3.2.2",
5657
"@eggjs/tegg-controller-plugin": "^3.2.2",
5758
"@eggjs/tegg-plugin": "^3.2.2",
59+
"@eggjs/tracer": "^3.0.0",
5860
"@eggjs/tsconfig": "1",
5961
"@types/methods": "^1.1.4",
6062
"@types/mocha": "10",
6163
"@types/node": "22",
62-
"egg": "beta",
64+
"egg": "^4.0.8",
6365
"egg-errors": "^2.2.1",
64-
"egg-tracer": "^2.0.0",
6566
"eslint": "8",
6667
"eslint-config-egg": "14",
67-
"mocha": "^11.0.1",
68-
"pedding": "^2.0.0",
68+
"mocha": "11",
69+
"pedding": "2",
6970
"rimraf": "6",
7071
"tsd": "^0.31.2",
7172
"tshy": "3",
72-
"tshy-after": "^1.3.1",
73+
"tshy-after": "1",
7374
"typescript": "5"
7475
},
7576
"scripts": {

src/app/extend/application.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import assert from 'node:assert';
55
import mergeDescriptors from 'merge-descriptors';
66
import { isAsyncFunction, isObject } from 'is-type-of';
77
import { mock, restore } from 'mm';
8-
import type { HttpClient } from 'urllib';
98
import { Transport, EggLogger, LoggerLevel, LoggerMeta } from 'egg-logger';
10-
import { EggCore, EggCoreOptions, Context } from '@eggjs/core';
9+
import { Application, type Context, type Agent } from 'egg';
10+
import type { MockAgent } from 'urllib';
1111
import { getMockAgent, restoreMockAgent } from '../../lib/mock_agent.js';
1212
import {
1313
createMockHttpClient, MockResultFunction,
@@ -16,6 +16,7 @@ import {
1616
} from '../../lib/mock_httpclient.js';
1717
import { request as supertestRequest, EggTestRequest } from '../../lib/supertest.js';
1818
import { MockOptions } from '../../lib/types.js';
19+
import type AgentUnittest from './agent.js';
1920

2021
const debug = debuglog('@eggjs/mock/app/extend/application');
2122

@@ -40,18 +41,13 @@ export interface MockContextData {
4041
}
4142

4243
export interface MockContext extends Context {
43-
service: any;
4444
}
4545

46-
export default abstract class ApplicationUnittest extends EggCore {
46+
export default abstract class ApplicationUnittest extends Application {
4747
[key: string]: any;
48-
declare options: MockOptions & EggCoreOptions;
48+
declare options: Application['options'] & MockOptions;
4949
_mockHttpClient?: MockHttpClientMethod;
50-
declare logger: EggLogger;
51-
declare coreLogger: EggLogger;
52-
abstract getLogger(name: string): EggLogger;
53-
declare httpClient: HttpClient;
54-
declare httpclient: HttpClient;
50+
declare agent: AgentUnittest & Agent;
5551

5652
/**
5753
* mock Context
@@ -117,7 +113,7 @@ export default abstract class ApplicationUnittest extends EggCore {
117113
mockCtxStorage: false,
118114
reuseCtxStorage: false,
119115
});
120-
return await this.ctxStorage.run(ctx as any, async () => {
116+
return await this.ctxStorage.run(ctx, async () => {
121117
return await fn(ctx);
122118
});
123119
}
@@ -346,7 +342,7 @@ export default abstract class ApplicationUnittest extends EggCore {
346342
* get mock httpclient agent
347343
* @function App#mockHttpclientAgent
348344
*/
349-
mockAgent() {
345+
mockAgent(): MockAgent {
350346
return getMockAgent(this);
351347
}
352348

src/lib/app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'node:path';
55
import { Base } from 'sdk-base';
66
import { detectPort } from 'detect-port';
77
import { importModule } from '@eggjs/utils';
8+
import { type Agent } from 'egg';
89
import { sleep, rimraf, getProperty, getSourceDirname } from './utils.js';
910
import { formatOptions } from './format_options.js';
1011
import { context } from './context.js';
@@ -93,7 +94,7 @@ class MockApplicationWorker extends Base {
9394

9495
// https://github.com/eggjs/egg/blob/8bb7c7e7d59d6aeca4b2ed1eb580368dcb731a4d/lib/egg.js#L125
9596
// egg single mode mount this at start(), so egg-mock should impel it.
96-
app.agent = agent;
97+
app.agent = agent as AgentUnittest & Agent;
9798
Reflect.set(agent, 'app', app);
9899

99100
// egg-mock plugin need to override egg context

test/app_proxy.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('test/app_proxy.test.ts', () => {
3030

3131
it('should not set property', async () => {
3232
assert.throws(() => {
33-
app.curl = function* mockCurl() {
33+
(app as any).curl = async function mockCurl() {
3434
return 'mock';
3535
};
3636
}, /can't set curl before ready/);
@@ -100,11 +100,11 @@ describe('test/app_proxy.test.ts', () => {
100100
after(() => app.close());
101101

102102
it('should override property with setter', async () => {
103-
app.curl = async function mockCurl() {
103+
(app as any).curl = async function mockCurl() {
104104
return 'mock';
105105
};
106-
const data = await app.curl();
107-
assert(data === 'mock');
106+
const data = await app.curl('http://127.0.0.1:7001');
107+
assert.equal(data, 'mock');
108108
});
109109

110110
it('should ignore when set property on MockApplication', async () => {

test/fixtures/tegg-app-esm/config/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default {
1212
enable: true,
1313
},
1414
tracer: {
15-
package: 'egg-tracer',
15+
package: '@eggjs/tracer',
1616
enable: true,
1717
},
1818
};

test/fixtures/tegg-app/config/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
enable: true,
1313
},
1414
tracer: {
15-
package: 'egg-tracer',
15+
package: '@eggjs/tracer',
1616
enable: true,
1717
},
1818
};

0 commit comments

Comments
 (0)