Skip to content

Commit f058c21

Browse files
committed
add plugins tests
1 parent 16553a1 commit f058c21

File tree

24 files changed

+226
-248
lines changed

24 files changed

+226
-248
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"formstream": "^1.5.1",
7979
"koa-static": "^5.0.0",
8080
"mm": "^3.4.0",
81-
"pedding": "^2.0.0",
81+
"pedding": "^2.0.1",
8282
"prettier": "^2.7.1",
8383
"rimraf": "6",
8484
"runscript": "^2.0.1",

src/app/extend/context.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ declare module '@eggjs/core' {
1919
getLogger(name: string): EggLogger;
2020
get logger(): EggLogger;
2121
get coreLogger(): EggLogger;
22+
get locals(): Record<string, any>;
2223
}
2324
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
'use strict';
2-
31
const path = require('path');
42
const fs = require('fs');
53

64
module.exports = async function () {
7-
var parts = this.multipart();
8-
var part;
9-
var fields = {};
10-
while (part = await parts) {
5+
const parts = this.multipart();
6+
let filePart;
7+
const fields = {};
8+
for await (const part of parts) {
9+
filePart = part;
1110
if (Array.isArray(part)) {
1211
fields[part[0]] = part[1];
1312
continue;
@@ -16,17 +15,17 @@ module.exports = async function () {
1615
}
1716
}
1817

19-
if (!part || !part.filename) {
18+
if (!filePart || !filePart.filename) {
2019
this.body = {
2120
message: 'no file',
2221
};
2322
return;
2423
}
2524

2625
const ws = fs.createWriteStream(path.join(this.app.config.logger.dir, 'multipart-test-file'));
27-
part.pipe(ws);
26+
filePart.pipe(ws);
2827
this.body = {
29-
filename: part.filename,
28+
filename: filePart.filename,
3029
fields,
3130
};
3231
};

test/fixtures/apps/multipart/config/config.default.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
exports.multipart = {
42
fileExtensions: ['.foo'],
53
};
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
31
exports.schedule = {
42
type: 'worker',
53
cron: '*/5 * * * * *',
64
};
75

8-
exports.task = function* (ctx) {
6+
exports.task = async (ctx) => {
97
ctx.logger.warn('cron wow');
108
};
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
'use strict';
2-
31
exports.keys = 'test key';

test/fixtures/apps/watcher-development-app/agent.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
'use strict';
1+
const path = require('node:path');
22

3-
const utils = require('../../../utils');
4-
const file_path1 = utils.getFilepath('apps/watcher-development-app/tmp-agent.txt');
5-
const dir_path = utils.getFilepath('apps/watcher-development-app/tmp-agent');
3+
const file_path1 = path.join(__dirname, 'tmp-agent.txt');
4+
const dir_path = path.join(__dirname, 'tmp-agent');
65

76
module.exports = function(agent) {
87
let count = 0;

test/fixtures/apps/watcher-development-app/app/router.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
const path = require('node:path');
22

3-
const utils = require('../../../../utils');
4-
const file_path1 = utils.getFilepath('apps/watcher-development-app/tmp.txt');
5-
// const file_path2 = utils.getFilePath('apps/watcher-development-app/tmp/tmp.txt');
6-
const dir_path = utils.getFilepath('apps/watcher-development-app/tmp');
3+
const file_path1 = path.join(__dirname, '../tmp.txt');
4+
const dir_path = path.join(__dirname, '../tmp');
75

86
module.exports = function(app) {
97
let fileChangeCount = 0;

test/fixtures/apps/watcher-development-app/config/config.unittest.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
exports.env = 'local';
42

53
exports.watcher = {

test/lib/core/messenger/local.test.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { strict as assert } from 'node:assert';
2-
import { scheduler } from 'node:timers/promises';
32
import { mm } from '@eggjs/mock';
43
import { pending } from 'pedding';
54
import { singleProcessApp, MockApplication } from '../../../utils.js';
@@ -22,11 +21,11 @@ describe('test/lib/core/messenger/local.test.ts', () => {
2221
describe('broadcast()', () => {
2322
it('app.messenger.broadcast should work', done => {
2423
done = pending(2, done);
25-
app.messenger.once('broadcast-event', msg => {
24+
app.messenger.once('broadcast-event', (msg: unknown) => {
2625
assert.deepEqual(msg, { foo: 'bar' });
2726
done();
2827
});
29-
app.agent.messenger.once('broadcast-event', msg => {
28+
app.agent.messenger.once('broadcast-event', (msg: unknown) => {
3029
assert.deepEqual(msg, { foo: 'bar' });
3130
done();
3231
});
@@ -36,11 +35,11 @@ describe('test/lib/core/messenger/local.test.ts', () => {
3635

3736
it('agent.messenger.broadcast should work', done => {
3837
done = pending(2, done);
39-
app.messenger.once('broadcast-event', msg => {
38+
app.messenger.once('broadcast-event', (msg: unknown) => {
4039
assert.deepEqual(msg, { foo: 'bar' });
4140
done();
4241
});
43-
app.agent.messenger.once('broadcast-event', msg => {
42+
app.agent.messenger.once('broadcast-event', (msg: unknown) => {
4443
assert.deepEqual(msg, { foo: 'bar' });
4544
done();
4645
});
@@ -55,7 +54,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
5554
throw new Error('should not emit on agent');
5655
});
5756

58-
app.messenger.once('sendToApp-event', msg => {
57+
app.messenger.once('sendToApp-event', (msg: unknown) => {
5958
assert.deepEqual(msg, { foo: 'bar' });
6059
done();
6160
});
@@ -68,7 +67,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
6867
throw new Error('should not emit on agent');
6968
});
7069

71-
app.messenger.once('sendToApp-event', msg => {
70+
app.messenger.once('sendToApp-event', (msg: unknown) => {
7271
assert.deepEqual(msg, { foo: 'bar' });
7372
done();
7473
});
@@ -79,7 +78,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
7978

8079
describe('sendToAgent()', () => {
8180
it('app.messenger.sendToAgent should work', done => {
82-
app.agent.messenger.once('sendToAgent-event', msg => {
81+
app.agent.messenger.once('sendToAgent-event', (msg: unknown) => {
8382
assert.deepEqual(msg, { foo: 'bar' });
8483
done();
8584
});
@@ -92,7 +91,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
9291
});
9392

9493
it('agent.messenger.sendToAgent should work', done => {
95-
app.agent.messenger.once('sendToAgent-event', msg => {
94+
app.agent.messenger.once('sendToAgent-event', (msg: unknown) => {
9695
assert.deepEqual(msg, { foo: 'bar' });
9796
done();
9897
});
@@ -107,7 +106,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
107106

108107
describe('sendRandom()', () => {
109108
it('app.messenger.sendRandom should work', done => {
110-
app.agent.messenger.once('sendRandom-event', msg => {
109+
app.agent.messenger.once('sendRandom-event', (msg: unknown) => {
111110
assert.deepEqual(msg, { foo: 'bar' });
112111
done();
113112
});
@@ -124,7 +123,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
124123
throw new Error('should not emit on agent');
125124
});
126125

127-
app.messenger.once('sendRandom-event', msg => {
126+
app.messenger.once('sendRandom-event', (msg: unknown) => {
128127
assert.deepEqual(msg, { foo: 'bar' });
129128
done();
130129
});
@@ -136,11 +135,11 @@ describe('test/lib/core/messenger/local.test.ts', () => {
136135
describe('sendTo(pid)', () => {
137136
it('app.messenger.sendTo should work', done => {
138137
done = pending(2, done);
139-
app.messenger.once('sendTo-event', msg => {
138+
app.messenger.once('sendTo-event', (msg: unknown) => {
140139
assert.deepEqual(msg, { foo: 'bar' });
141140
done();
142141
});
143-
app.agent.messenger.once('sendTo-event', msg => {
142+
app.agent.messenger.once('sendTo-event', (msg: unknown) => {
144143
assert.deepEqual(msg, { foo: 'bar' });
145144
done();
146145
});
@@ -153,12 +152,12 @@ describe('test/lib/core/messenger/local.test.ts', () => {
153152
});
154153

155154
it('agent.messenger.sendTo should work', done => {
156-
done = pedding(done, 2);
157-
app.messenger.once('sendTo-event', msg => {
155+
done = pending(done, 2);
156+
app.messenger.once('sendTo-event', (msg: unknown) => {
158157
assert.deepEqual(msg, { foo: 'bar' });
159158
done();
160159
});
161-
app.agent.messenger.once('sendTo-event', msg => {
160+
app.agent.messenger.once('sendTo-event', (msg: unknown) => {
162161
assert.deepEqual(msg, { foo: 'bar' });
163162
done();
164163
});
@@ -174,7 +173,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
174173
});
175174

176175
it('app.messenger.send should work', done => {
177-
app.agent.messenger.once('send-event', msg => {
176+
app.agent.messenger.once('send-event', (msg: unknown) => {
178177
assert.deepEqual(msg, { foo: 'bar' });
179178
done();
180179
});
@@ -191,7 +190,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
191190
throw new Error('should not emit on agent');
192191
});
193192

194-
app.messenger.once('send-event', msg => {
193+
app.messenger.once('send-event', (msg: unknown) => {
195194
assert.deepEqual(msg, { foo: 'bar' });
196195
done();
197196
});
@@ -200,16 +199,16 @@ describe('test/lib/core/messenger/local.test.ts', () => {
200199
});
201200
});
202201

203-
describe('_onMessage()', () => {
202+
describe('onMessage()', () => {
204203
it('should ignore if message format error', () => {
205-
app.messenger._onMessage();
206-
app.messenger._onMessage('foo');
207-
app.messenger._onMessage({ action: 1 });
204+
app.messenger.onMessage();
205+
app.messenger.onMessage('foo');
206+
app.messenger.onMessage({ action: 1 });
208207
});
209208

210209
it('should emit with action', done => {
211210
app.messenger.once('test-action', done);
212-
app.messenger._onMessage({ action: 'test-action' });
211+
app.messenger.onMessage({ action: 'test-action' });
213212
});
214213
});
215214
});

test/lib/plugins/depd.test.js renamed to test/lib/plugins/depd.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
'use strict';
1+
import { strict as assert } from 'node:assert';
2+
import { mm } from '@eggjs/mock';
3+
import { MockApplication, createApp } from '../../utils.js';
24

3-
const assert = require('assert');
4-
5-
const mm = require('egg-mock');
6-
const utils = require('../../utils');
7-
8-
describe('test/lib/plugins/depd.test.js', () => {
5+
describe('test/lib/plugins/depd.test.ts', () => {
96
afterEach(mm.restore);
107

11-
let app;
8+
let app: MockApplication;
129
before(() => {
13-
app = utils.app('apps/demo');
10+
app = createApp('apps/demo');
1411
return app.ready();
1512
});
1613
after(() => app.close());

test/lib/plugins/development.test.js renamed to test/lib/plugins/development.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const mm = require('egg-mock');
4-
const utils = require('../../utils');
1+
import path from 'node:path';
2+
import fs from 'node:fs';
3+
import { mm } from '@eggjs/mock';
4+
import { MockApplication, createApp, cluster, getFilepath } from '../../utils.js';
55

6-
describe('test/lib/plugins/development.test.js', () => {
6+
describe('test/lib/plugins/development.test.ts', () => {
77
afterEach(mm.restore);
88

99
describe('development app', () => {
10-
let app;
10+
let app: MockApplication;
1111
before(() => {
1212
mm.env('local');
1313
mm(process.env, 'EGG_LOG', 'none');
14-
app = utils.app('apps/development');
14+
app = createApp('apps/development');
1515
return app.ready();
1616
});
1717
after(() => app.close());
1818

1919
it('should ignore assets', async () => {
20-
mm(app.logger, 'info', msg => {
20+
mm(app.logger, 'info', (msg: string) => {
2121
if (msg.match(/status /)) {
2222
throw new Error('should not log status');
2323
}
@@ -42,15 +42,15 @@ describe('test/lib/plugins/development.test.js', () => {
4242
});
4343

4444
describe('reload workers', () => {
45-
let app;
46-
const baseDir = utils.getFilepath('apps/reload-worker');
45+
let app: MockApplication;
46+
const baseDir = getFilepath('apps/reload-worker');
4747
const filepath = path.join(baseDir, 'app/controller/home.js');
4848
const body = fs.readFileSync(filepath);
4949

5050
before(() => {
5151
mm.env('local');
52-
app = utils.cluster('apps/reload-worker');
53-
app.debug();
52+
app = cluster('apps/reload-worker');
53+
// app.debug();
5454
app.coverage(false);
5555
return app.ready();
5656
});

test/lib/plugins/i18n.test.js renamed to test/lib/plugins/i18n.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
'use strict';
2-
const utils = require('../../utils');
1+
import { MockApplication, createApp } from '../../utils.js';
32

4-
describe('test/lib/plugins/i18n.test.js', () => {
5-
let app;
3+
describe('test/lib/plugins/i18n.test.ts', () => {
4+
let app: MockApplication;
65
before(() => {
7-
app = utils.app('apps/i18n');
6+
app = createApp('apps/i18n');
87
return app.ready();
98
});
109
after(() => app.close());

test/lib/plugins/logrotator.test.js

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

0 commit comments

Comments
 (0)