From 78141e83a83f69f769470a100688c415a0ae1070 Mon Sep 17 00:00:00 2001 From: killa Date: Fri, 4 Nov 2022 22:35:51 +0800 Subject: [PATCH] feat: impl parallel for mocha parallel mode (#185) - set ENABLE_MOCHA_PARALLEL/AUTO_AGENT to true - require egg-mock/lib/parallel/agent_register for mocha --- .github/workflows/nodejs.yml | 4 +-- .gitignore | 2 ++ README.md | 3 ++ bin/ets.js | 0 lib/cmd/test.js | 31 ++++++++++++++++++- package.json | 10 +++++- test/fixtures/test-demo-app/app/router.js | 9 ++++++ .../test-demo-app/config/config.default.js | 3 ++ test/fixtures/test-demo-app/node_modules/egg | 1 + test/fixtures/test-demo-app/package.json | 3 ++ test/fixtures/test-demo-app/test/a.test.js | 12 +++++++ test/lib/cmd/test.test.js | 10 ++++++ 12 files changed, 84 insertions(+), 4 deletions(-) mode change 100644 => 100755 bin/ets.js create mode 100644 test/fixtures/test-demo-app/app/router.js create mode 100644 test/fixtures/test-demo-app/config/config.default.js create mode 120000 test/fixtures/test-demo-app/node_modules/egg create mode 100644 test/fixtures/test-demo-app/package.json create mode 100644 test/fixtures/test-demo-app/test/a.test.js diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index a030a61a..0f8d7819 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -30,7 +30,7 @@ jobs: uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v1 + uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} @@ -41,6 +41,6 @@ jobs: run: npm run ci - name: Code Coverage - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.gitignore b/.gitignore index f25a1d80..f673d872 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ test/fixtures/example-ts-ets/typings/ !test/fixtures/example-ts-ets/node_modules/ !test/fixtures/example-ts-simple/node_modules/ !test/fixtures/test-files/node_modules/ +!test/fixtures/test-demo-app/node_modules/ **/run/*.json .tmp @@ -28,3 +29,4 @@ package-lock.json .nyc_output yarn.lock .c8_output +.idea diff --git a/README.md b/README.md index f839a9df..a425ca78 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,9 @@ You can pass any mocha argv. - `--changed` / `-c` only test changed test files(test files means files that match `${pwd}/test/**/*.test.(js|ts)`) - `--dry-run` / `-d` whether dry-run the test command, just show the command - `--espower` / `-e` whether auto require intelli-espower-loader(js) or espower-typescript(ts) for power-assert, default to `true`. +- `--parallel` enable mocha parallel mode, default to `false`. +- `--auto-agent` auto start agent in mocha master agent. +- `--jobs` number of jobs to run in parallel, default to `os.cpus().length - 1`. - see more at #### environment diff --git a/bin/ets.js b/bin/ets.js old mode 100644 new mode 100755 diff --git a/lib/cmd/test.js b/lib/cmd/test.js index 08ed397a..fa37544a 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -6,6 +6,7 @@ const path = require('path'); const globby = require('globby'); const Command = require('../command'); const { getChangedTestFiles } = require('../utils'); +const os = require('os'); class TestCommand extends Command { constructor(rawArgv) { @@ -45,6 +46,22 @@ class TestCommand extends Command { default: true, alias: 'e', }, + parallel: { + type: 'boolean', + description: 'mocha parallel mode', + default: false, + alias: 'p', + }, + 'auto-agent': { + description: 'auto bootstrap agent in mocha master process', + type: 'boolean', + default: true, + }, + jobs: { + description: 'number of jobs to run in parallel', + type: 'number', + default: os.cpus().length - 1, + }, }; } @@ -69,6 +86,12 @@ class TestCommand extends Command { console.log(testArgs.join('\n')); return; } + if (context.argv.parallel) { + context.env.ENABLE_MOCHA_PARALLEL = 'true'; + } + if (context.argv.parallel && context.argv['auto-agent']) { + context.env.AUTO_AGENT = 'true'; + } debug('run test: %s %s', mochaFile, testArgs.join(' ')); await this.helper.forkNode(mochaFile, testArgs, opt); @@ -118,7 +141,13 @@ class TestCommand extends Command { requireArr.push(require.resolve('intelli-espower-loader')); } } - + if (testArgv.parallel && testArgv['auto-agent']) { + try { + requireArr.push(require.resolve('egg-mock/lib/parallel/agent_register')); + } catch (_) { + console.warn('Please install egg-mock, or can not use auto agent'); + } + } testArgv.require = requireArr; let pattern; diff --git a/package.json b/package.json index d54bd966..13915dcf 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,14 @@ "ts-node": "^10.8.0", "ypkgfiles": "^1.6.0" }, + "peerDependencies": { + "egg-mock": "^5.0.2" + }, + "peerDependenciesMeta": { + "egg-mock": { + "optional": true + } + }, "devDependencies": { "@types/mocha": "^9.1.1", "autod": "^3.1.2", @@ -49,7 +57,7 @@ "cross-env": "^3.1.3", "egg": "^2.35.0", "egg-ci": "^2.1.0", - "egg-mock": "^4.2.1", + "egg-mock": "^5.0.2", "enzyme": "^2.0.0", "esbuild-register": "^2.5.0", "eslint": "^8.16.0", diff --git a/test/fixtures/test-demo-app/app/router.js b/test/fixtures/test-demo-app/app/router.js new file mode 100644 index 00000000..a5729403 --- /dev/null +++ b/test/fixtures/test-demo-app/app/router.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = function(app) { + app.get('/', function* () { + this.body = { + fooPlugin: app.fooPlugin, + }; + }); +}; diff --git a/test/fixtures/test-demo-app/config/config.default.js b/test/fixtures/test-demo-app/config/config.default.js new file mode 100644 index 00000000..652fa000 --- /dev/null +++ b/test/fixtures/test-demo-app/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.keys = '123'; diff --git a/test/fixtures/test-demo-app/node_modules/egg b/test/fixtures/test-demo-app/node_modules/egg new file mode 120000 index 00000000..56f30152 --- /dev/null +++ b/test/fixtures/test-demo-app/node_modules/egg @@ -0,0 +1 @@ +../../../../node_modules/egg \ No newline at end of file diff --git a/test/fixtures/test-demo-app/package.json b/test/fixtures/test-demo-app/package.json new file mode 100644 index 00000000..48517648 --- /dev/null +++ b/test/fixtures/test-demo-app/package.json @@ -0,0 +1,3 @@ +{ + "name": "demo-app" +} diff --git a/test/fixtures/test-demo-app/test/a.test.js b/test/fixtures/test-demo-app/test/a.test.js new file mode 100644 index 00000000..1fb9aead --- /dev/null +++ b/test/fixtures/test-demo-app/test/a.test.js @@ -0,0 +1,12 @@ +'use strict'; + +const { app } = require('egg-mock/bootstrap'); + +describe('a.test.js', () => { + it('should work', async () => { + await app.httpRequest() + .get('/') + .expect(200) + .expect({}); + }); +}); diff --git a/test/lib/cmd/test.test.js b/test/lib/cmd/test.test.js index 7824bfb6..d0784800 100644 --- a/test/lib/cmd/test.test.js +++ b/test/lib/cmd/test.test.js @@ -333,4 +333,14 @@ describe('test/lib/cmd/test.test.js', () => { .end(done); }); }); + + it('test parallel', done => { + mm(process.env, 'TESTS', 'test/**/*.test.js'); + coffee.fork(eggBin, [ 'test', '--parallel' ], { cwd: path.join(__dirname, '../../fixtures/test-demo-app') }) + // .debug() + .expect('stdout', /should work/) + .expect('stdout', /a\.test\.js/) + .expect('code', 0) + .end(done); + }); });