Skip to content

Commit

Permalink
cluster test
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 22, 2024
1 parent f0197e6 commit 9f2abf0
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ run/
.idea
.nyc_output
package-lock.json
.package-lock.json
.tshy*
.eslintcache
dist
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/format_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};

Expand Down
2 changes: 1 addition & 1 deletion test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
125 changes: 85 additions & 40 deletions test/cluster.test.js → test/cluster.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});
Expand All @@ -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);
});
Expand All @@ -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,
});
Expand All @@ -55,15 +49,15 @@ describe('test/cluster.test.js', () => {
after(() => app.close());

it('should work', done => {
request(app.callback())
app.httpRequest()
.get('/hello')
.expect('hi')
.expect(200, done);
});
});

describe('cluster with shortpath baseDir', () => {
let app;
let app: MockApplication;
before(done => {
app = mm.cluster({
baseDir: 'demo',
Expand All @@ -75,19 +69,43 @@ describe('test/cluster.test.js', () => {
after(() => app.close());

it('should work', done => {
request(app.callback())
app.httpRequest()
.get('/hello')
.expect('hi')
.expect(200, done);
});
});

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,
});
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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());
Expand Down Expand Up @@ -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/)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports = {
mockEnv() {
this.config.env = 'mocked by plugin';
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/bar/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

exports.foobar = 'bar';

exports.keys = '123';
10 changes: 5 additions & 5 deletions test/fixtures/chair/index.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 0 additions & 2 deletions test/fixtures/yadan_app/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
'use strict';

exports.keys = '123';
1 change: 1 addition & 0 deletions test/fixtures/yadan_app/node_modules/yadan/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9f2abf0

Please sign in to comment.