Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type defines #185

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@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",
Expand Down
14 changes: 5 additions & 9 deletions src/app/extend/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 { Transport, Logger, LoggerLevel, LoggerMeta } from 'egg-logger';
import { EggCore, EggCoreOptions, Context } from '@eggjs/core';
import { getMockAgent, restoreMockAgent } from '../../lib/mock_agent.js';
import {
Expand Down Expand Up @@ -47,10 +47,6 @@ export default abstract class ApplicationUnittest extends EggCore {
[key: string]: any;
declare options: MockOptions & EggCoreOptions;
_mockHttpClient?: MockHttpClientMethod;
declare logger: EggLogger;
declare coreLogger: EggLogger;
abstract getLogger(name: string): EggLogger;
declare httpClient: HttpClient;
declare httpclient: HttpClient;

/**
Expand Down Expand Up @@ -404,7 +400,7 @@ export default abstract class ApplicationUnittest extends EggCore {
* @param {String|Logger} [logger] - logger instance, default is `app.logger`
* @function App#mockLog
*/
mockLog(logger?: string | EggLogger) {
mockLog(logger?: string | Logger) {
logger = logger ?? this.logger;
if (typeof logger === 'string') {
logger = this.getLogger(logger);
Expand All @@ -424,7 +420,7 @@ export default abstract class ApplicationUnittest extends EggCore {
});
}

__checkExpectLog(expectOrNot: boolean, str: string | RegExp, logger?: string | EggLogger) {
__checkExpectLog(expectOrNot: boolean, str: string | RegExp, logger?: string | Logger) {
logger = logger || this.logger;
if (typeof logger === 'string') {
logger = this.getLogger(logger);
Expand Down Expand Up @@ -460,7 +456,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 | EggLogger) {
expectLog(str: string | RegExp, logger?: string | Logger) {
this.__checkExpectLog(true, str, logger);
}

Expand All @@ -470,7 +466,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 | EggLogger) {
notExpectLog(str: string | RegExp, logger?: string | Logger) {
this.__checkExpectLog(false, str, logger);
}

Expand Down
4 changes: 4 additions & 0 deletions src/typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// make sure to import egg typings and let typescript know about it
// @see https://github.com/whxaxes/blog/issues/11
// and https://www.typescriptlang.org/docs/handbook/declaration-merging.html
import 'egg';
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