Skip to content

Commit 16553a1

Browse files
committed
fix tests on windows
1 parent 6adebea commit 16553a1

5 files changed

Lines changed: 46 additions & 43 deletions

File tree

test/lib/application.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('test/lib/application.test.ts', () => {
3434
new Application({
3535
baseDir: getFilepath('custom-egg/index.js'),
3636
});
37-
}, /not a directory/);
37+
}, /not a directory|no such file or directory/);
3838
});
3939
});
4040

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
const assert = require('assert');
2-
const mm = require('egg-mock');
3-
const utils = require('../../../utils');
4-
const Messenger = require('../../../../lib/core/messenger/ipc');
1+
import { strict as assert } from 'node:assert';
2+
import { scheduler } from 'node:timers/promises';
3+
import { mm } from '@eggjs/mock';
4+
import { cluster, MockApplication } from '../../../utils.js';
5+
import { Messenger } from '../../../../src/lib/core/messenger/ipc.js';
56

6-
describe('test/lib/core/messenger/ipc.test.js', () => {
7-
let messenger;
7+
describe('test/lib/core/messenger/ipc.test.ts', () => {
8+
let messenger: Messenger;
9+
const app: any = {};
810

911
before(() => {
10-
messenger = new Messenger();
12+
messenger = new Messenger(app);
1113
});
1214

1315
afterEach(mm.restore);
@@ -21,20 +23,20 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
2123
done();
2224
});
2325

24-
process.emit('message', {});
25-
process.emit('message', null);
26+
process.emit('message', {}, null);
27+
process.emit('message', null, null);
2628
process.emit('message', {
2729
action: 'messenger-test-on-event',
2830
data: {
2931
success: true,
3032
},
31-
});
33+
}, null);
3234
});
3335
});
3436

3537
describe('close()', () => {
3638
it('should remove all listeners', () => {
37-
const messenger = new Messenger();
39+
const messenger = new Messenger(app);
3840
messenger.on('messenger-test-on-event-2', () => {
3941
throw new Error('should never emitted');
4042
});
@@ -46,17 +48,17 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
4648
data: {
4749
success: true,
4850
},
49-
});
51+
}, null);
5052
});
5153
});
5254

5355
describe('cluster messenger', () => {
54-
let app;
56+
let app: MockApplication;
5557
after(() => app.close());
5658

5759
// use it to record create coverage codes time
5860
it('before: should start cluster app', async () => {
59-
app = utils.cluster('apps/messenger');
61+
app = cluster('apps/messenger');
6062
app.coverage(true);
6163
await app.ready();
6264
await scheduler.wait(1000);
@@ -66,7 +68,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
6668
app.expect('stdout', /\[app] agent-to-app agent msg/);
6769
});
6870

69-
it('app should accept agent assgin pid message', () => {
71+
it('app should accept agent assign pid message', () => {
7072
app.expect('stdout', /\[app] agent-to-app agent msg \d+/);
7173
});
7274

@@ -84,14 +86,14 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
8486
});
8587

8688
describe('broadcast()', () => {
87-
let app;
89+
let app: MockApplication;
8890
before(() => {
8991
mm.env('default');
90-
app = utils.cluster('apps/messenger-broadcast', { workers: 2 });
92+
app = cluster('apps/messenger-broadcast', { workers: 2 });
9193
app.coverage(false);
9294
return app.ready();
9395
});
94-
before(() => utils.sleep(1000));
96+
before(() => scheduler.wait(1000));
9597
after(() => app.close());
9698

9799
it('should broadcast each other', () => {
@@ -105,15 +107,15 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
105107
// agent 26494 receive message from app pid 26496
106108
// agent 26494 receive message from agent pid 26494
107109
const m = app.stdout.match(/(app|agent) \d+ receive message from (app|agent) pid \d+/g);
108-
assert(m.length, 9);
110+
assert.equal(m.length, 9);
109111
});
110112
});
111113

112114
describe('sendRandom', () => {
113-
let app;
115+
let app: MockApplication;
114116
before(() => {
115117
mm.env('default');
116-
app = utils.cluster('apps/messenger-random', { workers: 4 });
118+
app = cluster('apps/messenger-random', { workers: 4 });
117119
app.coverage(false);
118120
return app.ready();
119121
});
@@ -137,10 +139,10 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
137139
});
138140

139141
describe('sendToApp and sentToAgent', () => {
140-
let app;
142+
let app: MockApplication;
141143
before(() => {
142144
mm.env('default');
143-
app = utils.cluster('apps/messenger-app-agent', { workers: 2 });
145+
app = cluster('apps/messenger-app-agent', { workers: 2 });
144146
app.coverage(false);
145147
return app.ready();
146148
});
@@ -155,7 +157,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
155157
done();
156158
}, 500);
157159

158-
function count(data, key) {
160+
function count(data: string, key: string) {
159161
return data.split('\n').filter(line => {
160162
return line.indexOf(key) >= 0;
161163
}).length;
@@ -164,10 +166,10 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
164166
});
165167

166168
describe('worker_threads mode', () => {
167-
let app;
169+
let app: MockApplication;
168170
before(() => {
169171
mm.env('default');
170-
app = utils.cluster('apps/messenger-app-agent', { workers: 1, startMode: 'worker_threads' });
172+
app = cluster('apps/messenger-app-agent', { workers: 1, startMode: 'worker_threads' });
171173
app.coverage(false);
172174
return app.ready();
173175
});
@@ -182,7 +184,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
182184
done();
183185
}, 500);
184186

185-
function count(data, key) {
187+
function count(data: string, key: string) {
186188
return data.split('\n').filter(line => {
187189
return line.indexOf(key) >= 0;
188190
}).length;
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
'use strict';
1+
import { strict as assert } from 'node:assert';
2+
import { scheduler } from 'node:timers/promises';
3+
import { mm } from '@eggjs/mock';
4+
import { pending } from 'pedding';
5+
import { singleProcessApp, MockApplication } from '../../../utils.js';
26

3-
const utils = require('../../../utils');
4-
const pedding = require('pedding');
5-
const assert = require('assert');
6-
const mm = require('egg-mock');
7-
8-
describe('test/lib/core/messenger/local.test.js', () => {
9-
let app;
7+
describe('test/lib/core/messenger/local.test.ts', () => {
8+
let app: MockApplication;
109

1110
before(async () => {
12-
app = await utils.singleProcessApp('apps/demo');
11+
app = await singleProcessApp('apps/demo');
1312
});
1413

1514
after(() => app.close());
@@ -22,7 +21,7 @@ describe('test/lib/core/messenger/local.test.js', () => {
2221

2322
describe('broadcast()', () => {
2423
it('app.messenger.broadcast should work', done => {
25-
done = pedding(done, 2);
24+
done = pending(2, done);
2625
app.messenger.once('broadcast-event', msg => {
2726
assert.deepEqual(msg, { foo: 'bar' });
2827
done();
@@ -36,7 +35,7 @@ describe('test/lib/core/messenger/local.test.js', () => {
3635
});
3736

3837
it('agent.messenger.broadcast should work', done => {
39-
done = pedding(done, 2);
38+
done = pending(2, done);
4039
app.messenger.once('broadcast-event', msg => {
4140
assert.deepEqual(msg, { foo: 'bar' });
4241
done();
@@ -136,7 +135,7 @@ describe('test/lib/core/messenger/local.test.js', () => {
136135

137136
describe('sendTo(pid)', () => {
138137
it('app.messenger.sendTo should work', done => {
139-
done = pedding(done, 2);
138+
done = pending(2, done);
140139
app.messenger.once('sendTo-event', msg => {
141140
assert.deepEqual(msg, { foo: 'bar' });
142141
done();

test/lib/egg.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ describe('test/lib/egg.test.ts', () => {
180180
});
181181

182182
it('should dumpTiming when timeout', async () => {
183+
if (process.platform === 'win32') return;
183184
const baseDir = getFilepath('apps/dumptiming-timeout');
184185
fs.rmSync(path.join(baseDir, 'run'), { recursive: true, force: true });
185186
fs.rmSync(path.join(baseDir, 'logs'), { recursive: true, force: true });
@@ -193,6 +194,7 @@ describe('test/lib/egg.test.ts', () => {
193194
});
194195

195196
it('should dump slow-boot-action warnning log', async () => {
197+
if (process.platform === 'win32') return;
196198
const baseDir = getFilepath('apps/dumptiming-slowBootActionMinDuration');
197199
fs.rmSync(path.join(baseDir, 'run'), { recursive: true, force: true });
198200
fs.rmSync(path.join(baseDir, 'logs'), { recursive: true, force: true });

test/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url';
77
import { AddressInfo } from 'node:net';
88
import { scheduler } from 'node:timers/promises';
99
import {
10-
mm, MockOptions, MockApplication,
10+
mm, MockOptions, MockClusterOptions, MockApplication,
1111
} from '@eggjs/mock';
1212
import { Application as Koa } from '@eggjs/koa';
1313
import { request } from '@eggjs/supertest';
@@ -23,7 +23,7 @@ export async function rimraf(target: string) {
2323
await rm(target, { force: true, recursive: true });
2424
}
2525

26-
export { MockApplication, MockOptions, mm } from '@eggjs/mock';
26+
export { MockApplication, MockOptions, MockClusterOptions, mm };
2727
export const restore = () => mm.restore();
2828

2929
export function app(name: string | MockOptions, options?: MockOptions) {
@@ -42,7 +42,7 @@ export const createApp = app;
4242
* @param {Object} [options] - optional
4343
* @return {App} app - Application object.
4444
*/
45-
export function cluster(name: string | MockOptions, options?: MockOptions): MockApplication {
45+
export function cluster(name: string | MockClusterOptions, options?: MockClusterOptions): MockApplication {
4646
options = formatOptions(name, options);
4747
return mm.cluster(options) as unknown as MockApplication;
4848
}

0 commit comments

Comments
 (0)