Skip to content

Commit 7bf4b72

Browse files
committed
first test pass version
1 parent 111876c commit 7bf4b72

35 files changed

+935
-910
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,6 @@
116116
"src"
117117
],
118118
"types": "./dist/commonjs/index.d.ts",
119-
"main": "./dist/commonjs/index.js"
119+
"main": "./dist/commonjs/index.js",
120+
"module": "./dist/esm/index.js"
120121
}

src/app/extend/application.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ export default abstract class ApplicationUnittest extends EggCore {
6464
* };
6565
* ```
6666
*/
67-
mockContext(data: MockContextData, options?: MockContextOptions) {
67+
mockContext(data?: MockContextData, options?: MockContextOptions) {
68+
data = data ?? {};
6869
function mockRequest(req: IncomingMessage) {
69-
for (const key in data.headers) {
70+
for (const key in data?.headers) {
7071
mock(req.headers, key, data.headers[key]);
7172
mock(req.headers, key.toLowerCase(), data.headers[key]);
7273
}
@@ -75,7 +76,6 @@ export default abstract class ApplicationUnittest extends EggCore {
7576
// try to use app.options.mockCtxStorage first
7677
const mockCtxStorage = this.options.mockCtxStorage ?? true;
7778
options = Object.assign({ mockCtxStorage }, options);
78-
data = data || {};
7979

8080
if ('_customMockContext' in this && typeof this._customMockContext === 'function') {
8181
this._customMockContext(data);
@@ -368,6 +368,7 @@ export default abstract class ApplicationUnittest extends EggCore {
368368
mockEnv(env: string) {
369369
mock(this.config, 'env', env);
370370
mock(this.config, 'serverEnv', env);
371+
debug('mock env: %o', env);
371372
return this;
372373
}
373374

src/bootstrap.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import * as assert from 'assert';
2-
import { MockApplication, EggMock } from './';
1+
// import * as assert from 'assert';
2+
// import { MockApplication, EggMock } from './';
33

4-
export {
5-
assert,
6-
};
7-
export declare const app: MockApplication;
8-
export declare const mock: EggMock;
9-
export declare const mm: EggMock;
4+
// export {
5+
// assert,
6+
// };
7+
// export declare const app: MockApplication;
8+
// export declare const mock: EggMock;
9+
// export declare const mm: EggMock;
1010

src/bootstrap.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/bootstrap.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { strict as assert } from 'node:assert';
2+
import path from 'node:path';
3+
import { readJSONSync } from 'utility';
4+
import { mock } from './index.js';
5+
import { getBootstrapApp, setupApp } from './lib/app_handler.js';
6+
import { getEggOptions } from './lib/utils.js';
7+
8+
const options = getEggOptions();
9+
10+
// throw error when an egg plugin test is using bootstrap
11+
const pkgInfo = readJSONSync(path.join(options.baseDir || process.cwd(), 'package.json'));
12+
if (pkgInfo.eggPlugin) {
13+
throw new Error('DO NOT USE bootstrap to test plugin');
14+
}
15+
16+
setupApp();
17+
18+
const app = getBootstrapApp();
19+
20+
export {
21+
assert,
22+
app,
23+
mock,
24+
mock as mm,
25+
};

0 commit comments

Comments
 (0)