Skip to content

Commit

Permalink
feat: impl parallel for mocha parallel mode (#185)
Browse files Browse the repository at this point in the history
- set ENABLE_MOCHA_PARALLEL/AUTO_AGENT to true
- require egg-mock/lib/parallel/agent_register for mocha
  • Loading branch information
killagu authored Nov 4, 2022
1 parent fd275a3 commit 78141e8
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand All @@ -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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,3 +29,4 @@ package-lock.json
.nyc_output
yarn.lock
.c8_output
.idea
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://mochajs.org/#usage>

#### environment
Expand Down
Empty file modified bin/ets.js
100644 → 100755
Empty file.
31 changes: 30 additions & 1 deletion lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
},
};
}

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/test-demo-app/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(app) {
app.get('/', function* () {
this.body = {
fooPlugin: app.fooPlugin,
};
});
};
3 changes: 3 additions & 0 deletions test/fixtures/test-demo-app/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = '123';
1 change: 1 addition & 0 deletions test/fixtures/test-demo-app/node_modules/egg

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

3 changes: 3 additions & 0 deletions test/fixtures/test-demo-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "demo-app"
}
12 changes: 12 additions & 0 deletions test/fixtures/test-demo-app/test/a.test.js
Original file line number Diff line number Diff line change
@@ -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({});
});
});
10 changes: 10 additions & 0 deletions test/lib/cmd/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 78141e8

Please sign in to comment.