Skip to content

Commit

Permalink
📦 NEW: Support mochawesome reporter (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Nov 19, 2022
1 parent 5fccb6e commit 3c326b3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test/fixtures/example-ts-ets/typings/
!test/fixtures/test-files/node_modules/
!test/fixtures/test-demo-app/node_modules/

.mochawesome-reports
**/run/*.json
.tmp
.vscode
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ You can pass any mocha argv.
- `--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`.
- `--mochawesome` enable [mochawesome](https://github.com/adamgruber/mochawesome) reporter, default to `false`.
- see more at <https://mochajs.org/#usage>

#### environment
Expand Down
5 changes: 0 additions & 5 deletions lib/cmd/cov.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
/* istanbul ignore next */
'use strict';

const debug = require('debug')('egg-bin');
const path = require('path');
const fs = require('fs/promises');
const Command = require('./test');
const { defaultExcludes } = require('../utils');

const EXCLUDES = Symbol('cov#excludes');

/* istanbul ignore next */
class CovCommand extends Command {
constructor(argv) {
super(argv);
Expand Down
16 changes: 16 additions & 0 deletions lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class TestCommand extends Command {
type: 'number',
default: os.cpus().length - 1,
},
mochawesome: {
type: 'boolean',
description: 'enable mochawesome reporter',
default: false,
},
};
}

Expand Down Expand Up @@ -147,6 +152,16 @@ class TestCommand extends Command {
console.warn('Please install egg-mock, or can not use auto agent');
}
}
// handle mochawesome enable
if (!testArgv.reporter && testArgv.mochawesome) {
testArgv.reporter = require.resolve('mochawesome');
testArgv['reporter-options'] = 'reportDir=node_modules/.mochawesome-reports';
if (testArgv.parallel) {
// https://github.com/adamgruber/mochawesome#parallel-mode
requireArr.push(require.resolve('mochawesome/register'));
}
}

testArgv.require = requireArr;

let pattern;
Expand Down Expand Up @@ -198,6 +213,7 @@ class TestCommand extends Command {
testArgv.typescript = undefined;
testArgv['dry-run'] = undefined;
testArgv.dryRun = undefined;
testArgv.mochawesome = undefined;

return this.helper.unparseArgv(testArgv);
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"jest-changed-files": "^28.0.2",
"minimatch": "^5.1.0",
"mocha": "^10.0.0",
"mochawesome": "^7.1.3",
"power-assert": "^1.6.1",
"semver": "^7.3.7",
"source-map-support": "^0.5.21",
Expand Down Expand Up @@ -85,6 +86,7 @@
"pkgfiles": "node bin/egg-bin.js pkgfiles",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "node bin/egg-bin.js test -t 120000 --parallel",
"test-mochawesome": "npm run test-local -- --mochawesome",
"cov": "c8 -r lcov -r text-summary npm run test-local",
"ci-test-only": "npm run test-local -- test/lib/cmd/cov.test.js",
"ci": "npm run lint && npm run pkgfiles -- --check && npm run ci-test-only && npm run test-local",
Expand Down
20 changes: 18 additions & 2 deletions test/lib/cmd/cov.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const fs = require('fs');
const path = require('path');
const assert = require('assert');
Expand Down Expand Up @@ -36,6 +34,24 @@ describe('test/lib/cmd/cov.test.js', () => {
if (!process.env.NYC_ROOT_ID) assertCoverage(cwd);
});

it('should success with --mochawesome', async () => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
const child = coffee.fork(eggBin, [ 'cov', '--mochawesome' ], { cwd })
// .debug()
.expect('stdout', /should success/)
.expect('stdout', /a\.test\.js/)
.expect('stdout', /b[\/|\\]b\.test\.js/)
.expect('stdout', /\[mochawesome] Report JSON saved to/)
.expect('stdout', /node_modules\/\.mochawesome-reports\/mochawesome\.json/)
.notExpect('stdout', /a.js/);

child.expect('stdout', /Statements {3}:/);

await child.expect('code', 0).end();
// only test on npm run test
if (!process.env.NYC_ROOT_ID) assertCoverage(cwd);
});

it('should exit when not test files', () => {
return coffee.fork(eggBin, [ 'cov', 'test/**/*.nth.js' ], { cwd })
// .debug()
Expand Down
14 changes: 14 additions & 0 deletions test/lib/cmd/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ describe('test/lib/cmd/test.test.js', () => {
.end(done);
});

it('should success with --mochawesome', done => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
coffee.fork(eggBin, [ 'test', '--mochawesome' ], { cwd })
// .debug()
.expect('stdout', /should success/)
.expect('stdout', /a\.test\.js/)
.expect('stdout', /b\/b\.test\.js/)
.expect('stdout', /\[mochawesome] Report JSON saved to/)
.expect('stdout', /node_modules\/\.mochawesome-reports\/mochawesome\.json/)
.notExpect('stdout', /\ba\.js/)
.expect('code', 0)
.end(done);
});

it('should ignore node_modules and fixtures', done => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
coffee.fork(eggBin, [ 'test' ], { cwd: path.join(__dirname, '../../fixtures/test-files-glob') })
Expand Down

0 comments on commit 3c326b3

Please sign in to comment.