From 1e7c830f918353ce9d33192a6417e392df694e94 Mon Sep 17 00:00:00 2001 From: Kevin Locke Date: Fri, 12 Jun 2020 15:40:12 -0600 Subject: [PATCH] Create mocha-ur2ue root hook plugin module Based on my node-project-template. To work around mochajs/mocha#1926 and mochajs/mocha#2640. Signed-off-by: Kevin Locke --- .travis.yml | 8 +-- README.md | 65 +++++++----------- bin/cmd.js | 139 -------------------------------------- doc-src/spec/footer.xhtml | 45 ------------ doc-src/spec/header.xhtml | 24 ------- index.js | 39 ----------- jsdoc-lint.conf.json | 23 ------- jsdoc.conf.json | 25 ------- package.json | 51 ++++---------- 9 files changed, 38 insertions(+), 381 deletions(-) delete mode 100755 bin/cmd.js delete mode 100644 doc-src/spec/footer.xhtml delete mode 100644 doc-src/spec/header.xhtml delete mode 100644 index.js delete mode 100644 jsdoc-lint.conf.json delete mode 100644 jsdoc.conf.json diff --git a/.travis.yml b/.travis.yml index 35ae4ae..5124688 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,10 +23,4 @@ install: - npm install script: - - npm run test-cov - -# Note: Both codecov and coveralls detect branch/PR info from Travis CI -# https://github.com/codecov/codecov-node/blob/v1.0.1/lib/services/travis.js -# https://github.com/nickmerwin/node-coveralls/blob/2.11.5/lib/getOptions.js#L18 -after_success: - - npm run upload-cov + - npm run test diff --git a/README.md b/README.md index cbc425d..8470e7e 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,40 @@ -Project Template -================ - -[![Build Status: Linux](https://img.shields.io/travis/kevinoid/project-template/master.svg?style=flat&label=build+on+linux)](https://travis-ci.org/kevinoid/project-template) -[![Build Status: Windows](https://img.shields.io/appveyor/ci/kevinoid/project-template/master.svg?style=flat&label=build+on+windows)](https://ci.appveyor.com/project/kevinoid/project-template) -[![Coverage](https://img.shields.io/codecov/c/github/kevinoid/project-template.svg?style=flat)](https://codecov.io/github/kevinoid/project-template?branch=master) -[![Dependency Status](https://img.shields.io/david/kevinoid/project-template.svg?style=flat)](https://david-dm.org/kevinoid/project-template) -[![Supported Node Version](https://img.shields.io/node/v/@kevinoid/project-template.svg?style=flat)](https://www.npmjs.com/package/@kevinoid/project-template) -[![Version on NPM](https://img.shields.io/npm/v/@kevinoid/project-template.svg?style=flat)](https://www.npmjs.com/package/@kevinoid/project-template) - -A Node.js/npm project template with [codecov](https://codecov.io/), -[coveralls](https://coveralls.io/), [ESLint](https://eslint.org/), -[conventional-changelog](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-cli), -[nyc](https://github.com/istanbuljs/nyc), [JSDoc](http://usejsdoc.org/), and -[mocha](https://mochajs.org/). - -It is the template that I am using for my own Node.js projects, which -represents my current preferences. I am not advocating for these choices nor -this template specifically, although I am happy to discuss or explain any -choices made herein. It is being published both for my own convenience and -in case it may be useful to others with similar tastes. +mocha-ur2ue +=========== + +[![Build Status: Linux](https://img.shields.io/travis/kevinoid/mocha-ur2ue/master.svg?style=flat&label=build+on+linux)](https://travis-ci.org/kevinoid/mocha-ur2ue) +[![Build Status: Windows](https://img.shields.io/appveyor/ci/kevinoid/mocha-ur2ue/master.svg?style=flat&label=build+on+windows)](https://ci.appveyor.com/project/kevinoid/mocha-ur2ue) +[![Coverage](https://img.shields.io/codecov/c/github/kevinoid/mocha-ur2ue.svg?style=flat)](https://codecov.io/github/kevinoid/mocha-ur2ue?branch=master) +[![Dependency Status](https://img.shields.io/david/kevinoid/mocha-ur2ue.svg?style=flat)](https://david-dm.org/kevinoid/mocha-ur2ue) +[![Supported Node Version](https://img.shields.io/node/v/mocha-ur2ue.svg?style=flat)](https://www.npmjs.com/package/mocha-ur2ue) +[![Version on NPM](https://img.shields.io/npm/v/mocha-ur2ue.svg?style=flat)](https://www.npmjs.com/package/mocha-ur2ue) + +A [Mocha](https://mochajs.org/) [root hook +plugin](https://mochajs.org/#root-hook-plugins) to convert +[`unhandledrejection` +events](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event) +to unhandled exceptions so that they will cause test failures. This works +around a long-standing issue +([mochajs/mocha#1926](https://github.com/mochajs/mocha/issues/1926) and +[mochajs/mocha#2640](https://github.com/mochajs/mocha/issues/2640)) that +unhandled rejections do not cause test failure in Mocha. ## Introductory Example -```js +```sh +npm install --save-dev mocha mocha-ur2ue +mocha --require mocha-ur2ue --recursive tests ``` -## Features - - ## Installation -[This package](https://www.npmjs.com/package/@kevinoid/project-template) can be +[This package](https://www.npmjs.com/package/mocha-ur2ue) can be installed using [npm](https://www.npmjs.com/), either globally or locally, by running: ```sh -npm install @kevinoid/project-template +npm install mocha-ur2ue ``` -## Recipes - -More examples can be found in the [test -specifications](https://kevinoid.github.io/project-template/spec). - -## API Docs - -To use this module as a library, see the [API -Documentation](https://kevinoid.github.io/project-template/api). - ## Contributing Contributions are appreciated. Contributors agree to abide by the [Contributor @@ -67,7 +54,3 @@ save a lot of time and effort. This project is available under the terms of the [MIT License](LICENSE.txt). See the [summary at TLDRLegal](https://tldrlegal.com/license/mit-license). - -The [template](https://github.com/kevinoid/node-project-template) upon which -this project is based is available under the terms of -[CC0 1.0 Universal](https://creativecommons.org/publicdomain/zero/1.0/). diff --git a/bin/cmd.js b/bin/cmd.js deleted file mode 100755 index 15164fd..0000000 --- a/bin/cmd.js +++ /dev/null @@ -1,139 +0,0 @@ -#!/usr/bin/env node -/** - * An executable command which will be added to $PATH. - * - * @copyright Copyright 2017-2019 Kevin Locke - * @license MIT - */ - -'use strict'; - -const Yargs = require('yargs/yargs'); -const packageJson = require('../package.json'); -const modulename = require('..'); - -/** Options for command entry points. - * - * @typedef {{ - * stdin: !module:stream.Readable, - * stdout: !module:stream.Writable, - * stderr: !module:stream.Writable - * }} CommandOptions - * @property {!module:stream.Readable} stdin Stream from which input is read. - * @property {!module:stream.Writable} stdout Stream to which output is - * written. - * @property {!module:stream.Writable} stderr Stream to which errors and - * non-output status messages are written. - */ -// const CommandOptions; - -/** Entry point for this command. - * - * @param {Array} args Command-line arguments. - * @param {!CommandOptions} options Options. - * @param {function(number)} callback Callback with exit code. - */ -function modulenameCmd(args, options, callback) { - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - - if (args !== undefined - && args !== null - && Math.floor(args.length) !== args.length) { - throw new TypeError('args must be Array-like'); - } - - if (!options || typeof options !== 'object') { - throw new TypeError('options must be an object'); - } - - if (!options.stdin || typeof options.stdin.on !== 'function') { - throw new TypeError('options.stdin must be a stream.Readable'); - } - if (!options.stdout || typeof options.stdout.write !== 'function') { - throw new TypeError('options.stdout must be a stream.Writable'); - } - if (!options.stderr || typeof options.stderr.write !== 'function') { - throw new TypeError('options.stderr must be a stream.Writable'); - } - - if (args.length >= 2) { - // Strip "node" and script name, ensure args are strings - args = Array.prototype.slice.call(args, 2).map(String); - } else { - args = []; - } - - // Workaround for https://github.com/yargs/yargs/issues/783 - // Necessary because mocha package.json overrides .parserConfiguration() - require.main = module; - const yargs = new Yargs(undefined, undefined, require) - .parserConfiguration({ - 'parse-numbers': false, - 'duplicate-arguments-array': false, - 'flatten-duplicate-arrays': false, - 'greedy-arrays': false, - }) - .usage('Usage: $0 [options] [args...]') - .help() - .alias('help', 'h') - .alias('help', '?') - .option('quiet', { - alias: 'q', - describe: 'Print less output', - count: true, - }) - .option('verbose', { - alias: 'v', - describe: 'Print more output', - count: true, - }) - .version(`${packageJson.name} ${packageJson.version}`) - .alias('version', 'V') - .strict(); - yargs.parse(args, (err, argOpts, output) => { - if (err) { - if (output) { - options.stderr.write(`${output}\n`); - } else { - options.stderr.write(`${err.name}: ${err.message}\n`); - } - callback(undefined, 1); - return; - } - - if (output) { - options.stdout.write(`${output}\n`); - } - - if (argOpts.help || argOpts.version) { - callback(0); - return; - } - - if (argOpts._.length !== 1) { - options.stderr.write('Error: Exactly one argument is required.\n'); - callback(1); - return; - } - - // Parse arguments then call API function with parsed options - const cmdOpts = { - files: argOpts._, - verbosity: argOpts.verbose - argOpts.quiet, - }; - modulename(cmdOpts, callback); - }); -} - -modulenameCmd.default = modulenameCmd; -module.exports = modulenameCmd; - -if (require.main === module) { - // This file was invoked directly. - // Note: Could pass process.exit as callback to force immediate exit. - modulenameCmd(process.argv, process, (exitCode) => { - process.exitCode = exitCode; - }); -} diff --git a/doc-src/spec/footer.xhtml b/doc-src/spec/footer.xhtml deleted file mode 100644 index 3d09b3d..0000000 --- a/doc-src/spec/footer.xhtml +++ /dev/null @@ -1,45 +0,0 @@ - - - - - - - - diff --git a/doc-src/spec/header.xhtml b/doc-src/spec/header.xhtml deleted file mode 100644 index 655ab13..0000000 --- a/doc-src/spec/header.xhtml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - Specifications in Mocha - - - - - - - - - -
diff --git a/index.js b/index.js deleted file mode 100644 index d8147f8..0000000 --- a/index.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @copyright Copyright 2016 Kevin Locke - * @license MIT - */ - -'use strict'; - -const modulename = {}; - -modulename.func = function func(options, callback) { - if (!callback && typeof options === 'function') { - callback = options; - options = undefined; - } - - if (!callback) { - return new Promise((resolve, reject) => { - func(options, (err, result) => { - if (err) { reject(err); } else { resolve(result); } - }); - }); - } - - if (typeof callback !== 'function') { - throw new TypeError('callback must be a function'); - } - - if (options !== undefined && typeof options !== 'object') { - process.nextTick(() => { - callback(new TypeError('options must be an object')); - }); - return undefined; - } - - // Do stuff - return undefined; -}; - -module.exports = modulename; diff --git a/jsdoc-lint.conf.json b/jsdoc-lint.conf.json deleted file mode 100644 index ad6b26b..0000000 --- a/jsdoc-lint.conf.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "//": "JSDoc configuration for linting", - "opts": { - "access": "all", - "destination": "doc/api", - "pedantic": true, - "recurse": true - }, - "source": { - "exclude": [ - "coverage", - "doc", - "node_modules" - ] - }, - "tags": { - "allowUnknownTags": false, - "dictionaries": [ - "jsdoc", - "closure" - ] - } -} diff --git a/jsdoc.conf.json b/jsdoc.conf.json deleted file mode 100644 index 1fdc68b..0000000 --- a/jsdoc.conf.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "//": "JSDoc configuration", - "opts": { - "destination": "doc/api", - "recurse": true - }, - "source": { - "include": [ - "README.md" - ], - "exclude": [ - "coverage", - "doc", - "node_modules", - "test" - ] - }, - "tags": { - "allowUnknownTags": false, - "dictionaries": [ - "jsdoc", - "closure" - ] - } -} diff --git a/package.json b/package.json index 5b5cb63..03beee7 100644 --- a/package.json +++ b/package.json @@ -1,52 +1,36 @@ { - "name": "@kevinoid/project-template", + "name": "mocha-ur2ue", "version": "0.0.1", - "description": "A Node.js/npm project template with codecov, coveralls, ESLint, conventional-changelog, nyc, JSDoc, and mocha.", + "description": "Mocha root hook plugin to convert unhandled rejection events to unhandled exceptions so that they will cause test failures.", "keywords": [ - "project-template", - "template" + "mocha", + "mocha-plugin", + "promise" ], "license": "MIT", - "homepage": "https://github.com/kevinoid/node-project-template", - "bugs": "https://github.com/kevinoid/node-project-template/issues", + "homepage": "https://github.com/kevinoid/mocha-ur2ue", + "bugs": "https://github.com/kevinoid/mocha-ur2ue/issues", "author": "Kevin Locke ", "repository": { "type": "git", - "url": "https://github.com/kevinoid/node-project-template.git" + "url": "https://github.com/kevinoid/mocha-ur2ue.git" }, "main": "index.js", - "bin": { - "project-template": "bin/cmd.js" - }, "//": "All scripts should run in POSIX sh and Windows cmd.exe", "scripts": { "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -u", - "clean": "rimraf coverage && rimraf doc", - "doc": "npm run doc-js && npm run doc-spec", - "doc-js": "rimraf doc/api && jsdoc -c jsdoc.conf.json .", - "doc-spec": "rimraf doc/spec && mkdir doc/spec && mocha --reporter doc --recursive test | nodecat doc-src/spec/header.xhtml - doc-src/spec/footer.xhtml > doc/spec/index.xhtml", - "lint": "npm run lint-js && npm run lint-doc", - "lint-doc": "jsdoc -t templates/silent -c jsdoc-lint.conf.json . && echo JSDoc passed.", + "lint": "npm run lint-js", "lint-js": "eslint --report-unused-disable-directives . && echo ESLint passed.", - "postpublish": "git -C doc push && git push --follow-tags origin master gh-pages && echo Remember to update GitHub Releases from CHANGELOG.md", - "postversion": "rimraf doc && git clone -b gh-pages -l -q . doc && npm run doc && git -C doc add . && git -C doc commit -n -m \"Docs for v$npm_package_version\"", - "preversion": "npm run test-cov && nyc check-coverage --statements 95 && depcheck --ignore-dirs doc && david && git-branch-is master && travis-status -b master -c -w -x && appveyor-status -b master -c -w -p kevinoid/node-project-template", - "start": "supervisor --quiet --no-restart-on exit --extensions js,json --ignore test --poll-interval 2000 server.js", + "postpublish": "git push --follow-tags origin master gh-pages && echo Remember to update GitHub Releases from CHANGELOG.md", + "preversion": "npm run test && depcheck && david && git-branch-is master && travis-status -b master -c -w -x && appveyor-status -b master -c -w -p kevinoid/mocha-ur2ue", "test": "npm run lint && npm run test-unit", - "test-cov": "npm run lint && npm run test-unit-cov", "test-unit": "mocha --recursive test", - "test-unit-cov": "nyc mocha --recursive test", - "upload-cov": "codecov < ./coverage/lcov.info && coveralls < ./coverage/lcov.info", "version": "npm run changelog && echo && echo === Please edit CHANGELOG.md as desired, then exit === && echo && $npm_config_shell && git commit -m \"Update CHANGELOG.md for $npm_package_version\" CHANGELOG.md", "version-deps": "npm install conventional-changelog-cli david depcheck git-branch-is travis-status" }, - "dependencies": { - "yargs": "^15.0.0" - }, + "dependencies": {}, "devDependencies": { "@kevinoid/eslint-config": "^9.0.0", - "codecov": "^3.0.0", - "coveralls": "^3.0.0", "eslint": "^7.0.0", "eslint-config-airbnb-base": "^14.2.0", "eslint-plugin-import": "^2.18.2", @@ -54,11 +38,7 @@ "eslint-plugin-node": "^11.0.0", "eslint-plugin-promise": "^4.2.1", "eslint-plugin-unicorn": "^20.0.0", - "jsdoc": "^3.6.0", - "mocha": "^8.0.1", - "nodecat": "^2.0.0", - "nyc": "^15.0.0", - "rimraf": "^3.0.0" + "mocha": "^8.0.1" }, "engines": { "node": ">=10.13", @@ -79,10 +59,5 @@ "mocha": { "checkLeaks": true, "exit": false - }, - "nyc": { - "exclude": [ - "test" - ] } }