Skip to content

Commit 9f2abf0

Browse files
committed
cluster test
1 parent f0197e6 commit 9f2abf0

File tree

12 files changed

+145
-94
lines changed

12 files changed

+145
-94
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ run/
99
.idea
1010
.nyc_output
1111
package-lock.json
12+
.package-lock.json
1213
.tshy*
1314
.eslintcache
1415
dist

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const mock = {
6262
_mock(process.env, 'EGG_LOG', level);
6363
},
6464

65-
home(homePath: string) {
65+
home(homePath?: string) {
6666
if (homePath) {
6767
_mock(process.env, 'EGG_HOME', homePath);
6868
}

src/lib/format_options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function formatOptions(initOptions?: MockOptions) {
4141
// it will throw when framework is not found
4242
framework = getFrameworkPath({ framework, baseDir: options.baseDir });
4343
}
44-
options.framework = framework;
44+
options.framework = options.customEgg = framework;
4545

4646
const plugins = options.plugins = options.plugins || {};
4747

test/app.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function call(method: string) {
175175
});
176176
});
177177

178-
it.only('should app.expectLog(), app.notExpectLog() work', async () => {
178+
it('should app.expectLog(), app.notExpectLog() work', async () => {
179179
await app.httpRequest()
180180
.get('/logger')
181181
.expect(200)

test/cluster.test.js renamed to test/cluster.test.ts

Lines changed: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
'use strict';
2-
3-
const path = require('path');
4-
const assert = require('assert');
5-
const request = require('supertest');
6-
const mm = require('..');
7-
8-
const fixtures = path.join(__dirname, 'fixtures');
9-
10-
describe('test/cluster.test.js', () => {
1+
import { strict as assert } from 'node:assert';
2+
import { getFixtures } from './helper.js';
3+
import mm, { MockApplication } from '../src/index.js';
114

5+
describe('test/cluster.test.ts', () => {
126
afterEach(mm.restore);
137

148
describe('normal', () => {
15-
let app;
9+
let app: MockApplication;
1610
before(() => {
1711
app = mm.cluster({
18-
baseDir: path.join(fixtures, 'demo'),
12+
baseDir: getFixtures('demo'),
1913
cache: false,
2014
coverage: false,
2115
});
@@ -25,8 +19,8 @@ describe('test/cluster.test.js', () => {
2519
after(() => app.close());
2620

2721
it('should have members', async () => {
28-
assert(app.callback() === app);
29-
assert(app.listen() === app);
22+
assert.equal(app.callback(), app);
23+
assert.equal(app.listen(), app);
3024
await app.ready();
3125
assert(app.process);
3226
});
@@ -43,10 +37,10 @@ describe('test/cluster.test.js', () => {
4337
});
4438

4539
describe('cluster with fullpath baseDir', () => {
46-
let app;
40+
let app: MockApplication;
4741
before(done => {
4842
app = mm.cluster({
49-
baseDir: path.join(fixtures, 'demo'),
43+
baseDir: getFixtures('demo'),
5044
cache: false,
5145
coverage: false,
5246
});
@@ -55,15 +49,15 @@ describe('test/cluster.test.js', () => {
5549
after(() => app.close());
5650

5751
it('should work', done => {
58-
request(app.callback())
52+
app.httpRequest()
5953
.get('/hello')
6054
.expect('hi')
6155
.expect(200, done);
6256
});
6357
});
6458

6559
describe('cluster with shortpath baseDir', () => {
66-
let app;
60+
let app: MockApplication;
6761
before(done => {
6862
app = mm.cluster({
6963
baseDir: 'demo',
@@ -75,19 +69,43 @@ describe('test/cluster.test.js', () => {
7569
after(() => app.close());
7670

7771
it('should work', done => {
78-
request(app.callback())
72+
app.httpRequest()
7973
.get('/hello')
8074
.expect('hi')
8175
.expect(200, done);
8276
});
8377
});
8478

8579
describe('cluster with customEgg=string', () => {
86-
let app;
80+
let app: MockApplication;
81+
before(done => {
82+
app = mm.cluster({
83+
baseDir: 'apps/barapp',
84+
customEgg: getFixtures('bar'),
85+
cache: false,
86+
coverage: false,
87+
});
88+
app.ready(done);
89+
});
90+
after(() => app.close());
91+
92+
it('should work', done => {
93+
app.httpRequest()
94+
.get('/')
95+
.expect({
96+
foo: 'bar',
97+
foobar: 'bar',
98+
})
99+
.expect(200, done);
100+
});
101+
});
102+
103+
describe('cluster with framework=string', () => {
104+
let app: MockApplication;
87105
before(done => {
88106
app = mm.cluster({
89107
baseDir: 'apps/barapp',
90-
customEgg: path.join(fixtures, 'bar'),
108+
framework: getFixtures('bar'),
91109
cache: false,
92110
coverage: false,
93111
});
@@ -96,7 +114,7 @@ describe('test/cluster.test.js', () => {
96114
after(() => app.close());
97115

98116
it('should work', done => {
99-
request(app.callback())
117+
app.httpRequest()
100118
.get('/')
101119
.expect({
102120
foo: 'bar',
@@ -107,13 +125,13 @@ describe('test/cluster.test.js', () => {
107125
});
108126

109127
describe('cluster with customEgg=true', () => {
110-
let app;
128+
let app: MockApplication;
111129
before(done => {
112130
mm(process, 'cwd', () => {
113-
return path.join(fixtures, 'bar');
131+
return getFixtures('bar');
114132
});
115133
app = mm.cluster({
116-
baseDir: path.join(fixtures, 'apps/barapp'),
134+
baseDir: getFixtures('apps/barapp'),
117135
customEgg: true,
118136
cache: false,
119137
coverage: false,
@@ -123,7 +141,34 @@ describe('test/cluster.test.js', () => {
123141
after(() => app.close());
124142

125143
it('should work', done => {
126-
request(app.callback())
144+
app.httpRequest()
145+
.get('/')
146+
.expect({
147+
foo: 'bar',
148+
foobar: 'bar',
149+
})
150+
.expect(200, done);
151+
});
152+
});
153+
154+
describe('cluster with framework=true', () => {
155+
let app: MockApplication;
156+
before(done => {
157+
mm(process, 'cwd', () => {
158+
return getFixtures('bar');
159+
});
160+
app = mm.cluster({
161+
baseDir: getFixtures('apps/barapp'),
162+
framework: true,
163+
cache: false,
164+
coverage: false,
165+
});
166+
app.ready(done);
167+
});
168+
after(() => app.close());
169+
170+
it('should work', done => {
171+
app.httpRequest()
127172
.get('/')
128173
.expect({
129174
foo: 'bar',
@@ -134,8 +179,8 @@ describe('test/cluster.test.js', () => {
134179
});
135180

136181
describe('cluster with cache', () => {
137-
let app1;
138-
let app2;
182+
let app1: MockApplication;
183+
let app2: MockApplication;
139184
afterEach(() => {
140185
const promises = [];
141186
app1 && promises.push(app1.close());
@@ -179,17 +224,17 @@ describe('test/cluster.test.js', () => {
179224
});
180225

181226
describe('cluster with eggPath', () => {
182-
let app;
227+
let app: MockApplication;
183228
after(() => app.close());
184229

185230
it('should get eggPath', async () => {
186231
app = mm.cluster({
187232
baseDir: 'demo',
188-
customEgg: path.join(__dirname, 'fixtures/chair'),
233+
customEgg: getFixtures('chair'),
189234
eggPath: '/path/to/eggPath',
190235
cache: false,
191236
coverage: false,
192-
});
237+
} as any);
193238
await app
194239
.debug()
195240
.expect('stdout', /\/path\/to\/eggPath/)
@@ -198,13 +243,13 @@ describe('test/cluster.test.js', () => {
198243
});
199244

200245
describe('cluster with workers', () => {
201-
let app;
246+
let app: MockApplication;
202247
after(() => app.close());
203248

204249
it('should get 2 workers', async () => {
205250
app = mm.cluster({
206251
baseDir: 'demo',
207-
customEgg: path.join(__dirname, 'fixtures/chair'),
252+
customEgg: getFixtures('chair'),
208253
workers: 2,
209254
cache: false,
210255
coverage: false,
@@ -217,13 +262,13 @@ describe('test/cluster.test.js', () => {
217262
});
218263

219264
describe('cluster with opts.customEgg', () => {
220-
let app;
265+
let app: MockApplication;
221266
after(() => app.close());
222267

223268
it('should pass execArgv', async () => {
224269
app = mm.cluster({
225270
baseDir: 'custom_egg',
226-
customEgg: path.join(__dirname, 'fixtures/bar'),
271+
customEgg: getFixtures('bar'),
227272
workers: 1,
228273
cache: false,
229274
coverage: false,
@@ -239,7 +284,7 @@ describe('test/cluster.test.js', () => {
239284
});
240285

241286
describe('cluster with egg.framework=yadan', () => {
242-
let app;
287+
let app: MockApplication;
243288
after(() => app.close());
244289

245290
it('should pass execArgv', async () => {
@@ -254,8 +299,8 @@ describe('test/cluster.test.js', () => {
254299
});
255300
});
256301

257-
describe('prerequire', () => {
258-
let app;
302+
describe.skip('prerequire', () => {
303+
let app: MockApplication;
259304
after(() => app.close());
260305

261306
it('should load files', async () => {
@@ -275,12 +320,12 @@ describe('test/cluster.test.js', () => {
275320
});
276321

277322
describe('custom port', () => {
278-
let app;
323+
let app: MockApplication;
279324
after(() => app.close());
280325

281326
it('should use it', async () => {
282327
app = mm.cluster({
283-
baseDir: path.join(fixtures, 'demo'),
328+
baseDir: getFixtures('demo'),
284329
cache: false,
285330
coverage: false,
286331
port: 5566,

test/fixtures/apps/no-framework/plugin/a/app/extend/application.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
module.exports = {
42
mockEnv() {
53
this.config.env = 'mocked by plugin';
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
exports.foobar = 'bar';
42

53
exports.keys = '123';

test/fixtures/chair/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
'use strict';
2-
31
const egg = require('egg');
42

5-
function startCluster(options) {
3+
async function startCluster(options) {
64
// print for the testcase that will assert stdout
75
console.log(options.eggPath);
86
delete options.eggPath;
9-
egg.startCluster(options);
7+
await egg.startCluster(options);
108
}
119

12-
Object.assign(exports, egg, { startCluster });
10+
exports.startCluster = startCluster;
11+
exports.Application = egg.Application;
12+
exports.Agent = egg.Agent;
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
'use strict';
2-
31
exports.keys = '123';

test/fixtures/yadan_app/node_modules/yadan/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)