Skip to content

Commit

Permalink
first test pass version
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 22, 2024
1 parent 111876c commit 7bf4b72
Show file tree
Hide file tree
Showing 35 changed files with 935 additions and 910 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@
"src"
],
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/commonjs/index.js"
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js"
}
7 changes: 4 additions & 3 deletions src/app/extend/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ export default abstract class ApplicationUnittest extends EggCore {
* };
* ```
*/
mockContext(data: MockContextData, options?: MockContextOptions) {
mockContext(data?: MockContextData, options?: MockContextOptions) {
data = data ?? {};
function mockRequest(req: IncomingMessage) {
for (const key in data.headers) {
for (const key in data?.headers) {
mock(req.headers, key, data.headers[key]);
mock(req.headers, key.toLowerCase(), data.headers[key]);
}
Expand All @@ -75,7 +76,6 @@ export default abstract class ApplicationUnittest extends EggCore {
// try to use app.options.mockCtxStorage first
const mockCtxStorage = this.options.mockCtxStorage ?? true;
options = Object.assign({ mockCtxStorage }, options);
data = data || {};

if ('_customMockContext' in this && typeof this._customMockContext === 'function') {
this._customMockContext(data);
Expand Down Expand Up @@ -368,6 +368,7 @@ export default abstract class ApplicationUnittest extends EggCore {
mockEnv(env: string) {
mock(this.config, 'env', env);
mock(this.config, 'serverEnv', env);
debug('mock env: %o', env);
return this;
}

Expand Down
16 changes: 8 additions & 8 deletions src/bootstrap.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as assert from 'assert';
import { MockApplication, EggMock } from './';
// import * as assert from 'assert';
// import { MockApplication, EggMock } from './';

export {
assert,
};
export declare const app: MockApplication;
export declare const mock: EggMock;
export declare const mm: EggMock;
// export {
// assert,
// };
// export declare const app: MockApplication;
// export declare const mock: EggMock;
// export declare const mm: EggMock;

23 changes: 0 additions & 23 deletions src/bootstrap.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { strict as assert } from 'node:assert';
import path from 'node:path';
import { readJSONSync } from 'utility';
import { mock } from './index.js';
import { getBootstrapApp, setupApp } from './lib/app_handler.js';
import { getEggOptions } from './lib/utils.js';

const options = getEggOptions();

// throw error when an egg plugin test is using bootstrap
const pkgInfo = readJSONSync(path.join(options.baseDir || process.cwd(), 'package.json'));
if (pkgInfo.eggPlugin) {
throw new Error('DO NOT USE bootstrap to test plugin');
}

setupApp();

const app = getBootstrapApp();

export {
assert,
app,
mock,
mock as mm,
};
Loading

0 comments on commit 7bf4b72

Please sign in to comment.