From af87bd36bc8988161b548a5d7fdce35919e0a684 Mon Sep 17 00:00:00 2001 From: Ryan Reeves Date: Wed, 3 Apr 2024 12:19:09 +1100 Subject: [PATCH] chore(prettier): [RO-26549] Added prettier and ran it agains all files --- .editorconfig | 10 +- .eslintrc | 33 ------ .eslintrc.json | 10 ++ README.md | 51 ++++----- example/src/config/default.js | 2 +- example/src/config/development.js | 2 +- example/src/controllers/HelloController.js | 4 +- example/src/injector.js | 6 +- example/src/runtime/WebServer.js | 2 - example/start.js | 3 +- jest.config.js | 6 +- package-lock.json | 16 +++ package.json | 1 + src/handlers/HtmlErrorRender.js | 1 - src/injector.js | 8 +- src/middleware/BaseMiddleware.js | 4 +- src/middleware/DefaultMiddleware.js | 10 +- src/middleware/ErrorMiddleware.js | 14 +-- src/middleware/PromiseMiddleware.js | 9 +- .../WinstonRequestLoggingMiddleware.js | 12 +-- src/webserver/BaseController.js | 7 +- src/webserver/BaseWebServer.js | 3 +- src/webserver/ControllerRegistration.js | 13 +-- test/{.eslintrc => .eslintrc.json} | 5 - test/fixtures/config/test.js | 4 +- test/fixtures/controllers/MockController.js | 11 +- .../PromiseMiddlewareTestController.js | 2 - .../controllers/ServerErrorTestController.js | 1 - test/fixtures/injector.js | 9 +- .../fixtures/middleware/TestBaseMiddleware.js | 2 - test/fixtures/runtime/TestWebServer.js | 7 +- test/unit/middleware/BaseMiddleware.spec.js | 9 +- .../unit/middleware/DefaultMiddleware.spec.js | 1 - test/unit/middleware/ErrorMiddleware.spec.js | 15 +-- .../unit/middleware/NoCacheMiddleware.spec.js | 1 - .../unit/middleware/PromiseMiddleware.spec.js | 32 ++++-- .../WinstonRequestLoggingMiddleware.spec.js | 102 +++++++++++------- test/unit/webserver/BaseController.spec.js | 1 - test/unit/webserver/BaseWebServer.spec.js | 4 +- .../webserver/ControllerRegistration.spec.js | 1 - 40 files changed, 192 insertions(+), 242 deletions(-) delete mode 100644 .eslintrc create mode 100644 .eslintrc.json rename test/{.eslintrc => .eslintrc.json} (61%) diff --git a/.editorconfig b/.editorconfig index 4e10649..61f8993 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,12 +1,14 @@ root = true [*] -indent_style = space -indent_size = 2 -end_of_line = lf charset = utf-8 -trim_trailing_whitespace = true +end_of_line = lf +indent_size = 2 +indent_style = space insert_final_newline = true +max_line_length = 120 +quote_type = single +trim_trailing_whitespace = true [node_modules/**.js] codepaint = false diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index a443486..0000000 --- a/.eslintrc +++ /dev/null @@ -1,33 +0,0 @@ -{ - "env": { - "node": true, - "es2021": true - }, - "plugins": ["node"], - "parserOptions": { - "ecmaVersion": "latest" - }, - "rules": { - "arrow-parens": ["error", "always"], - "no-shadow": 0, - "radix": 0, - "keyword-spacing": 0, - "consistent-return": 0, - "arrow-body-style": 0, - "no-use-before-define": 0, - "quotes": [ - "error", - "single", - { - "avoidEscape": true - } - ], - "semi": ["error", "always"], - "one-var": [ - "error", - { - "uninitialized": "always" - } - ] - } -} diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..ebdde34 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,10 @@ +{ + "env": { + "node": true, + "es2021": true + }, + "plugins": ["node"], + "parserOptions": { + "ecmaVersion": "latest" + } +} diff --git a/README.md b/README.md index 794d42e..09a5326 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) @@ -25,11 +25,13 @@ The Spur Framework is a collection of commonly used Node.JS libraries used to cr ## Installing `Dependencies:` + ```shell $ npm install --save spur-ioc spur-common spur-config ``` `Module:` + ```shell $ npm install --save spur-web ``` @@ -57,15 +59,11 @@ module.exports = function () { // Register configuration registerConfig(ioc, path.join(__dirname, './config')); - ioc.merge(spurCommon()); ioc.merge(spurWeb()); // register folders in your project to be auto-injected - ioc.registerFolders(__dirname, [ - 'controllers/', - 'runtime/' - ]); + ioc.registerFolders(__dirname, ['controllers/', 'runtime/']); return ioc; }; @@ -76,7 +74,6 @@ module.exports = function () { ```javascript module.exports = function (BaseWebServer, path) { class WebServer extends BaseWebServer { - // Add additional changes to the middleware by overriding the method registerDefaultMiddleware() { super.registerDefaultMiddleware(); @@ -95,7 +92,6 @@ Files ending in `*Controller.js` are auto registered as controllers. ```javascript module.exports = function (BaseController) { class HelloController extends BaseController { - configure(app) { super.configure(app); @@ -110,7 +106,6 @@ module.exports = function (BaseController) { getHello(req, res) { res.send('hello'); } - } return new HelloController(); @@ -130,8 +125,7 @@ injector().inject(function (UncaughtHandler, WebServer, Logger, config, configLo Logger.info(`PORT: ${config.Port}`); Logger.info(`CONFIG: ${configLoader.configName}`); - WebServer.start() - .then(() => { + WebServer.start().then(() => { // Execute other logic after the server has started }); }); @@ -152,7 +146,7 @@ To see the latest list of the default dependencies that are injected, check out List of external dependencies used and exposed by spur-web. They can be found at npmjs.org using their original names. | Name | Original Module Name | -| :---- | :---- | +| :----------------- | :--------------------------------------------------------------- | | **express** | [express](https://www.npmjs.org/package/express) | | **expressDevice** | [express-device](https://www.npmjs.org/package/express-device) | | **methodOverride** | [method-override](https://www.npmjs.org/package/method-override) | @@ -166,8 +160,8 @@ All of the files under the `src/` directory are made available when this module #### 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 +169,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, 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 | | **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 +184,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 diff --git a/example/src/config/default.js b/example/src/config/default.js index 4df5e00..cfd3264 100644 --- a/example/src/config/default.js +++ b/example/src/config/default.js @@ -1,6 +1,6 @@ module.exports = function () { return this.properties({ Type: 'default', - Port: 9000 + Port: 9000, }); }; diff --git a/example/src/config/development.js b/example/src/config/development.js index b3395c2..e83947d 100644 --- a/example/src/config/development.js +++ b/example/src/config/development.js @@ -2,6 +2,6 @@ module.exports = function () { this.extends('default'); return this.properties({ - Type: 'development' + Type: 'development', }); }; diff --git a/example/src/controllers/HelloController.js b/example/src/controllers/HelloController.js index 519b830..564298d 100644 --- a/example/src/controllers/HelloController.js +++ b/example/src/controllers/HelloController.js @@ -1,6 +1,5 @@ module.exports = function (BaseController) { class HelloController extends BaseController { - configure(app) { super.configure(app); @@ -14,12 +13,11 @@ module.exports = function (BaseController) { getHello(req, res) { const model = { - user: req.query.user || 'John Doe' + user: req.query.user || 'John Doe', }; res.render('hello', model); } - } return new HelloController(); diff --git a/example/src/injector.js b/example/src/injector.js index 23bb1ec..939d63c 100644 --- a/example/src/injector.js +++ b/example/src/injector.js @@ -15,15 +15,11 @@ module.exports = function () { // Register configuration registerConfig(ioc, path.join(__dirname, './config')); - ioc.merge(spurCommon()); ioc.merge(spurWeb()); // register folders in your project to be auto-injected - ioc.registerFolders(__dirname, [ - 'controllers/', - 'runtime/' - ]); + ioc.registerFolders(__dirname, ['controllers/', 'runtime/']); return ioc; }; diff --git a/example/src/runtime/WebServer.js b/example/src/runtime/WebServer.js index af086b0..340fd94 100644 --- a/example/src/runtime/WebServer.js +++ b/example/src/runtime/WebServer.js @@ -1,11 +1,9 @@ module.exports = function (BaseWebServer) { class WebServer extends BaseWebServer { - // Add additional changes to the middleware by overriding the method registerDefaultMiddleware() { super.registerDefaultMiddleware(); } - } // Assure there is just one instance diff --git a/example/start.js b/example/start.js index 0263b81..ddca26b 100644 --- a/example/start.js +++ b/example/start.js @@ -7,8 +7,7 @@ injector().inject(function (UncaughtHandler, WebServer, Logger, config, configLo Logger.info(`PORT: ${config.Port}`); Logger.info(`CONFIG: ${configLoader.configName}`); - WebServer.start() - .then(() => { + WebServer.start().then(() => { // Execute other logic after the server has started }); }); diff --git a/jest.config.js b/jest.config.js index c669010..aa7481a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -9,11 +9,11 @@ module.exports = { branches: 62, functions: 84, lines: 89, - statements: 89 - } + statements: 89, + }, }, maxWorkers: 1, - moduleFileExtensions: ['js','json'], + moduleFileExtensions: ['js', 'json'], rootDir: '.', testEnvironment: 'node', testMatch: ['**/*.spec.js'], diff --git a/package-lock.json b/package-lock.json index a8a85d6..cdb4179 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "eslint": "8.42.0", "eslint-plugin-node": "11.1.0", "jest": "29.7.0", + "prettier": "3.2.5", "spur-common": "3.0.2", "spur-config": "2.0.4" } @@ -4793,6 +4794,21 @@ "node": ">= 0.8.0" } }, + "node_modules/prettier": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz", + "integrity": "sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/pretty-format": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", diff --git a/package.json b/package.json index 02b0214..6426a56 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "eslint": "8.42.0", "eslint-plugin-node": "11.1.0", "jest": "29.7.0", + "prettier": "3.2.5", "spur-common": "3.0.2", "spur-config": "2.0.4" } diff --git a/src/handlers/HtmlErrorRender.js b/src/handlers/HtmlErrorRender.js index 99e26be..e1de361 100644 --- a/src/handlers/HtmlErrorRender.js +++ b/src/handlers/HtmlErrorRender.js @@ -1,6 +1,5 @@ module.exports = function () { class HtmlErrorRender { - static render(err, req, res) { return new HtmlErrorRender().render(err, req, res); } diff --git a/src/injector.js b/src/injector.js index 1cbebf2..f54478b 100644 --- a/src/injector.js +++ b/src/injector.js @@ -16,14 +16,10 @@ module.exports = function injector() { methodOverride, cookieParser, bodyParser, - expressWinston + expressWinston, }); - ioc.registerFolders(__dirname, [ - 'handlers', - 'middleware', - 'webserver' - ]); + ioc.registerFolders(__dirname, ['handlers', 'middleware', 'webserver']); return ioc; }; diff --git a/src/middleware/BaseMiddleware.js b/src/middleware/BaseMiddleware.js index e2a933d..228daec 100644 --- a/src/middleware/BaseMiddleware.js +++ b/src/middleware/BaseMiddleware.js @@ -1,6 +1,4 @@ -module.exports = function ( - Logger -) { +module.exports = function (Logger) { class BaseMiddleware { configure(app) { this.app = app; diff --git a/src/middleware/DefaultMiddleware.js b/src/middleware/DefaultMiddleware.js index 831375a..02c782c 100644 --- a/src/middleware/DefaultMiddleware.js +++ b/src/middleware/DefaultMiddleware.js @@ -1,12 +1,5 @@ -module.exports = function ( - bodyParser, - cookieParser, - methodOverride, - expressDevice, - BaseMiddleware -) { +module.exports = function (bodyParser, cookieParser, methodOverride, expressDevice, BaseMiddleware) { class DefaultMiddleware extends BaseMiddleware { - configure(app) { super.configure(app); @@ -16,7 +9,6 @@ module.exports = function ( this.app.use(methodOverride()); this.app.use(expressDevice.capture()); } - } return new DefaultMiddleware(); diff --git a/src/middleware/ErrorMiddleware.js b/src/middleware/ErrorMiddleware.js index d42089c..afd6941 100644 --- a/src/middleware/ErrorMiddleware.js +++ b/src/middleware/ErrorMiddleware.js @@ -1,14 +1,8 @@ const _some = require('lodash.some'); const _assignIn = require('lodash.assignin'); -module.exports = function ( - SpurErrors, - Logger, - HtmlErrorRender, - BaseMiddleware -) { +module.exports = function (SpurErrors, Logger, HtmlErrorRender, BaseMiddleware) { class ErrorMiddleware extends BaseMiddleware { - configure(app) { super.configure(app); @@ -34,7 +28,7 @@ module.exports = function ( res.format({ text: () => this.sendTextResponse(err, req, res), html: () => this.sendHtmlResponse(err, req, res), - json: () => this.sendJsonResponse(err, req, res) + json: () => this.sendJsonResponse(err, req, res), }); next(); @@ -45,7 +39,7 @@ module.exports = function ( const checkStatus = (status) => status === statusCode; if (!_some(this.EXCLUDE_STATUSCODE_FROM_LOGS, checkStatus)) { - Logger.error(err, '\n', err.stack, '\n', (err.data || '')); + Logger.error(err, '\n', err.stack, '\n', err.data || ''); } } @@ -55,7 +49,7 @@ module.exports = function ( } err.data = _assignIn(err.data, { - url: req.url + url: req.url, }); } diff --git a/src/middleware/PromiseMiddleware.js b/src/middleware/PromiseMiddleware.js index 9a952f9..b0bf2e3 100644 --- a/src/middleware/PromiseMiddleware.js +++ b/src/middleware/PromiseMiddleware.js @@ -1,10 +1,5 @@ -module.exports = function ( - Promise, - BaseMiddleware -) { - +module.exports = function (Promise, BaseMiddleware) { class PromiseMiddleware extends BaseMiddleware { - configure(app) { super.configure(app); @@ -42,7 +37,7 @@ module.exports = function ( }, json: () => { this.json(results); - } + }, }); }) .catch(this.req.next); diff --git a/src/middleware/WinstonRequestLoggingMiddleware.js b/src/middleware/WinstonRequestLoggingMiddleware.js index 80233bd..ab08135 100644 --- a/src/middleware/WinstonRequestLoggingMiddleware.js +++ b/src/middleware/WinstonRequestLoggingMiddleware.js @@ -1,14 +1,7 @@ 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 +11,11 @@ module.exports = function ( winstonInstance: Logger, meta: true, expressFormat: true, - colorStatus: true + colorStatus: true, }; this.app.use(expressWinston.logger(_assignIn(this.options, this.config))); } - } return new WinstonRequestLoggingMiddleware(); diff --git a/src/webserver/BaseController.js b/src/webserver/BaseController.js index 6956eec..679c7ab 100644 --- a/src/webserver/BaseController.js +++ b/src/webserver/BaseController.js @@ -1,9 +1,5 @@ -module.exports = function ( - config, - Logger -) { +module.exports = function (config, Logger) { class BaseController { - constructor() { this.rootWebPath = config.RootWebPath || ''; } @@ -11,7 +7,6 @@ module.exports = function ( configure() { Logger.info(`Registering controller: ${this.constructor.name}`); } - } return BaseController; diff --git a/src/webserver/BaseWebServer.js b/src/webserver/BaseWebServer.js index 4c53ef0..bda496d 100644 --- a/src/webserver/BaseWebServer.js +++ b/src/webserver/BaseWebServer.js @@ -7,10 +7,9 @@ module.exports = function ( ErrorMiddleware, config, ControllerRegistration, - WinstonRequestLoggingMiddleware + WinstonRequestLoggingMiddleware, ) { class BaseWebServer { - constructor() { this.app = express(); } diff --git a/src/webserver/ControllerRegistration.js b/src/webserver/ControllerRegistration.js index dbc6938..87148e6 100644 --- a/src/webserver/ControllerRegistration.js +++ b/src/webserver/ControllerRegistration.js @@ -1,17 +1,12 @@ 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); + return c instanceof BaseController; }; const filteredValues = Object.values(controllers).filter(instanceOfBaseController); @@ -21,7 +16,9 @@ module.exports = function ( const registeredCount = filteredValues.length; - Logger.info(`Registered ${registeredCount} Controller(s)`, { count: registeredCount }); + Logger.info(`Registered ${registeredCount} Controller(s)`, { + count: registeredCount, + }); return registeredCount; } diff --git a/test/.eslintrc b/test/.eslintrc.json similarity index 61% rename from test/.eslintrc rename to test/.eslintrc.json index 4ebd415..76fcea3 100644 --- a/test/.eslintrc +++ b/test/.eslintrc.json @@ -7,10 +7,5 @@ "globals": { "injector": true, "jest": true - }, - "rules": { - "no-unused-expressions": 0, - "max-len": 0, - "no-console": 0 } } diff --git a/test/fixtures/config/test.js b/test/fixtures/config/test.js index 3f471ac..6878b6e 100644 --- a/test/fixtures/config/test.js +++ b/test/fixtures/config/test.js @@ -1,6 +1,6 @@ module.exports = function () { return this.properties({ Port: 9088, - RootWebPath: '/user/agustin/test/' + RootWebPath: '/user/agustin/test/', }); -} +}; diff --git a/test/fixtures/controllers/MockController.js b/test/fixtures/controllers/MockController.js index 16ccf5f..1663115 100644 --- a/test/fixtures/controllers/MockController.js +++ b/test/fixtures/controllers/MockController.js @@ -1,12 +1,15 @@ module.exports = function (BaseController) { - class MockController extends BaseController { configure(app) { super.configure(app); - app.get("/", (req, res) => { res.status(200).send("SomeIndex") }); - app.get("/with-error", (req, res) => { throw new Error("Throwing a basic error") }); + app.get('/', (req, res) => { + res.status(200).send('SomeIndex'); + }); + app.get('/with-error', (req, res) => { + throw new Error('Throwing a basic error'); + }); } } return new MockController(); -} +}; diff --git a/test/fixtures/controllers/PromiseMiddlewareTestController.js b/test/fixtures/controllers/PromiseMiddlewareTestController.js index 74cb714..1c4caa4 100644 --- a/test/fixtures/controllers/PromiseMiddlewareTestController.js +++ b/test/fixtures/controllers/PromiseMiddlewareTestController.js @@ -1,7 +1,5 @@ module.exports = function (BaseController, Promise) { - class PromiseMiddlewareTestController extends BaseController { - configure(app) { super.configure(app); app.get('/promise-middleware-test--jsonasync', this.getJsonAsync); diff --git a/test/fixtures/controllers/ServerErrorTestController.js b/test/fixtures/controllers/ServerErrorTestController.js index 1456119..7fd5a8a 100644 --- a/test/fixtures/controllers/ServerErrorTestController.js +++ b/test/fixtures/controllers/ServerErrorTestController.js @@ -1,5 +1,4 @@ module.exports = function (BaseController, SpurErrors) { - class ServerErrorTestController extends BaseController { configure(app) { super.configure(app); diff --git a/test/fixtures/injector.js b/test/fixtures/injector.js index f85aecc..d39f092 100644 --- a/test/fixtures/injector.js +++ b/test/fixtures/injector.js @@ -6,7 +6,6 @@ const registerConfig = require('spur-common/registerConfig'); const colors = require('colors'); module.exports = function () { - const ioc = spur.create('test-spur-web'); registerConfig(ioc, path.join(__dirname, 'config')); @@ -14,14 +13,10 @@ module.exports = function () { ioc.merge(spurCommon()); ioc.merge(localInjector()); - ioc.registerFolders(__dirname, [ - 'controllers/', - 'middleware/', - 'runtime/' - ]); + ioc.registerFolders(__dirname, ['controllers/', 'middleware/', 'runtime/']); ioc.registerDependencies({ - colors: colors + colors: colors, }); return ioc; diff --git a/test/fixtures/middleware/TestBaseMiddleware.js b/test/fixtures/middleware/TestBaseMiddleware.js index bc9f1b1..ab58f49 100644 --- a/test/fixtures/middleware/TestBaseMiddleware.js +++ b/test/fixtures/middleware/TestBaseMiddleware.js @@ -1,7 +1,5 @@ module.exports = function (BaseMiddleware, Logger) { - class TestBaseMiddleware extends BaseMiddleware { - configure() { super.configure(); Logger.log('Subclass called'); diff --git a/test/fixtures/runtime/TestWebServer.js b/test/fixtures/runtime/TestWebServer.js index 267c72b..68f438c 100644 --- a/test/fixtures/runtime/TestWebServer.js +++ b/test/fixtures/runtime/TestWebServer.js @@ -1,12 +1,9 @@ module.exports = function (BaseWebServer) { - class TestWebServer extends BaseWebServer { - - registerDefaultMiddleware(){ + registerDefaultMiddleware() { super.registerDefaultMiddleware(); } - } return new TestWebServer(); -} +}; diff --git a/test/unit/middleware/BaseMiddleware.spec.js b/test/unit/middleware/BaseMiddleware.spec.js index de2be4a..647c02c 100644 --- a/test/unit/middleware/BaseMiddleware.spec.js +++ b/test/unit/middleware/BaseMiddleware.spec.js @@ -1,5 +1,4 @@ describe('BaseMiddleware', function () { - beforeEach(() => { return injector().inject((TestBaseMiddleware, Logger) => { this.TestBaseMiddleware = TestBaseMiddleware; @@ -12,12 +11,8 @@ describe('BaseMiddleware', function () { it('should log subclass registration', () => { this.TestBaseMiddleware.configure('app'); - expect(this.Logger.recorded.log).toStrictEqual([ - ['Subclass called'] - ]); + expect(this.Logger.recorded.log).toStrictEqual([['Subclass called']]); - expect(this.Logger.recorded.info).toStrictEqual([ - ['Registering Middleware: TestBaseMiddleware'] - ]); + expect(this.Logger.recorded.info).toStrictEqual([['Registering Middleware: TestBaseMiddleware']]); }); }); diff --git a/test/unit/middleware/DefaultMiddleware.spec.js b/test/unit/middleware/DefaultMiddleware.spec.js index 2211925..e3a27fe 100644 --- a/test/unit/middleware/DefaultMiddleware.spec.js +++ b/test/unit/middleware/DefaultMiddleware.spec.js @@ -1,5 +1,4 @@ describe('DefaultMiddleware', function () { - beforeEach(() => { return injector().inject((DefaultMiddleware, Logger, express) => { this.DefaultMiddleware = DefaultMiddleware; diff --git a/test/unit/middleware/ErrorMiddleware.spec.js b/test/unit/middleware/ErrorMiddleware.spec.js index 384f67b..1d56ade 100644 --- a/test/unit/middleware/ErrorMiddleware.spec.js +++ b/test/unit/middleware/ErrorMiddleware.spec.js @@ -1,9 +1,12 @@ describe('ErrorMiddleware', function () { - let htmlErrorRenderRenderSpy, errorMiddlewareSendTextResponseSpy, errorMiddlewareSendHtmlResponseSpy, errorMiddlewareSendJsonResponseSpy, loggerErrorSpy; + let htmlErrorRenderRenderSpy, + errorMiddlewareSendTextResponseSpy, + errorMiddlewareSendHtmlResponseSpy, + errorMiddlewareSendJsonResponseSpy, + loggerErrorSpy; beforeEach(() => { - injector().inject((ErrorMiddleware, HTTPService, - TestWebServer, HtmlErrorRender, Logger, config) => { + injector().inject((ErrorMiddleware, HTTPService, TestWebServer, HtmlErrorRender, Logger, config) => { this.ErrorMiddleware = ErrorMiddleware; this.HTTPService = HTTPService; this.TestWebServer = TestWebServer; @@ -33,9 +36,7 @@ describe('ErrorMiddleware', function () { this.sendRequest = (accept, url) => { return this.startServerOnPort(this.mockPort).then(() => { - return this.HTTPService.get(url) - .set({ Accept: accept }) - .promise(); + return this.HTTPService.get(url).set({ Accept: accept }).promise(); }); }; @@ -46,7 +47,7 @@ describe('ErrorMiddleware', function () { expect.any(String), expect.any(String), expect.any(String), - { url: expectUrl } + { url: expectUrl }, ); }; }); diff --git a/test/unit/middleware/NoCacheMiddleware.spec.js b/test/unit/middleware/NoCacheMiddleware.spec.js index 5fef012..4154c1e 100644 --- a/test/unit/middleware/NoCacheMiddleware.spec.js +++ b/test/unit/middleware/NoCacheMiddleware.spec.js @@ -1,5 +1,4 @@ describe('NoCacheMiddleware', function () { - beforeEach(() => { return injector().inject((NoCacheMiddleware) => { this.NoCacheMiddleware = NoCacheMiddleware; diff --git a/test/unit/middleware/PromiseMiddleware.spec.js b/test/unit/middleware/PromiseMiddleware.spec.js index 20262d4..b3621ba 100644 --- a/test/unit/middleware/PromiseMiddleware.spec.js +++ b/test/unit/middleware/PromiseMiddleware.spec.js @@ -1,5 +1,4 @@ describe('PromiseMiddleware', function () { - beforeEach(() => { return injector().inject((TestWebServer, HTTPService, Logger, config) => { this.TestWebServer = TestWebServer; @@ -20,18 +19,29 @@ describe('PromiseMiddleware', function () { }); it('jsonAsync - success', () => { - return this.getResponse('jsonasync').promise().then((response) => { - expect(response).toEqual(expect.objectContaining( - { type: 'application/json', body: 'jsonAsync success' } - )); - }); + return this.getResponse('jsonasync') + .promise() + .then((response) => { + expect(response).toEqual( + expect.objectContaining({ + type: 'application/json', + body: 'jsonAsync success', + }), + ); + }); }); it('sendStatusAsync - success', () => { - return this.getResponse('sendstatusasync').promise().then((response) => { - expect(response).toEqual(expect.objectContaining( - { type: 'text/plain', status: 200, text: 'OK' } - )); - }); + return this.getResponse('sendstatusasync') + .promise() + .then((response) => { + expect(response).toEqual( + expect.objectContaining({ + type: 'text/plain', + status: 200, + text: 'OK', + }), + ); + }); }); }); diff --git a/test/unit/middleware/WinstonRequestLoggingMiddleware.spec.js b/test/unit/middleware/WinstonRequestLoggingMiddleware.spec.js index 1b0de0c..557a109 100644 --- a/test/unit/middleware/WinstonRequestLoggingMiddleware.spec.js +++ b/test/unit/middleware/WinstonRequestLoggingMiddleware.spec.js @@ -4,23 +4,33 @@ 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, + 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(); + }, + ); }); afterEach(() => { @@ -43,9 +53,13 @@ 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, + colorStatus: true, + }), + ); }); it('should call expressWinston.logger with options', () => { @@ -59,7 +73,7 @@ describe('WinstonRequestLoggingMiddleware', function () { meta: false, expressFormat: false, colorStatus: false, - fakeOption: '123' + fakeOption: '123', }; this.WinstonRequestLoggingMiddleware.configure(this.app); @@ -72,9 +86,13 @@ 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, + colorStatus: false, + }), + ); }); it('should add a non-default option', () => { @@ -104,24 +122,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 +153,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 +165,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'), {}); + }); }); }); }); diff --git a/test/unit/webserver/BaseController.spec.js b/test/unit/webserver/BaseController.spec.js index e306d6a..08f84a1 100644 --- a/test/unit/webserver/BaseController.spec.js +++ b/test/unit/webserver/BaseController.spec.js @@ -1,5 +1,4 @@ describe('BaseController', function () { - beforeEach(() => { injector().inject((BaseController) => { this.BaseController = BaseController; diff --git a/test/unit/webserver/BaseWebServer.spec.js b/test/unit/webserver/BaseWebServer.spec.js index a5224b4..3a89c1e 100644 --- a/test/unit/webserver/BaseWebServer.spec.js +++ b/test/unit/webserver/BaseWebServer.spec.js @@ -1,5 +1,4 @@ describe('BaseTestWebServer', function () { - beforeEach(() => { return injector().inject((TestWebServer, HTTPService, Logger) => { this.TestWebServer = TestWebServer; @@ -16,8 +15,7 @@ describe('BaseTestWebServer', function () { }); it('get index', (done) => { - this.HTTPService - .get('http://localhost:9088/') + this.HTTPService.get('http://localhost:9088/') .promise() .then((res) => { expect(res.text).toBe('SomeIndex'); diff --git a/test/unit/webserver/ControllerRegistration.spec.js b/test/unit/webserver/ControllerRegistration.spec.js index d2f7779..4cfb076 100644 --- a/test/unit/webserver/ControllerRegistration.spec.js +++ b/test/unit/webserver/ControllerRegistration.spec.js @@ -1,5 +1,4 @@ describe('ControllerRegistration', function () { - beforeEach(() => { injector().inject((ControllerRegistration) => { this.ControllerRegistration = ControllerRegistration;