From 3c326b3a5280a7272c84a248e387b590468500b7 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 19 Nov 2022 17:35:10 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Support=20mochawesome=20r?= =?UTF-8?q?eporter=20(#192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + README.md | 1 + lib/cmd/cov.js | 5 ----- lib/cmd/test.js | 16 ++++++++++++++++ package.json | 2 ++ test/lib/cmd/cov.test.js | 20 ++++++++++++++++++-- test/lib/cmd/test.test.js | 14 ++++++++++++++ 7 files changed, 52 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index f673d872..9269fce1 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index a425ca78..0b414965 100644 --- a/README.md +++ b/README.md @@ -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 #### environment diff --git a/lib/cmd/cov.js b/lib/cmd/cov.js index ba18e214..f4847939 100644 --- a/lib/cmd/cov.js +++ b/lib/cmd/cov.js @@ -1,6 +1,3 @@ -/* istanbul ignore next */ -'use strict'; - const debug = require('debug')('egg-bin'); const path = require('path'); const fs = require('fs/promises'); @@ -8,8 +5,6 @@ const Command = require('./test'); const { defaultExcludes } = require('../utils'); const EXCLUDES = Symbol('cov#excludes'); - -/* istanbul ignore next */ class CovCommand extends Command { constructor(argv) { super(argv); diff --git a/lib/cmd/test.js b/lib/cmd/test.js index 7b95607c..1c5808cc 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -61,6 +61,11 @@ class TestCommand extends Command { type: 'number', default: os.cpus().length - 1, }, + mochawesome: { + type: 'boolean', + description: 'enable mochawesome reporter', + default: false, + }, }; } @@ -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; @@ -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); } diff --git a/package.json b/package.json index 95f9f281..16833b64 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/test/lib/cmd/cov.test.js b/test/lib/cmd/cov.test.js index ddfc0b21..682e9c98 100644 --- a/test/lib/cmd/cov.test.js +++ b/test/lib/cmd/cov.test.js @@ -1,5 +1,3 @@ -'use strict'; - const fs = require('fs'); const path = require('path'); const assert = require('assert'); @@ -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() diff --git a/test/lib/cmd/test.test.js b/test/lib/cmd/test.test.js index 23dcdfca..155cfc5e 100644 --- a/test/lib/cmd/test.test.js +++ b/test/lib/cmd/test.test.js @@ -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') })