@eggjs/mock 7.0.2-beta.1 → 7.0.2-beta.11

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 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
- this.body = ret.data.toString();
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
@@ -86,6 +86,7 @@ var MockApplicationWorker = class extends Base {
86
86
  debug("http server instantiate");
87
87
  createServer(app);
88
88
  await app.ready();
89
+ if (app.server) app.emit("server", app.server);
89
90
  setCustomLoader(app);
90
91
  const msg = {
91
92
  action: "egg-ready",
@@ -29,10 +29,12 @@ function setupApp() {
29
29
  debug("mockParallelApp app: %s", !!app);
30
30
  } else {
31
31
  app = createApp(options);
32
- if (typeof beforeAll === "function") beforeAll(() => app.ready());
33
- if (typeof afterEach === "function") {
34
- afterEach(() => app.backgroundTasksFinished());
35
- afterEach(restore);
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
  }
@@ -8,7 +8,6 @@ function createServer(app) {
8
8
  server = http.createServer(server);
9
9
  app[SERVER] = server;
10
10
  if (!app.server) app.server = server;
11
- app.emit("server", server);
12
11
  }
13
12
  return server;
14
13
  }
@@ -41,6 +41,7 @@ var MockParallelApplication = class extends Base {
41
41
  debug("http server instantiate");
42
42
  createServer(app);
43
43
  await app.ready();
44
+ if (app.server) app.emit("server", app.server);
44
45
  const msg = {
45
46
  action: "egg-ready",
46
47
  data: this.options
@@ -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;
@@ -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
- const { app: bootstrapApp } = await import("./bootstrap.js");
13
- app = bootstrapApp;
14
- await app.ready();
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.1",
3
+ "version": "7.0.2-beta.11",
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.1",
76
- "@eggjs/supertest": "9.0.2-beta.1",
77
- "@eggjs/utils": "5.0.2-beta.1"
75
+ "@eggjs/extend2": "5.0.2-beta.11",
76
+ "@eggjs/utils": "5.0.2-beta.11",
77
+ "@eggjs/supertest": "9.0.2-beta.11"
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.1",
83
- "@eggjs/tracer": "4.0.2-beta.1",
84
- "egg": "4.1.2-beta.1"
82
+ "@eggjs/tracer": "4.0.2-beta.11",
83
+ "egg": "4.1.2-beta.11",
84
+ "@eggjs/errors": "3.0.2-beta.11"
85
85
  },
86
86
  "peerDependencies": {
87
87
  "vitest": "^4.0.15",
88
- "egg": "4.1.2-beta.1"
88
+ "egg": "4.1.2-beta.11"
89
89
  },
90
90
  "peerDependenciesMeta": {
91
91
  "vitest": {