From 9f2abf08edc1870f6d27136414a7277a284b211d Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 22 Dec 2024 22:30:06 +0800 Subject: [PATCH] cluster test --- .gitignore | 1 + src/index.ts | 2 +- src/lib/format_options.ts | 2 +- test/app.test.ts | 2 +- test/{cluster.test.js => cluster.test.ts} | 125 ++++++++++++------ .../plugin/a/app/extend/application.js | 2 - test/fixtures/bar/config/config.default.js | 2 - test/fixtures/chair/index.js | 10 +- .../yadan_app/config/config.default.js | 2 - .../yadan_app/node_modules/yadan/index.js | 1 + ...options.test.js => format_options.test.ts} | 55 +++++--- test/{mm.test.js => mm.test.ts} | 35 +++-- 12 files changed, 145 insertions(+), 94 deletions(-) rename test/{cluster.test.js => cluster.test.ts} (71%) rename test/{format_options.test.js => format_options.test.ts} (72%) rename test/{mm.test.js => mm.test.ts} (82%) diff --git a/.gitignore b/.gitignore index 89bd3e6..69e4d28 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ run/ .idea .nyc_output package-lock.json +.package-lock.json .tshy* .eslintcache dist diff --git a/src/index.ts b/src/index.ts index 254211b..7b94d7e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,7 +62,7 @@ const mock = { _mock(process.env, 'EGG_LOG', level); }, - home(homePath: string) { + home(homePath?: string) { if (homePath) { _mock(process.env, 'EGG_HOME', homePath); } diff --git a/src/lib/format_options.ts b/src/lib/format_options.ts index 438b176..ddaa0dc 100644 --- a/src/lib/format_options.ts +++ b/src/lib/format_options.ts @@ -41,7 +41,7 @@ export function formatOptions(initOptions?: MockOptions) { // it will throw when framework is not found framework = getFrameworkPath({ framework, baseDir: options.baseDir }); } - options.framework = framework; + options.framework = options.customEgg = framework; const plugins = options.plugins = options.plugins || {}; diff --git a/test/app.test.ts b/test/app.test.ts index 704e92c..b76f264 100644 --- a/test/app.test.ts +++ b/test/app.test.ts @@ -175,7 +175,7 @@ function call(method: string) { }); }); - it.only('should app.expectLog(), app.notExpectLog() work', async () => { + it('should app.expectLog(), app.notExpectLog() work', async () => { await app.httpRequest() .get('/logger') .expect(200) diff --git a/test/cluster.test.js b/test/cluster.test.ts similarity index 71% rename from test/cluster.test.js rename to test/cluster.test.ts index edff1d6..f986e2a 100644 --- a/test/cluster.test.js +++ b/test/cluster.test.ts @@ -1,21 +1,15 @@ -'use strict'; - -const path = require('path'); -const assert = require('assert'); -const request = require('supertest'); -const mm = require('..'); - -const fixtures = path.join(__dirname, 'fixtures'); - -describe('test/cluster.test.js', () => { +import { strict as assert } from 'node:assert'; +import { getFixtures } from './helper.js'; +import mm, { MockApplication } from '../src/index.js'; +describe('test/cluster.test.ts', () => { afterEach(mm.restore); describe('normal', () => { - let app; + let app: MockApplication; before(() => { app = mm.cluster({ - baseDir: path.join(fixtures, 'demo'), + baseDir: getFixtures('demo'), cache: false, coverage: false, }); @@ -25,8 +19,8 @@ describe('test/cluster.test.js', () => { after(() => app.close()); it('should have members', async () => { - assert(app.callback() === app); - assert(app.listen() === app); + assert.equal(app.callback(), app); + assert.equal(app.listen(), app); await app.ready(); assert(app.process); }); @@ -43,10 +37,10 @@ describe('test/cluster.test.js', () => { }); describe('cluster with fullpath baseDir', () => { - let app; + let app: MockApplication; before(done => { app = mm.cluster({ - baseDir: path.join(fixtures, 'demo'), + baseDir: getFixtures('demo'), cache: false, coverage: false, }); @@ -55,7 +49,7 @@ describe('test/cluster.test.js', () => { after(() => app.close()); it('should work', done => { - request(app.callback()) + app.httpRequest() .get('/hello') .expect('hi') .expect(200, done); @@ -63,7 +57,7 @@ describe('test/cluster.test.js', () => { }); describe('cluster with shortpath baseDir', () => { - let app; + let app: MockApplication; before(done => { app = mm.cluster({ baseDir: 'demo', @@ -75,7 +69,7 @@ describe('test/cluster.test.js', () => { after(() => app.close()); it('should work', done => { - request(app.callback()) + app.httpRequest() .get('/hello') .expect('hi') .expect(200, done); @@ -83,11 +77,35 @@ describe('test/cluster.test.js', () => { }); describe('cluster with customEgg=string', () => { - let app; + let app: MockApplication; + before(done => { + app = mm.cluster({ + baseDir: 'apps/barapp', + customEgg: getFixtures('bar'), + cache: false, + coverage: false, + }); + app.ready(done); + }); + after(() => app.close()); + + it('should work', done => { + app.httpRequest() + .get('/') + .expect({ + foo: 'bar', + foobar: 'bar', + }) + .expect(200, done); + }); + }); + + describe('cluster with framework=string', () => { + let app: MockApplication; before(done => { app = mm.cluster({ baseDir: 'apps/barapp', - customEgg: path.join(fixtures, 'bar'), + framework: getFixtures('bar'), cache: false, coverage: false, }); @@ -96,7 +114,7 @@ describe('test/cluster.test.js', () => { after(() => app.close()); it('should work', done => { - request(app.callback()) + app.httpRequest() .get('/') .expect({ foo: 'bar', @@ -107,13 +125,13 @@ describe('test/cluster.test.js', () => { }); describe('cluster with customEgg=true', () => { - let app; + let app: MockApplication; before(done => { mm(process, 'cwd', () => { - return path.join(fixtures, 'bar'); + return getFixtures('bar'); }); app = mm.cluster({ - baseDir: path.join(fixtures, 'apps/barapp'), + baseDir: getFixtures('apps/barapp'), customEgg: true, cache: false, coverage: false, @@ -123,7 +141,34 @@ describe('test/cluster.test.js', () => { after(() => app.close()); it('should work', done => { - request(app.callback()) + app.httpRequest() + .get('/') + .expect({ + foo: 'bar', + foobar: 'bar', + }) + .expect(200, done); + }); + }); + + describe('cluster with framework=true', () => { + let app: MockApplication; + before(done => { + mm(process, 'cwd', () => { + return getFixtures('bar'); + }); + app = mm.cluster({ + baseDir: getFixtures('apps/barapp'), + framework: true, + cache: false, + coverage: false, + }); + app.ready(done); + }); + after(() => app.close()); + + it('should work', done => { + app.httpRequest() .get('/') .expect({ foo: 'bar', @@ -134,8 +179,8 @@ describe('test/cluster.test.js', () => { }); describe('cluster with cache', () => { - let app1; - let app2; + let app1: MockApplication; + let app2: MockApplication; afterEach(() => { const promises = []; app1 && promises.push(app1.close()); @@ -179,17 +224,17 @@ describe('test/cluster.test.js', () => { }); describe('cluster with eggPath', () => { - let app; + let app: MockApplication; after(() => app.close()); it('should get eggPath', async () => { app = mm.cluster({ baseDir: 'demo', - customEgg: path.join(__dirname, 'fixtures/chair'), + customEgg: getFixtures('chair'), eggPath: '/path/to/eggPath', cache: false, coverage: false, - }); + } as any); await app .debug() .expect('stdout', /\/path\/to\/eggPath/) @@ -198,13 +243,13 @@ describe('test/cluster.test.js', () => { }); describe('cluster with workers', () => { - let app; + let app: MockApplication; after(() => app.close()); it('should get 2 workers', async () => { app = mm.cluster({ baseDir: 'demo', - customEgg: path.join(__dirname, 'fixtures/chair'), + customEgg: getFixtures('chair'), workers: 2, cache: false, coverage: false, @@ -217,13 +262,13 @@ describe('test/cluster.test.js', () => { }); describe('cluster with opts.customEgg', () => { - let app; + let app: MockApplication; after(() => app.close()); it('should pass execArgv', async () => { app = mm.cluster({ baseDir: 'custom_egg', - customEgg: path.join(__dirname, 'fixtures/bar'), + customEgg: getFixtures('bar'), workers: 1, cache: false, coverage: false, @@ -239,7 +284,7 @@ describe('test/cluster.test.js', () => { }); describe('cluster with egg.framework=yadan', () => { - let app; + let app: MockApplication; after(() => app.close()); it('should pass execArgv', async () => { @@ -254,8 +299,8 @@ describe('test/cluster.test.js', () => { }); }); - describe('prerequire', () => { - let app; + describe.skip('prerequire', () => { + let app: MockApplication; after(() => app.close()); it('should load files', async () => { @@ -275,12 +320,12 @@ describe('test/cluster.test.js', () => { }); describe('custom port', () => { - let app; + let app: MockApplication; after(() => app.close()); it('should use it', async () => { app = mm.cluster({ - baseDir: path.join(fixtures, 'demo'), + baseDir: getFixtures('demo'), cache: false, coverage: false, port: 5566, diff --git a/test/fixtures/apps/no-framework/plugin/a/app/extend/application.js b/test/fixtures/apps/no-framework/plugin/a/app/extend/application.js index 0221413..1e5f6fe 100644 --- a/test/fixtures/apps/no-framework/plugin/a/app/extend/application.js +++ b/test/fixtures/apps/no-framework/plugin/a/app/extend/application.js @@ -1,5 +1,3 @@ -'use strict'; - module.exports = { mockEnv() { this.config.env = 'mocked by plugin'; diff --git a/test/fixtures/bar/config/config.default.js b/test/fixtures/bar/config/config.default.js index d7933b0..8b642a7 100644 --- a/test/fixtures/bar/config/config.default.js +++ b/test/fixtures/bar/config/config.default.js @@ -1,5 +1,3 @@ -'use strict'; - exports.foobar = 'bar'; exports.keys = '123'; diff --git a/test/fixtures/chair/index.js b/test/fixtures/chair/index.js index 67c4cdd..7059896 100644 --- a/test/fixtures/chair/index.js +++ b/test/fixtures/chair/index.js @@ -1,12 +1,12 @@ -'use strict'; - const egg = require('egg'); -function startCluster(options) { +async function startCluster(options) { // print for the testcase that will assert stdout console.log(options.eggPath); delete options.eggPath; - egg.startCluster(options); + await egg.startCluster(options); } -Object.assign(exports, egg, { startCluster }); +exports.startCluster = startCluster; +exports.Application = egg.Application; +exports.Agent = egg.Agent; diff --git a/test/fixtures/yadan_app/config/config.default.js b/test/fixtures/yadan_app/config/config.default.js index 652fa00..440624c 100644 --- a/test/fixtures/yadan_app/config/config.default.js +++ b/test/fixtures/yadan_app/config/config.default.js @@ -1,3 +1 @@ -'use strict'; - exports.keys = '123'; diff --git a/test/fixtures/yadan_app/node_modules/yadan/index.js b/test/fixtures/yadan_app/node_modules/yadan/index.js index d394909..c99f09b 100644 --- a/test/fixtures/yadan_app/node_modules/yadan/index.js +++ b/test/fixtures/yadan_app/node_modules/yadan/index.js @@ -10,3 +10,4 @@ class YadanApplication extends egg.Application { exports.Agent = egg.Agent; exports.Application = YadanApplication; +exports.startCluster = egg.startCluster; diff --git a/test/format_options.test.js b/test/format_options.test.ts similarity index 72% rename from test/format_options.test.js rename to test/format_options.test.ts index 9ae2cf8..a541338 100644 --- a/test/format_options.test.js +++ b/test/format_options.test.ts @@ -1,11 +1,11 @@ -'use strict'; - -const path = require('path'); -const assert = require('assert'); -const mm = require('..'); -const formatOptions = require('../lib/format_options'); - -describe('test/format_options.test.js', () => { +import { strict as assert } from 'node:assert'; +import path from 'node:path'; +import mm from '../src/index.js'; +import { formatOptions } from '../src/lib/format_options.js'; +import { getSourceDirname } from '../src/lib/utils.js'; +import { getFixtures } from './helper.js'; + +describe('test/format_options.test.ts', () => { afterEach(mm.restore); it('should return the default options', () => { @@ -16,7 +16,7 @@ describe('test/format_options.test.js', () => { assert(options.baseDir === process.cwd()); assert.deepEqual(options.plugins['egg-mock'], { enable: true, - path: path.join(__dirname, '..'), + path: path.join(getSourceDirname(), '..'), }); }); @@ -27,7 +27,7 @@ describe('test/format_options.test.js', () => { try { formatOptions(); throw new Error('should not run'); - } catch (err) { + } catch (err: any) { assert(/D:[\\|\/]projectWorkSpace[\\|\/]summer/.test(err.message)); } }); @@ -52,7 +52,7 @@ describe('test/format_options.test.js', () => { }); it('should return options when set full baseDir', () => { - const baseDir = path.join(__dirname, 'fixtures/app'); + const baseDir = getFixtures('app'); const options = formatOptions({ baseDir }); assert(options); assert(options.baseDir === baseDir); @@ -61,28 +61,41 @@ describe('test/format_options.test.js', () => { it('should return options when set short baseDir', () => { const options = formatOptions({ baseDir: 'apps/foo' }); assert(options); - assert(options.baseDir === path.join(__dirname, 'fixtures/apps/foo')); + assert(options.baseDir === getFixtures('apps/foo')); }); it('should return options when set customEgg', () => { - const customEgg = path.join(__dirname, 'fixtures/bar'); + const customEgg = getFixtures('bar'); const options = formatOptions({ customEgg }); assert(options); assert(options.customEgg === customEgg); + assert(options.framework === customEgg); }); it('should return options when set customEgg=true', () => { - const baseDir = path.join(__dirname, 'fixtures/bar'); + const baseDir = getFixtures('bar'); mm(process, 'cwd', () => { return baseDir; }); const options = formatOptions({ customEgg: true }); assert(options); - assert(options.customEgg === baseDir); + assert.equal(options.framework, baseDir); + assert.equal(options.customEgg, baseDir); + }); + + it('should return options when set framework=true', () => { + const baseDir = getFixtures('bar'); + mm(process, 'cwd', () => { + return baseDir; + }); + const options = formatOptions({ framework: true }); + assert(options); + assert.equal(options.framework, baseDir); + assert.equal(options.customEgg, baseDir); }); it('should push plugins when in plugin dir', () => { - const baseDir = path.join(__dirname, 'fixtures/plugin'); + const baseDir = getFixtures('plugin'); mm(process, 'cwd', () => { return baseDir; }); @@ -94,8 +107,8 @@ describe('test/format_options.test.js', () => { }); }); - it('should not push pluings when in plugin dir but options.plugin = false', () => { - const baseDir = path.join(__dirname, 'fixtures/plugin'); + it('should not push plugins when in plugin dir but options.plugin = false', () => { + const baseDir = getFixtures('plugin'); mm(process, 'cwd', () => { return baseDir; }); @@ -107,7 +120,7 @@ describe('test/format_options.test.js', () => { }); it('should not throw when no eggPlugin', () => { - const baseDir = path.join(__dirname, 'fixtures/plugin_throw'); + const baseDir = getFixtures('plugin_throw'); mm(process, 'cwd', () => { return baseDir; }); @@ -115,7 +128,7 @@ describe('test/format_options.test.js', () => { }); it('should throw when no eggPlugin and options.plugin === true', () => { - const baseDir = path.join(__dirname, 'fixtures/plugin_throw'); + const baseDir = getFixtures('plugin_throw'); mm(process, 'cwd', () => { return baseDir; }); @@ -123,7 +136,7 @@ describe('test/format_options.test.js', () => { formatOptions({ plugin: true, }); - }, new RegExp(`should set eggPlugin in ${baseDir}/package.json`)); + }, new RegExp(`should set "eggPlugin" property in ${baseDir}/package.json`)); }); it('should mock process.env.HOME when EGG_SERVER_ENV is default, test, prod', () => { diff --git a/test/mm.test.js b/test/mm.test.ts similarity index 82% rename from test/mm.test.js rename to test/mm.test.ts index 3eaf1f9..b49e8a6 100644 --- a/test/mm.test.js +++ b/test/mm.test.ts @@ -1,19 +1,16 @@ -'use strict'; +import { strict as assert } from 'node:assert'; +import path from 'node:path'; +import fs from 'node:fs'; +import mm, { MockApplication } from '../src/index.js'; +import { getFixtures } from './helper.js'; -const path = require('path'); -const fs = require('fs'); -const assert = require('assert'); -const mm = require('..'); - -const baseDir = path.join(__dirname, 'fixtures/apps/env-app'); - -describe('test/mm.test.js', () => { +const baseDir = getFixtures('apps/env-app'); +describe('test/mm.test.ts', () => { afterEach(mm.restore); describe('mm.env()', () => { - - let app; + let app: MockApplication; beforeEach(() => { mm(process.env, 'EGG_HOME', baseDir); }); @@ -68,31 +65,31 @@ describe('test/mm.test.js', () => { }); describe('mm.app({ clean: false })', () => { - let app; + let app: MockApplication; after(() => app.close()); it('keep log dir', async () => { app = mm.app({ baseDir: 'apps/app-not-clean', clean: false }); await app.ready(); - assert(fs.existsSync(path.join(__dirname, 'fixtures/apps/app-not-clean/logs/keep'))); + assert(fs.existsSync(getFixtures('apps/app-not-clean/logs/keep'))); }); }); describe('mm.consoleLevel()', () => { - it('shoud mock EGG_LOG', () => { + it('should mock EGG_LOG', () => { mm.consoleLevel('none'); assert(process.env.EGG_LOG === 'NONE'); }); - it('shoud not mock', () => { + it('should not mock', () => { mm.consoleLevel(''); assert(!process.env.EGG_LOG); }); }); describe('mm.home', () => { - let app; - const baseDir = path.join(__dirname, 'fixtures/apps/mockhome'); + let app: MockApplication; + const baseDir = getFixtures('apps/mockhome'); before(() => { mm.home(baseDir); app = mm.app({ baseDir: 'apps/mockhome', clean: false }); @@ -111,7 +108,7 @@ describe('test/mm.test.js', () => { }); describe('egg-mock', () => { - let app; + let app: MockApplication; before(() => { app = mm.app({ baseDir: 'apps/no-framework', @@ -121,7 +118,7 @@ describe('test/mm.test.js', () => { after(() => app.close()); it('should not be a framework', () => { - app.mockEnv(); + app.mockEnv('prod test not work'); assert(app.config.env === 'mocked by plugin'); }); });