diff --git a/.eslintrc b/.eslintrc index a443486..17d85d8 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,7 +3,6 @@ "node": true, "es2021": true }, - "plugins": ["node"], "parserOptions": { "ecmaVersion": "latest" }, diff --git a/README.md b/README.md index 794d42e..0c95d6e 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ Common node modules and express middleware that are designed to be the boilerplate of a node web app. - [![NPM Version][npm-version-image]][npm-url] - [![NPM Install Size][npm-install-size-image]][npm-install-size-url] - [![NPM Downloads][npm-downloads-image]][npm-downloads-url] +[![NPM Version][npm-version-image]][npm-url] +[![NPM Install Size][npm-install-size-image]][npm-install-size-url] +[![NPM Downloads][npm-downloads-image]][npm-downloads-url] # About the Spur Framework @@ -15,7 +15,7 @@ The Spur Framework is a collection of commonly used Node.JS libraries used to cr # Topics - [Quick start](#quick-start) - - [Usage](#usage) + - [Usage](#usage) - [Available dependencies in injector](#available-dependencies-in-injector) - [Contributing](#contributing) - [License](#license) @@ -57,7 +57,6 @@ module.exports = function () { // Register configuration registerConfig(ioc, path.join(__dirname, './config')); - ioc.merge(spurCommon()); ioc.merge(spurWeb()); @@ -110,7 +109,6 @@ module.exports = function (BaseController) { getHello(req, res) { res.send('hello'); } - } return new HelloController(); @@ -131,9 +129,9 @@ injector().inject(function (UncaughtHandler, WebServer, Logger, config, configLo Logger.info(`CONFIG: ${configLoader.configName}`); WebServer.start() - .then(() => { - // Execute other logic after the server has started - }); + .then(() => { + // Execute other logic after the server has started + }); }); ``` @@ -160,14 +158,14 @@ List of external dependencies used and exposed by spur-web. They can be found at | **bodyParser** | [body-parser](https://www.npmjs.org/package/body-parser) | | **expressWinston** | [express-winston](https://www.npmjs.org/package/express-winston) | -### Local dependecies +### Local dependencies All of the files under the `src/` directory are made available when this module is merged into another injector. The following list are the notable dependencies available. #### Reusable -| Name | Source | Description | -| :---- | :---- | :---- | +| Name | Source | Description | +| :---- | :---- | :---- | | **BaseController** | [code](src/webserver/BaseController.js) | A base class in order to be able to identify all of the controllers derived from it. | | **BaseWebServer** | [code](src/webserver/BaseWebServer.js) | A base web server that sets all of the middleware mentioned here. | | **ControllerRegistration** | [code](src/webserver/ControllerRegistration.js) | Registers all of the controllers based on the BaseController type and also files that end with `Controller` | @@ -175,15 +173,14 @@ All of the files under the `src/` directory are made available when this module #### Used internally, but can be used/replaced -| Name | Source | Description | -| :---- | :---- | :---- | -| **HtmlErrorRender** | [code](src/handlers/HtmlErrorRender.js) | Sets basic error rendering for uncaught errors. | -| **DefaultMiddleware** | [code](src/middleware/DefaultMiddleware.js) | Registers default express middleware: cookie parser, body parser, method override, and express device | -| **ErrorMiddleware** | [code](src/middleware/ErrorMiddleware.js) | Adds error handling for unhandled errors for requests. | -| **NoCacheMiddleware** | [code](src/middleware/NoCacheMiddleware.js) | Middleware for no cache headers | +| Name | Source | Description | +| :---- | :---- | :---- | +| **HtmlErrorRender** | [code](src/handlers/HtmlErrorRender.js) | Sets basic error rendering for uncaught errors. | +| **DefaultMiddleware** | [code](src/middleware/DefaultMiddleware.js) | Registers default express middleware: cookie parser, and body parser | +| **ErrorMiddleware** | [code](src/middleware/ErrorMiddleware.js) | Adds error handling for unhandled errors for requests. | +| **NoCacheMiddleware** | [code](src/middleware/NoCacheMiddleware.js) | Middleware for no cache headers | | **PromiseMiddleware** | [code](src/middleware/PromiseMiddleware.js) | Extends the response object with functionality to be used through promises. It unwraps promises as they are being resolved. | -| **WinstonRequestLoggingMiddleware** | [code](src/middleware/WinstonRequestLoggingMiddleware.js) | Winston middleware for logging every request to the console log. | - +| **WinstonRequestLoggingMiddleware** | [code](src/middleware/WinstonRequestLoggingMiddleware.js) | Winston middleware for logging every request to the console log. | # Contributing @@ -191,10 +188,10 @@ All of the files under the `src/` directory are made available when this module Please send in pull requests and they will be reviewed in a timely manner. Please review this [generic guide to submitting a good pull requests](https://github.com/blog/1943-how-to-write-the-perfect-pull-request). The only things we ask in addition are the following: - * Please submit small pull requests - * Provide a good description of the changes - * Code changes must include tests - * Be nice to each other in comments. :innocent: +- Please submit small pull requests +- Provide a good description of the changes +- Code changes must include tests +- Be nice to each other in comments. :innocent: ## Style guide @@ -202,7 +199,7 @@ The majority of the settings are controlled using an [EditorConfig](.editorconfi ## All tests should pass -To run the test suite, first install the dependancies, then run `npm test` +To run the test suite, first install the dependencies, then run `npm test` ```shell $ npm install diff --git a/package-lock.json b/package-lock.json index a8a85d6..3381309 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,21 +11,16 @@ "dependencies": { "body-parser": "1.20.2", "cookie-parser": "1.4.6", - "express": "4.18.2", + "express": "4.19.2", "express-device": "0.4.2", "express-winston": "2.6.0", - "lodash.assignin": "4.2.0", - "lodash.invokemap": "4.6.0", - "lodash.some": "4.6.0", "method-override": "3.0.0", "spur-ioc": "2.0.0", "winston": "3.9.0" }, "devDependencies": { "@types/jest": "29.5.12", - "colors": "1.4.0", "eslint": "8.42.0", - "eslint-plugin-node": "11.1.0", "jest": "29.7.0", "spur-common": "3.0.2", "spur-config": "2.0.4" @@ -119,15 +114,6 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, - "node_modules/@babel/core/node_modules/semver": { - "version": "6.3.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", - "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", - "dev": true, - "bin": { - "semver": "bin/semver.js" - } - }, "node_modules/@babel/generator": { "version": "7.24.1", "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.24.1.tgz", @@ -168,15 +154,6 @@ "yallist": "^3.0.2" } }, - "node_modules/@babel/helper-compilation-targets/node_modules/semver": { - "version": "6.3.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", - "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", - "dev": true, - "bin": { - "semver": "bin/semver.js" - } - }, "node_modules/@babel/helper-compilation-targets/node_modules/yallist": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", @@ -2366,45 +2343,6 @@ "url": "https://opencollective.com/eslint" } }, - "node_modules/eslint-plugin-es": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/eslint-plugin-es/-/eslint-plugin-es-3.0.1.tgz", - "integrity": "sha512-GUmAsJaN4Fc7Gbtl8uOBlayo2DqhwWvEzykMHSCZHU3XdJ+NSzzZcVhXh3VxX5icqQ+oQdIEawXX8xkR3mIFmQ==", - "dev": true, - "dependencies": { - "eslint-utils": "^2.0.0", - "regexpp": "^3.0.0" - }, - "engines": { - "node": ">=8.10.0" - }, - "funding": { - "url": "https://github.com/sponsors/mysticatea" - }, - "peerDependencies": { - "eslint": ">=4.19.1" - } - }, - "node_modules/eslint-plugin-node": { - "version": "11.1.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-node/-/eslint-plugin-node-11.1.0.tgz", - "integrity": "sha512-oUwtPJ1W0SKD0Tr+wqu92c5xuCeQqB3hSCHasn/ZgjFdA9iDGNkNf2Zi9ztY7X+hNuMib23LNGRm6+uN+KLE3g==", - "dev": true, - "dependencies": { - "eslint-plugin-es": "^3.0.0", - "eslint-utils": "^2.0.0", - "ignore": "^5.1.1", - "minimatch": "^3.0.4", - "resolve": "^1.10.1", - "semver": "^6.1.0" - }, - "engines": { - "node": ">=8.10.0" - }, - "peerDependencies": { - "eslint": ">=5.16.0" - } - }, "node_modules/eslint-scope": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-7.2.0.tgz", @@ -2421,30 +2359,6 @@ "url": "https://opencollective.com/eslint" } }, - "node_modules/eslint-utils": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-2.1.0.tgz", - "integrity": "sha512-w94dQYoauyvlDc43XnGB8lU3Zt713vNChgt4EWwhXAP2XkBvndfxF0AgIqKOOasjPIPzj9JqgwkwbCYD0/V3Zg==", - "dev": true, - "dependencies": { - "eslint-visitor-keys": "^1.1.0" - }, - "engines": { - "node": ">=6" - }, - "funding": { - "url": "https://github.com/sponsors/mysticatea" - } - }, - "node_modules/eslint-utils/node_modules/eslint-visitor-keys": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-1.3.0.tgz", - "integrity": "sha512-6J72N8UNa462wa/KFODt/PJ3IU60SDpC3QXC1Hjc1BXXpfL2C9R5+AU7jhe0F6GREqVMh4Juu+NY7xn+6dipUQ==", - "dev": true, - "engines": { - "node": ">=4" - } - }, "node_modules/eslint-visitor-keys": { "version": "3.4.1", "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.4.1.tgz", @@ -2609,16 +2523,16 @@ } }, "node_modules/express": { - "version": "4.18.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", - "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "version": "4.19.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.19.2.tgz", + "integrity": "sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.1", + "body-parser": "1.20.2", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.5.0", + "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -2716,51 +2630,14 @@ "node": ">=0.8.0" } }, - "node_modules/express/node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", - "dependencies": { - "bytes": "3.1.2", - "content-type": "~1.0.4", - "debug": "2.6.9", - "depd": "2.0.0", - "destroy": "1.2.0", - "http-errors": "2.0.0", - "iconv-lite": "0.4.24", - "on-finished": "2.4.1", - "qs": "6.11.0", - "raw-body": "2.5.1", - "type-is": "~1.6.18", - "unpipe": "1.0.0" - }, - "engines": { - "node": ">= 0.8", - "npm": "1.2.8000 || >= 1.4.16" - } - }, "node_modules/express/node_modules/cookie": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", - "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", "engines": { "node": ">= 0.6" } }, - "node_modules/express/node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", - "dependencies": { - "bytes": "3.1.2", - "http-errors": "2.0.0", - "iconv-lite": "0.4.24", - "unpipe": "1.0.0" - }, - "engines": { - "node": ">= 0.8" - } - }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -4177,7 +4054,8 @@ "node_modules/lodash.assignin": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/lodash.assignin/-/lodash.assignin-4.2.0.tgz", - "integrity": "sha512-yX/rx6d/UTVh7sSVWVSIMjfnz95evAgDFdb1ZozC35I9mSFCkmzptOzevxjgbQUsc78NR44LVHWjsoMQXy9FDg==" + "integrity": "sha512-yX/rx6d/UTVh7sSVWVSIMjfnz95evAgDFdb1ZozC35I9mSFCkmzptOzevxjgbQUsc78NR44LVHWjsoMQXy9FDg==", + "dev": true }, "node_modules/lodash.bindall": { "version": "4.4.0", @@ -4202,7 +4080,8 @@ "node_modules/lodash.invokemap": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/lodash.invokemap/-/lodash.invokemap-4.6.0.tgz", - "integrity": "sha512-CfkycNtMqgUlfjfdh2BhKO/ZXrP8ePOX5lEU/g0R3ItJcnuxWDwokMGKx1hWcfOikmyOVx6X9IwWnDGlgKl61w==" + "integrity": "sha512-CfkycNtMqgUlfjfdh2BhKO/ZXrP8ePOX5lEU/g0R3ItJcnuxWDwokMGKx1hWcfOikmyOVx6X9IwWnDGlgKl61w==", + "dev": true }, "node_modules/lodash.isempty": { "version": "4.4.0", @@ -4239,7 +4118,8 @@ "node_modules/lodash.some": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/lodash.some/-/lodash.some-4.6.0.tgz", - "integrity": "sha512-j7MJE+TuT51q9ggt4fSgVqro163BEFjAt3u97IqU+JA2DkWl80nFTrowzLpZ/BnpN7rrl0JA/593NAdd8p/scQ==" + "integrity": "sha512-j7MJE+TuT51q9ggt4fSgVqro163BEFjAt3u97IqU+JA2DkWl80nFTrowzLpZ/BnpN7rrl0JA/593NAdd8p/scQ==", + "dev": true }, "node_modules/lodash.union": { "version": "4.6.0", @@ -4949,18 +4829,6 @@ "node": ">= 6" } }, - "node_modules/regexpp": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/regexpp/-/regexpp-3.2.0.tgz", - "integrity": "sha512-pq2bWo9mVD43nbts2wGv17XLiNLya+GklZ8kaDLV2Z08gDCsGpnKn9BFMepvWuHCbyVvY7J5o5+BVvoQbmlJLg==", - "dev": true, - "engines": { - "node": ">=8" - }, - "funding": { - "url": "https://github.com/sponsors/mysticatea" - } - }, "node_modules/require-all": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/require-all/-/require-all-3.0.0.tgz", @@ -5115,9 +4983,9 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, "node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -5503,9 +5371,9 @@ "dev": true }, "node_modules/superagent/node_modules/semver": { - "version": "7.5.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.1.tgz", - "integrity": "sha512-Wvss5ivl8TMRZXXESstBA4uR5iXgEN/VC5/sOcuXdVLzcdkz4HWetIoRfG5gb5X+ij/G9rw9YoGn3QoQ8OCSpw==", + "version": "7.6.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.0.tgz", + "integrity": "sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==", "dev": true, "dependencies": { "lru-cache": "^6.0.0" @@ -5823,9 +5691,9 @@ } }, "node_modules/word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", + "integrity": "sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==", "dev": true, "engines": { "node": ">=0.10.0" diff --git a/package.json b/package.json index 02b0214..e63313b 100644 --- a/package.json +++ b/package.json @@ -52,21 +52,16 @@ "dependencies": { "body-parser": "1.20.2", "cookie-parser": "1.4.6", - "express": "4.18.2", + "express": "4.19.2", "express-device": "0.4.2", "express-winston": "2.6.0", - "lodash.assignin": "4.2.0", - "lodash.invokemap": "4.6.0", - "lodash.some": "4.6.0", "method-override": "3.0.0", "spur-ioc": "2.0.0", "winston": "3.9.0" }, "devDependencies": { "@types/jest": "29.5.12", - "colors": "1.4.0", "eslint": "8.42.0", - "eslint-plugin-node": "11.1.0", "jest": "29.7.0", "spur-common": "3.0.2", "spur-config": "2.0.4" diff --git a/src/middleware/ErrorMiddleware.js b/src/middleware/ErrorMiddleware.js index 04e73d9..bd6047d 100644 --- a/src/middleware/ErrorMiddleware.js +++ b/src/middleware/ErrorMiddleware.js @@ -1,6 +1,3 @@ -const _some = require('lodash.some'); -const _assignIn = require('lodash.assignin'); - module.exports = function (SpurErrors, Logger, HtmlErrorRender, BaseMiddleware) { class ErrorMiddleware extends BaseMiddleware { configure(app) { @@ -39,13 +36,13 @@ module.exports = function (SpurErrors, Logger, HtmlErrorRender, BaseMiddleware) const statusCode = err.statusCode || 0; const checkStatus = (status) => status === statusCode; - if (!_some(this.EXCLUDE_STATUSCODE_FROM_LOGS, checkStatus)) { + if (!(this.EXCLUDE_STATUSCODE_FROM_LOGS ?? []).some(checkStatus)) { Logger.error(err, '\n', err?.stack ?? '', '\n', err?.data ?? ''); } } appendRequestData(err, req) { - err.data = _assignIn( + err.data = Object.assign( { ...err.data }, { url: req.url, diff --git a/src/middleware/WinstonRequestLoggingMiddleware.js b/src/middleware/WinstonRequestLoggingMiddleware.js index 80233bd..cb90e76 100644 --- a/src/middleware/WinstonRequestLoggingMiddleware.js +++ b/src/middleware/WinstonRequestLoggingMiddleware.js @@ -1,14 +1,5 @@ -const _assignIn = require('lodash.assignin'); - -module.exports = function ( - Logger, - expressWinston, - BaseMiddleware, - config -) { - +module.exports = function (Logger, expressWinston, BaseMiddleware, config) { class WinstonRequestLoggingMiddleware extends BaseMiddleware { - configure(app) { super.configure(app); @@ -18,12 +9,10 @@ module.exports = function ( winstonInstance: Logger, meta: true, expressFormat: true, - colorStatus: true }; - this.app.use(expressWinston.logger(_assignIn(this.options, this.config))); + this.app.use(expressWinston.logger(Object.assign(this.options, this.config))); } - } return new WinstonRequestLoggingMiddleware(); diff --git a/src/webserver/ControllerRegistration.js b/src/webserver/ControllerRegistration.js index dbc6938..64d2253 100644 --- a/src/webserver/ControllerRegistration.js +++ b/src/webserver/ControllerRegistration.js @@ -1,25 +1,15 @@ -const _invokeMap = require('lodash.invokemap'); - -module.exports = function ( - $injector, - Logger, - BaseController -) { +module.exports = function ($injector, Logger, BaseController) { const controllers = $injector.getRegex(/Controller$/) || []; class ControllerRegistration { - register(app) { - const instanceOfBaseController = function (c) { - return (c instanceof BaseController); - }; - - const filteredValues = Object.values(controllers).filter(instanceOfBaseController); - - // Call the configuration against every controller - _invokeMap(filteredValues, 'configure', app); - - const registeredCount = filteredValues.length; + const registeredCount = Object.values(controllers).reduce((count, controller) => { + if (controller instanceof BaseController) { + // Call the configuration against every controller + controller.configure(app); + return count + 1; + } + }, 0); Logger.info(`Registered ${registeredCount} Controller(s)`, { count: registeredCount }); diff --git a/test/fixtures/injector.js b/test/fixtures/injector.js index f85aecc..f2b3616 100644 --- a/test/fixtures/injector.js +++ b/test/fixtures/injector.js @@ -3,7 +3,6 @@ const spur = require('spur-ioc'); const spurCommon = require('spur-common'); const localInjector = require('../../src/injector'); const registerConfig = require('spur-common/registerConfig'); -const colors = require('colors'); module.exports = function () { @@ -20,9 +19,5 @@ module.exports = function () { 'runtime/' ]); - ioc.registerDependencies({ - colors: colors - }); - return ioc; }; diff --git a/test/integration/ModuleIntegration.spec.js b/test/integration/ModuleIntegration.spec.js index e0010b0..d2bf211 100644 --- a/test/integration/ModuleIntegration.spec.js +++ b/test/integration/ModuleIntegration.spec.js @@ -1,6 +1,6 @@ const spur = require('spur-ioc'); -// NEED to use require vs import to test module export for backward compatability +// NEED to use require vs import to test module export for backward compatibility const mainModule = require('../../'); describe('Integration', function () { diff --git a/test/unit/middleware/WinstonRequestLoggingMiddleware.spec.js b/test/unit/middleware/WinstonRequestLoggingMiddleware.spec.js index 1b0de0c..6ce0731 100644 --- a/test/unit/middleware/WinstonRequestLoggingMiddleware.spec.js +++ b/test/unit/middleware/WinstonRequestLoggingMiddleware.spec.js @@ -4,23 +4,23 @@ describe('WinstonRequestLoggingMiddleware', function () { beforeEach(() => { this.MockPort = 9088; - return injector().inject((WinstonRequestLoggingMiddleware, expressWinston, express, - HTTPService, Logger, config, colors, TestWebServer) => { - this.WinstonRequestLoggingMiddleware = WinstonRequestLoggingMiddleware; - this.expressWinston = expressWinston; - this.express = express; - this.HTTPService = HTTPService; - this.Logger = Logger; - this.config = config; - this.colors = colors; - this.TestWebServer = TestWebServer; - - expressWinstonLoggerSpy = jest.spyOn(this.expressWinston, 'logger'); - loggerLogSpy = jest.spyOn(this.Logger, 'log'); - - this.app = this.express(); - this.Logger.useNoop(); - }); + return injector().inject( + (WinstonRequestLoggingMiddleware, expressWinston, express, HTTPService, Logger, config, TestWebServer) => { + this.WinstonRequestLoggingMiddleware = WinstonRequestLoggingMiddleware; + this.expressWinston = expressWinston; + this.express = express; + this.HTTPService = HTTPService; + this.Logger = Logger; + this.config = config; + this.TestWebServer = TestWebServer; + + expressWinstonLoggerSpy = jest.spyOn(this.expressWinston, 'logger'); + loggerLogSpy = jest.spyOn(this.Logger, 'log'); + + this.app = this.express(); + this.Logger.useNoop(); + }, + ); }); afterEach(() => { @@ -43,9 +43,7 @@ describe('WinstonRequestLoggingMiddleware', function () { }); it('should use default options', () => { - expect(this.options).toEqual(expect.objectContaining( - { meta: true, expressFormat: true, colorStatus: true } - )); + expect(this.options).toEqual(expect.objectContaining({ meta: true, expressFormat: true })); }); it('should call expressWinston.logger with options', () => { @@ -58,8 +56,7 @@ describe('WinstonRequestLoggingMiddleware', function () { this.config.WinstonWebLogging = { meta: false, expressFormat: false, - colorStatus: false, - fakeOption: '123' + fakeOption: '123', }; this.WinstonRequestLoggingMiddleware.configure(this.app); @@ -72,9 +69,7 @@ describe('WinstonRequestLoggingMiddleware', function () { }); it('should use custom options', () => { - expect(this.options).toEqual(expect.objectContaining( - { meta: false, expressFormat: false, colorStatus: false } - )); + expect(this.options).toEqual(expect.objectContaining({ meta: false, expressFormat: false })); }); it('should add a non-default option', () => { @@ -104,24 +99,30 @@ describe('WinstonRequestLoggingMiddleware', function () { headers: { 'accept-encoding': 'gzip, deflate', connection: 'close', - host: 'localhost:9088' + host: 'localhost:9088', }, httpVersion: '1.1', method: 'GET', originalUrl: '/', query: {}, - url: '/' + url: '/', }, res: { - statusCode: 200 + statusCode: 200, }, - responseTime: expect.any(Number) + responseTime: expect.any(Number), }; return this.startServer().then(() => { - return this.HTTPService.get('http://localhost:9088').promise().then((res) => { - expect(loggerLogSpy).toHaveBeenLastCalledWith('info', expect.stringMatching(/GET \/ 200 \d+ms/), expectedData); - }); + return this.HTTPService.get('http://localhost:9088') + .promise() + .then(() => { + expect(loggerLogSpy).toHaveBeenLastCalledWith( + 'info', + expect.stringMatching(/GET \/ 200 \d+ms/), + expectedData, + ); + }); }); }); @@ -129,9 +130,11 @@ describe('WinstonRequestLoggingMiddleware', function () { this.config.WinstonWebLogging = { expressFormat: true, meta: false }; return this.startServer().then(() => { - return this.HTTPService.get('http://localhost:9088').promise().then((res) => { - expect(loggerLogSpy).toHaveBeenLastCalledWith('info', expect.stringMatching('GET / 200'), {}); - }); + return this.HTTPService.get('http://localhost:9088') + .promise() + .then(() => { + expect(loggerLogSpy).toHaveBeenLastCalledWith('info', expect.stringMatching('GET / 200'), {}); + }); }); }); @@ -139,9 +142,11 @@ describe('WinstonRequestLoggingMiddleware', function () { this.config.WinstonWebLogging = { expressFormat: true, meta: false }; return this.startServer().then(() => { - return this.HTTPService.get('http://localhost:9088/with-error').promise().catch((res) => { - expect(loggerLogSpy).toHaveBeenLastCalledWith('info', expect.stringMatching('GET /with-error 500'), {}); - }); + return this.HTTPService.get('http://localhost:9088/with-error') + .promise() + .catch(() => { + expect(loggerLogSpy).toHaveBeenLastCalledWith('info', expect.stringMatching('GET /with-error 500'), {}); + }); }); }); });