@eggjs/mock 7.0.2-beta.1 → 7.0.2-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/README.zh_CN.md +5 -5
- package/dist/app/extend/application.js +1 -1
- package/dist/lib/app.js +1 -0
- package/dist/lib/app_handler.js +6 -4
- package/dist/lib/format_options.js +1 -0
- package/dist/lib/mock_http_server.js +0 -1
- package/dist/lib/parallel/app.js +1 -0
- package/dist/lib/types.d.ts +4 -0
- package/dist/setup_vitest.js +18 -3
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -321,14 +321,14 @@ console.log(ctx.user.name); // Jason
|
|
|
321
321
|
|
|
322
322
|
```js
|
|
323
323
|
await app.mockContextScope(
|
|
324
|
-
async ctx => {
|
|
324
|
+
async (ctx) => {
|
|
325
325
|
console.log(ctx.user.name); // Jason
|
|
326
326
|
},
|
|
327
327
|
{
|
|
328
328
|
user: {
|
|
329
329
|
name: 'Jason',
|
|
330
330
|
},
|
|
331
|
-
}
|
|
331
|
+
},
|
|
332
332
|
);
|
|
333
333
|
```
|
|
334
334
|
|
|
@@ -443,7 +443,7 @@ describe('test app', () => {
|
|
|
443
443
|
return app
|
|
444
444
|
.httpRequest()
|
|
445
445
|
.get('/foo')
|
|
446
|
-
.expect(res => {
|
|
446
|
+
.expect((res) => {
|
|
447
447
|
assert(!res.headers.foo);
|
|
448
448
|
})
|
|
449
449
|
.expect(/bar/);
|
package/README.zh_CN.md
CHANGED
|
@@ -333,14 +333,14 @@ console.log(ctx.user.name); // Jason
|
|
|
333
333
|
|
|
334
334
|
```js
|
|
335
335
|
await app.mockContextScope(
|
|
336
|
-
async ctx => {
|
|
336
|
+
async (ctx) => {
|
|
337
337
|
console.log(ctx.user.name); // Jason
|
|
338
338
|
},
|
|
339
339
|
{
|
|
340
340
|
user: {
|
|
341
341
|
name: 'Jason',
|
|
342
342
|
},
|
|
343
|
-
}
|
|
343
|
+
},
|
|
344
344
|
);
|
|
345
345
|
```
|
|
346
346
|
|
|
@@ -403,9 +403,9 @@ return app.httpRequest().post('/login').expect(302);
|
|
|
403
403
|
模拟 httpclient 的请求,例如 `ctx.curl`
|
|
404
404
|
|
|
405
405
|
```js
|
|
406
|
-
app.get('/', async ctx => {
|
|
406
|
+
app.get('/', async (ctx) => {
|
|
407
407
|
const ret = await ctx.curl('https://eggjs.org');
|
|
408
|
-
|
|
408
|
+
ctx.body = ret.data.toString();
|
|
409
409
|
});
|
|
410
410
|
|
|
411
411
|
app.mockHttpclient('https://eggjs.org', {
|
|
@@ -432,7 +432,7 @@ describe('test app', () => {
|
|
|
432
432
|
return app
|
|
433
433
|
.httpRequest()
|
|
434
434
|
.get('/foo')
|
|
435
|
-
.expect(res => {
|
|
435
|
+
.expect((res) => {
|
|
436
436
|
assert(!res.headers.foo);
|
|
437
437
|
})
|
|
438
438
|
.expect(/bar/);
|
|
@@ -53,7 +53,7 @@ var ApplicationUnittest = class extends Application {
|
|
|
53
53
|
const req = this.mockRequest(data);
|
|
54
54
|
const res = new http.ServerResponse(req);
|
|
55
55
|
if (options.reuseCtxStorage !== false) {
|
|
56
|
-
if (this.currentContext && !this.currentContext[REUSED_CTX]) {
|
|
56
|
+
if (this.currentContext && this.currentContext.app === this && !this.currentContext[REUSED_CTX]) {
|
|
57
57
|
mockRequest(this.currentContext.request.req);
|
|
58
58
|
this.currentContext[REUSED_CTX] = true;
|
|
59
59
|
return this.currentContext;
|
package/dist/lib/app.js
CHANGED
package/dist/lib/app_handler.js
CHANGED
|
@@ -29,10 +29,12 @@ function setupApp() {
|
|
|
29
29
|
debug("mockParallelApp app: %s", !!app);
|
|
30
30
|
} else {
|
|
31
31
|
app = createApp(options);
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
if (!globalThis.__eggMockVitestSetup) {
|
|
33
|
+
if (typeof beforeAll === "function") beforeAll(() => app.ready());
|
|
34
|
+
if (typeof afterEach === "function") {
|
|
35
|
+
afterEach(() => app.backgroundTasksFinished());
|
|
36
|
+
afterEach(restore);
|
|
37
|
+
}
|
|
36
38
|
}
|
|
37
39
|
}
|
|
38
40
|
globalThis.__eggMockAppInstance = app;
|
|
@@ -51,6 +51,7 @@ function formatOptions(initOptions) {
|
|
|
51
51
|
const env = process.env.EGG_SERVER_ENV;
|
|
52
52
|
if (!isMocked(process.env, "HOME") && (env === "default" || env === "test" || env === "prod")) mm(process.env, "HOME", options.baseDir);
|
|
53
53
|
if (process.env.EGG_MOCK_SERVER_ENV) options.cache = false;
|
|
54
|
+
if (!options.startMode && process.env.EGG_VITEST_POOL === "threads") options.startMode = "worker_threads";
|
|
54
55
|
debug("[formatOptions] options: %j", options);
|
|
55
56
|
return options;
|
|
56
57
|
}
|
package/dist/lib/parallel/app.js
CHANGED
package/dist/lib/types.d.ts
CHANGED
|
@@ -46,6 +46,10 @@ interface MockOptions {
|
|
|
46
46
|
*/
|
|
47
47
|
mockCtxStorage?: boolean;
|
|
48
48
|
beforeInit?: (app: any) => Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Start mode for egg cluster-client
|
|
51
|
+
*/
|
|
52
|
+
startMode?: "process" | "worker_threads";
|
|
49
53
|
}
|
|
50
54
|
interface MockClusterOptions extends MockOptions {
|
|
51
55
|
workers?: number | string;
|
package/dist/setup_vitest.js
CHANGED
|
@@ -7,17 +7,32 @@ if (!g.before) g.before = beforeAll;
|
|
|
7
7
|
if (!g.after) g.after = afterAll;
|
|
8
8
|
if (!g.beforeEach) g.beforeEach = beforeEach;
|
|
9
9
|
if (!g.afterEach) g.afterEach = afterEach;
|
|
10
|
+
globalThis.__eggMockVitestSetup = true;
|
|
11
|
+
if (!globalThis.__teggVitestConfig) globalThis.__teggVitestConfig = {
|
|
12
|
+
restoreMocks: true,
|
|
13
|
+
getApp: async () => {
|
|
14
|
+
return (await import("./bootstrap.js"))?.app;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
10
17
|
let app;
|
|
18
|
+
let startupPromise;
|
|
11
19
|
beforeAll(async () => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
if (!startupPromise) {
|
|
21
|
+
startupPromise = (async () => {
|
|
22
|
+
const { app: bootstrapApp } = await import("./bootstrap.js");
|
|
23
|
+
app = bootstrapApp;
|
|
24
|
+
await app.ready();
|
|
25
|
+
})();
|
|
26
|
+
startupPromise.catch(() => {});
|
|
27
|
+
}
|
|
28
|
+
await startupPromise;
|
|
15
29
|
});
|
|
16
30
|
afterEach(async () => {
|
|
17
31
|
if (app && typeof app.backgroundTasksFinished === "function") await app.backgroundTasksFinished();
|
|
18
32
|
await proxyMock.restore();
|
|
19
33
|
});
|
|
20
34
|
afterAll(async () => {
|
|
35
|
+
if (globalThis.__eggVitestSharedMode || process.env.EGG_VITEST_ISOLATE === "false" || process.env.EGG_VITEST_POOL === "threads") return;
|
|
21
36
|
if (app) await app.close();
|
|
22
37
|
});
|
|
23
38
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/mock",
|
|
3
|
-
"version": "7.0.2-beta.
|
|
3
|
+
"version": "7.0.2-beta.10",
|
|
4
4
|
"description": "mock server plugin for egg",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"egg",
|
|
@@ -72,20 +72,20 @@
|
|
|
72
72
|
"sdk-base": "^5.0.1",
|
|
73
73
|
"urllib": "^4.8.2",
|
|
74
74
|
"utility": "^2.5.0",
|
|
75
|
-
"@eggjs/extend2": "5.0.2-beta.
|
|
76
|
-
"@eggjs/supertest": "9.0.2-beta.
|
|
77
|
-
"@eggjs/utils": "5.0.2-beta.
|
|
75
|
+
"@eggjs/extend2": "5.0.2-beta.10",
|
|
76
|
+
"@eggjs/supertest": "9.0.2-beta.10",
|
|
77
|
+
"@eggjs/utils": "5.0.2-beta.10"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@types/methods": "^1.1.4",
|
|
81
81
|
"typescript": "^5.9.3",
|
|
82
|
-
"@eggjs/errors": "3.0.2-beta.
|
|
83
|
-
"
|
|
84
|
-
"
|
|
82
|
+
"@eggjs/errors": "3.0.2-beta.10",
|
|
83
|
+
"egg": "4.1.2-beta.10",
|
|
84
|
+
"@eggjs/tracer": "4.0.2-beta.10"
|
|
85
85
|
},
|
|
86
86
|
"peerDependencies": {
|
|
87
87
|
"vitest": "^4.0.15",
|
|
88
|
-
"egg": "4.1.2-beta.
|
|
88
|
+
"egg": "4.1.2-beta.10"
|
|
89
89
|
},
|
|
90
90
|
"peerDependenciesMeta": {
|
|
91
91
|
"vitest": {
|