diff --git a/test/lib/application.test.ts b/test/lib/application.test.ts index 47798c87d8..f896e7f380 100644 --- a/test/lib/application.test.ts +++ b/test/lib/application.test.ts @@ -34,7 +34,7 @@ describe('test/lib/application.test.ts', () => { new Application({ baseDir: getFilepath('custom-egg/index.js'), }); - }, /not a directory/); + }, /not a directory|no such file or directory/); }); }); diff --git a/test/lib/core/messenger/ipc.test.js b/test/lib/core/messenger/ipc.test.ts similarity index 78% rename from test/lib/core/messenger/ipc.test.js rename to test/lib/core/messenger/ipc.test.ts index e34812b8de..84b158baea 100644 --- a/test/lib/core/messenger/ipc.test.js +++ b/test/lib/core/messenger/ipc.test.ts @@ -1,13 +1,15 @@ -const assert = require('assert'); -const mm = require('egg-mock'); -const utils = require('../../../utils'); -const Messenger = require('../../../../lib/core/messenger/ipc'); +import { strict as assert } from 'node:assert'; +import { scheduler } from 'node:timers/promises'; +import { mm } from '@eggjs/mock'; +import { cluster, MockApplication } from '../../../utils.js'; +import { Messenger } from '../../../../src/lib/core/messenger/ipc.js'; -describe('test/lib/core/messenger/ipc.test.js', () => { - let messenger; +describe('test/lib/core/messenger/ipc.test.ts', () => { + let messenger: Messenger; + const app: any = {}; before(() => { - messenger = new Messenger(); + messenger = new Messenger(app); }); afterEach(mm.restore); @@ -21,20 +23,20 @@ describe('test/lib/core/messenger/ipc.test.js', () => { done(); }); - process.emit('message', {}); - process.emit('message', null); + process.emit('message', {}, null); + process.emit('message', null, null); process.emit('message', { action: 'messenger-test-on-event', data: { success: true, }, - }); + }, null); }); }); describe('close()', () => { it('should remove all listeners', () => { - const messenger = new Messenger(); + const messenger = new Messenger(app); messenger.on('messenger-test-on-event-2', () => { throw new Error('should never emitted'); }); @@ -46,17 +48,17 @@ describe('test/lib/core/messenger/ipc.test.js', () => { data: { success: true, }, - }); + }, null); }); }); describe('cluster messenger', () => { - let app; + let app: MockApplication; after(() => app.close()); // use it to record create coverage codes time it('before: should start cluster app', async () => { - app = utils.cluster('apps/messenger'); + app = cluster('apps/messenger'); app.coverage(true); await app.ready(); await scheduler.wait(1000); @@ -66,7 +68,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => { app.expect('stdout', /\[app] agent-to-app agent msg/); }); - it('app should accept agent assgin pid message', () => { + it('app should accept agent assign pid message', () => { app.expect('stdout', /\[app] agent-to-app agent msg \d+/); }); @@ -84,14 +86,14 @@ describe('test/lib/core/messenger/ipc.test.js', () => { }); describe('broadcast()', () => { - let app; + let app: MockApplication; before(() => { mm.env('default'); - app = utils.cluster('apps/messenger-broadcast', { workers: 2 }); + app = cluster('apps/messenger-broadcast', { workers: 2 }); app.coverage(false); return app.ready(); }); - before(() => utils.sleep(1000)); + before(() => scheduler.wait(1000)); after(() => app.close()); it('should broadcast each other', () => { @@ -105,15 +107,15 @@ describe('test/lib/core/messenger/ipc.test.js', () => { // agent 26494 receive message from app pid 26496 // agent 26494 receive message from agent pid 26494 const m = app.stdout.match(/(app|agent) \d+ receive message from (app|agent) pid \d+/g); - assert(m.length, 9); + assert.equal(m.length, 9); }); }); describe('sendRandom', () => { - let app; + let app: MockApplication; before(() => { mm.env('default'); - app = utils.cluster('apps/messenger-random', { workers: 4 }); + app = cluster('apps/messenger-random', { workers: 4 }); app.coverage(false); return app.ready(); }); @@ -137,10 +139,10 @@ describe('test/lib/core/messenger/ipc.test.js', () => { }); describe('sendToApp and sentToAgent', () => { - let app; + let app: MockApplication; before(() => { mm.env('default'); - app = utils.cluster('apps/messenger-app-agent', { workers: 2 }); + app = cluster('apps/messenger-app-agent', { workers: 2 }); app.coverage(false); return app.ready(); }); @@ -155,7 +157,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => { done(); }, 500); - function count(data, key) { + function count(data: string, key: string) { return data.split('\n').filter(line => { return line.indexOf(key) >= 0; }).length; @@ -164,10 +166,10 @@ describe('test/lib/core/messenger/ipc.test.js', () => { }); describe('worker_threads mode', () => { - let app; + let app: MockApplication; before(() => { mm.env('default'); - app = utils.cluster('apps/messenger-app-agent', { workers: 1, startMode: 'worker_threads' }); + app = cluster('apps/messenger-app-agent', { workers: 1, startMode: 'worker_threads' }); app.coverage(false); return app.ready(); }); @@ -182,7 +184,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => { done(); }, 500); - function count(data, key) { + function count(data: string, key: string) { return data.split('\n').filter(line => { return line.indexOf(key) >= 0; }).length; diff --git a/test/lib/core/messenger/local.test.js b/test/lib/core/messenger/local.test.ts similarity index 92% rename from test/lib/core/messenger/local.test.js rename to test/lib/core/messenger/local.test.ts index 4bf4ec9bdb..0623a5221a 100644 --- a/test/lib/core/messenger/local.test.js +++ b/test/lib/core/messenger/local.test.ts @@ -1,15 +1,14 @@ -'use strict'; +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'; -const utils = require('../../../utils'); -const pedding = require('pedding'); -const assert = require('assert'); -const mm = require('egg-mock'); - -describe('test/lib/core/messenger/local.test.js', () => { - let app; +describe('test/lib/core/messenger/local.test.ts', () => { + let app: MockApplication; before(async () => { - app = await utils.singleProcessApp('apps/demo'); + app = await singleProcessApp('apps/demo'); }); after(() => app.close()); @@ -22,7 +21,7 @@ describe('test/lib/core/messenger/local.test.js', () => { describe('broadcast()', () => { it('app.messenger.broadcast should work', done => { - done = pedding(done, 2); + done = pending(2, done); app.messenger.once('broadcast-event', msg => { assert.deepEqual(msg, { foo: 'bar' }); done(); @@ -36,7 +35,7 @@ describe('test/lib/core/messenger/local.test.js', () => { }); it('agent.messenger.broadcast should work', done => { - done = pedding(done, 2); + done = pending(2, done); app.messenger.once('broadcast-event', msg => { assert.deepEqual(msg, { foo: 'bar' }); done(); @@ -136,7 +135,7 @@ describe('test/lib/core/messenger/local.test.js', () => { describe('sendTo(pid)', () => { it('app.messenger.sendTo should work', done => { - done = pedding(done, 2); + done = pending(2, done); app.messenger.once('sendTo-event', msg => { assert.deepEqual(msg, { foo: 'bar' }); done(); diff --git a/test/lib/egg.test.ts b/test/lib/egg.test.ts index d6d3723889..1e25804550 100644 --- a/test/lib/egg.test.ts +++ b/test/lib/egg.test.ts @@ -180,6 +180,7 @@ describe('test/lib/egg.test.ts', () => { }); it('should dumpTiming when timeout', async () => { + if (process.platform === 'win32') return; const baseDir = getFilepath('apps/dumptiming-timeout'); fs.rmSync(path.join(baseDir, 'run'), { recursive: true, force: true }); fs.rmSync(path.join(baseDir, 'logs'), { recursive: true, force: true }); @@ -193,6 +194,7 @@ describe('test/lib/egg.test.ts', () => { }); it('should dump slow-boot-action warnning log', async () => { + if (process.platform === 'win32') return; const baseDir = getFilepath('apps/dumptiming-slowBootActionMinDuration'); fs.rmSync(path.join(baseDir, 'run'), { recursive: true, force: true }); fs.rmSync(path.join(baseDir, 'logs'), { recursive: true, force: true }); diff --git a/test/utils.ts b/test/utils.ts index e891d929ff..ac06760b4d 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url'; import { AddressInfo } from 'node:net'; import { scheduler } from 'node:timers/promises'; import { - mm, MockOptions, MockApplication, + mm, MockOptions, MockClusterOptions, MockApplication, } from '@eggjs/mock'; import { Application as Koa } from '@eggjs/koa'; import { request } from '@eggjs/supertest'; @@ -23,7 +23,7 @@ export async function rimraf(target: string) { await rm(target, { force: true, recursive: true }); } -export { MockApplication, MockOptions, mm } from '@eggjs/mock'; +export { MockApplication, MockOptions, MockClusterOptions, mm }; export const restore = () => mm.restore(); export function app(name: string | MockOptions, options?: MockOptions) { @@ -42,7 +42,7 @@ export const createApp = app; * @param {Object} [options] - optional * @return {App} app - Application object. */ -export function cluster(name: string | MockOptions, options?: MockOptions): MockApplication { +export function cluster(name: string | MockClusterOptions, options?: MockClusterOptions): MockApplication { options = formatOptions(name, options); return mm.cluster(options) as unknown as MockApplication; }