Skip to content

Commit

Permalink
add plugins tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 1, 2025
1 parent 16553a1 commit f058c21
Show file tree
Hide file tree
Showing 24 changed files with 226 additions and 248 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"formstream": "^1.5.1",
"koa-static": "^5.0.0",
"mm": "^3.4.0",
"pedding": "^2.0.0",
"pedding": "^2.0.1",
"prettier": "^2.7.1",
"rimraf": "6",
"runscript": "^2.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/app/extend/context.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ declare module '@eggjs/core' {
getLogger(name: string): EggLogger;
get logger(): EggLogger;
get coreLogger(): EggLogger;
get locals(): Record<string, any>;
}
}
17 changes: 8 additions & 9 deletions test/fixtures/apps/multipart/app/controller/upload.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict';

const path = require('path');
const fs = require('fs');

module.exports = async function () {
var parts = this.multipart();
var part;
var fields = {};
while (part = await parts) {
const parts = this.multipart();
let filePart;
const fields = {};
for await (const part of parts) {
filePart = part;
if (Array.isArray(part)) {
fields[part[0]] = part[1];
continue;
Expand All @@ -16,17 +15,17 @@ module.exports = async function () {
}
}

if (!part || !part.filename) {
if (!filePart || !filePart.filename) {
this.body = {
message: 'no file',
};
return;
}

const ws = fs.createWriteStream(path.join(this.app.config.logger.dir, 'multipart-test-file'));
part.pipe(ws);
filePart.pipe(ws);
this.body = {
filename: part.filename,
filename: filePart.filename,
fields,
};
};
2 changes: 0 additions & 2 deletions test/fixtures/apps/multipart/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

exports.multipart = {
fileExtensions: ['.foo'],
};
Expand Down
4 changes: 1 addition & 3 deletions test/fixtures/apps/schedule/app/schedule/sub/cron.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

exports.schedule = {
type: 'worker',
cron: '*/5 * * * * *',
};

exports.task = function* (ctx) {
exports.task = async (ctx) => {
ctx.logger.warn('cron wow');
};
2 changes: 0 additions & 2 deletions test/fixtures/apps/schedule/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
'use strict';

exports.keys = 'test key';
7 changes: 3 additions & 4 deletions test/fixtures/apps/watcher-development-app/agent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
const path = require('node:path');

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

module.exports = function(agent) {
let count = 0;
Expand Down
8 changes: 3 additions & 5 deletions test/fixtures/apps/watcher-development-app/app/router.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
const path = require('node:path');

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

module.exports = function(app) {
let fileChangeCount = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

exports.env = 'local';

exports.watcher = {
Expand Down
45 changes: 22 additions & 23 deletions test/lib/core/messenger/local.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { strict as assert } from 'node:assert';
import { scheduler } from 'node:timers/promises';
import { mm } from '@eggjs/mock';
import { pending } from 'pedding';
import { singleProcessApp, MockApplication } from '../../../utils.js';
Expand All @@ -22,11 +21,11 @@ describe('test/lib/core/messenger/local.test.ts', () => {
describe('broadcast()', () => {
it('app.messenger.broadcast should work', done => {
done = pending(2, done);
app.messenger.once('broadcast-event', msg => {
app.messenger.once('broadcast-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
app.agent.messenger.once('broadcast-event', msg => {
app.agent.messenger.once('broadcast-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
Expand All @@ -36,11 +35,11 @@ describe('test/lib/core/messenger/local.test.ts', () => {

it('agent.messenger.broadcast should work', done => {
done = pending(2, done);
app.messenger.once('broadcast-event', msg => {
app.messenger.once('broadcast-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
app.agent.messenger.once('broadcast-event', msg => {
app.agent.messenger.once('broadcast-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
Expand All @@ -55,7 +54,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
throw new Error('should not emit on agent');
});

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

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

describe('sendToAgent()', () => {
it('app.messenger.sendToAgent should work', done => {
app.agent.messenger.once('sendToAgent-event', msg => {
app.agent.messenger.once('sendToAgent-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
Expand All @@ -92,7 +91,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
});

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

describe('sendRandom()', () => {
it('app.messenger.sendRandom should work', done => {
app.agent.messenger.once('sendRandom-event', msg => {
app.agent.messenger.once('sendRandom-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
Expand All @@ -124,7 +123,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
throw new Error('should not emit on agent');
});

app.messenger.once('sendRandom-event', msg => {
app.messenger.once('sendRandom-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
Expand All @@ -136,11 +135,11 @@ describe('test/lib/core/messenger/local.test.ts', () => {
describe('sendTo(pid)', () => {
it('app.messenger.sendTo should work', done => {
done = pending(2, done);
app.messenger.once('sendTo-event', msg => {
app.messenger.once('sendTo-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
app.agent.messenger.once('sendTo-event', msg => {
app.agent.messenger.once('sendTo-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
Expand All @@ -153,12 +152,12 @@ describe('test/lib/core/messenger/local.test.ts', () => {
});

it('agent.messenger.sendTo should work', done => {
done = pedding(done, 2);
app.messenger.once('sendTo-event', msg => {
done = pending(done, 2);
app.messenger.once('sendTo-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
app.agent.messenger.once('sendTo-event', msg => {
app.agent.messenger.once('sendTo-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
Expand All @@ -174,7 +173,7 @@ describe('test/lib/core/messenger/local.test.ts', () => {
});

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

app.messenger.once('send-event', msg => {
app.messenger.once('send-event', (msg: unknown) => {
assert.deepEqual(msg, { foo: 'bar' });
done();
});
Expand All @@ -200,16 +199,16 @@ describe('test/lib/core/messenger/local.test.ts', () => {
});
});

describe('_onMessage()', () => {
describe('onMessage()', () => {
it('should ignore if message format error', () => {
app.messenger._onMessage();
app.messenger._onMessage('foo');
app.messenger._onMessage({ action: 1 });
app.messenger.onMessage();
app.messenger.onMessage('foo');
app.messenger.onMessage({ action: 1 });
});

it('should emit with action', done => {
app.messenger.once('test-action', done);
app.messenger._onMessage({ action: 'test-action' });
app.messenger.onMessage({ action: 'test-action' });
});
});
});
15 changes: 6 additions & 9 deletions test/lib/plugins/depd.test.js → test/lib/plugins/depd.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'use strict';
import { strict as assert } from 'node:assert';
import { mm } from '@eggjs/mock';
import { MockApplication, createApp } from '../../utils.js';

const assert = require('assert');

const mm = require('egg-mock');
const utils = require('../../utils');

describe('test/lib/plugins/depd.test.js', () => {
describe('test/lib/plugins/depd.test.ts', () => {
afterEach(mm.restore);

let app;
let app: MockApplication;
before(() => {
app = utils.app('apps/demo');
app = createApp('apps/demo');
return app.ready();
});
after(() => app.close());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
const fs = require('fs');
const path = require('path');
const mm = require('egg-mock');
const utils = require('../../utils');
import path from 'node:path';
import fs from 'node:fs';
import { mm } from '@eggjs/mock';
import { MockApplication, createApp, cluster, getFilepath } from '../../utils.js';

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

describe('development app', () => {
let app;
let app: MockApplication;
before(() => {
mm.env('local');
mm(process.env, 'EGG_LOG', 'none');
app = utils.app('apps/development');
app = createApp('apps/development');
return app.ready();
});
after(() => app.close());

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

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

before(() => {
mm.env('local');
app = utils.cluster('apps/reload-worker');
app.debug();
app = cluster('apps/reload-worker');
// app.debug();
app.coverage(false);
return app.ready();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
const utils = require('../../utils');
import { MockApplication, createApp } from '../../utils.js';

describe('test/lib/plugins/i18n.test.js', () => {
let app;
describe('test/lib/plugins/i18n.test.ts', () => {
let app: MockApplication;
before(() => {
app = utils.app('apps/i18n');
app = createApp('apps/i18n');
return app.ready();
});
after(() => app.close());
Expand Down
29 changes: 0 additions & 29 deletions test/lib/plugins/logrotator.test.js

This file was deleted.

Loading

0 comments on commit f058c21

Please sign in to comment.