From 1d19bff7c5ad9c13d3e0f90c6c67ce44850c7789 Mon Sep 17 00:00:00 2001 From: javierbrea Date: Mon, 4 Jul 2022 12:28:41 +0200 Subject: [PATCH 01/18] feat: Add Json and Middleware variant handlers --- .eslintignore | 1 + packages/core/CHANGELOG.md | 10 + packages/core/jest.config.js | 2 +- packages/core/src/mocks/Mock.js | 3 +- packages/core/src/mocks/Mocks.js | 2 + packages/core/src/mocks/helpers.js | 24 +- packages/core/src/mocks/validations.js | 22 +- .../src/routes-handlers/RoutesHandlers.js | 8 +- .../Default.js} | 3 +- .../core/src/routes-handlers/handlers/Json.js | 72 ++++ .../routes-handlers/handlers/Middleware.js | 51 +++ packages/core/src/server/Server.js | 1 - packages/core/src/server/middlewares.js | 2 +- packages/core/test/Core.spec.js | 4 +- packages/core/test/mocks/Mocks.spec.js | 4 +- packages/core/test/mocks/helpers.spec.js | 160 +++++++- packages/core/test/mocks/validations.spec.js | 117 +++++- .../Default.spec.js} | 8 +- .../routes-handlers/handlers/Json.spec.js | 91 +++++ .../handlers/Middleware.spec.js | 89 +++++ packages/core/test/server/middlewares.spec.js | 2 +- packages/plugin-admin-api/jest.config.js | 2 +- test/core-e2e-legacy/.gitignore | 27 ++ test/core-e2e-legacy/.vscode/settings.json | 6 + test/core-e2e-legacy/README.md | 8 + test/core-e2e-legacy/jest.config.js | 18 + test/core-e2e-legacy/jest.setup.js | 4 + test/core-e2e-legacy/package.json | 11 + test/core-e2e-legacy/project.json | 6 + ...fig-file-with-custom-route-handler.spec.js | 56 +++ .../src/config-file-with-plugins.spec.js | 91 +++++ test/core-e2e-legacy/src/config-file.spec.js | 72 ++++ .../core-e2e-legacy/src/cors-argument.spec.js | 123 ++++++ .../core-e2e-legacy/src/custom-router.spec.js | 114 ++++++ .../src/delay-argument.spec.js | 43 +++ .../core-e2e-legacy/src/delay-setting.spec.js | 164 ++++++++ test/core-e2e-legacy/src/esm.spec.js | 97 +++++ test/core-e2e-legacy/src/events.spec.js | 111 ++++++ test/core-e2e-legacy/src/files-error.spec.js | 101 +++++ .../config-file-no-path/mocks.config.js | 11 + .../TraceMocksPlugin.js | 55 +++ .../TraceRoutesPlugin.js | 55 +++ .../mocks-server-with-plugins.config.js | 15 + .../config-file-with-plugins/mocks/mocks.json | 16 + .../mocks/routes/middlewares.js | 27 ++ .../mocks/routes/users.js | 72 ++++ .../fixtures/config-file-with-plugins/starter | 29 ++ .../CustomRoutesHandler.js | 43 +++ .../mocks.config.js | 13 + .../config-file-with-routes-handler/starter | 21 + .../src/fixtures/config-file/mocks.config.js | 12 + .../src/fixtures/config-file/starter | 21 + .../custom-routes-handler/db/users.js | 24 ++ .../fixtures/custom-routes-handler/mocks.js | 21 + .../custom-routes-handler/routes/users.js | 36 ++ .../src/fixtures/delays/db/users.js | 24 ++ .../src/fixtures/delays/mocks.js | 21 + .../src/fixtures/delays/routes/user.js | 53 +++ .../src/fixtures/delays/routes/users.js | 36 ++ .../src/fixtures/esm-config/babel.config.js | 3 + .../src/fixtures/esm-config/mocks.config.js | 15 + .../src/fixtures/esm-config/starter | 21 + .../src/fixtures/esm-modified/db/users.es | 12 + .../src/fixtures/esm-modified/mocks.js | 16 + .../esm-modified/routes/middlewares.es | 29 ++ .../src/fixtures/esm-modified/routes/users.es | 85 +++++ .../src/fixtures/esm/db/users.es | 12 + .../core-e2e-legacy/src/fixtures/esm/mocks.es | 16 + .../src/fixtures/esm/routes/middlewares.es | 29 ++ .../src/fixtures/esm/routes/users.es | 65 ++++ .../src/fixtures/files-error-mock/db/users.js | 24 ++ .../src/fixtures/files-error-mock/mocks.js | 11 + .../fixtures/files-error-mock/routes/user.js | 51 +++ .../fixtures/files-error-mock/routes/users.js | 37 ++ .../src/fixtures/files-error-routes/mocks.js | 26 ++ .../files-error-routes/routes/user.js | 51 +++ .../files-error-routes/routes/users.js | 13 + .../src/fixtures/json-files/mocks.json | 11 + .../src/fixtures/json-files/routes/users.json | 61 +++ .../src/fixtures/middleware-route/db/users.js | 24 ++ .../src/fixtures/middleware-route/mocks.js | 26 ++ .../middleware-route/routes/tracer.js | 36 ++ .../fixtures/middleware-route/routes/user.js | 51 +++ .../fixtures/middleware-route/routes/users.js | 37 ++ .../src/fixtures/multi-methods/db/users.js | 24 ++ .../src/fixtures/multi-methods/mocks.js | 21 + .../src/fixtures/multi-methods/routes/user.js | 35 ++ .../fixtures/multi-methods/routes/users.js | 37 ++ test/core-e2e-legacy/src/fixtures/starter | 21 + .../typescript-config/babel.config.js | 3 + .../typescript-config/mocks.config.js | 14 + .../src/fixtures/typescript-config/starter | 21 + .../typescript-custom-config/babel.config.js | 3 + .../typescript-custom-config/mocks.config.js | 26 ++ .../fixtures/typescript-custom-config/starter | 21 + .../fixtures/typescript-imports/mocks.json | 16 + .../typescript-imports/routes/books.ts | 3 + .../typescript-imports/routes/middlewares.ts | 3 + .../typescript-imports/routes/users.ts | 3 + .../src/fixtures/typescript/db/users.ts | 12 + .../src/fixtures/typescript/mocks.ts | 16 + .../src/fixtures/typescript/routes/books.json | 25 ++ .../fixtures/typescript/routes/middlewares.ts | 31 ++ .../src/fixtures/typescript/routes/users.ts | 65 ++++ .../fixtures/validation-not-array/mocks.js | 1 + .../routes/routes-not-valid.js | 1 + .../routes/routes-valid.js | 16 + .../src/fixtures/validations/mocks.js | 28 ++ .../src/fixtures/validations/routes/users.js | 93 +++++ .../web-tutorial-modified/db/users.js | 24 ++ .../fixtures/web-tutorial-modified/mocks.js | 31 ++ .../web-tutorial-modified/routes/user.js | 51 +++ .../web-tutorial-modified/routes/users.js | 61 +++ .../src/fixtures/web-tutorial/db/users.js | 24 ++ .../src/fixtures/web-tutorial/mocks.js | 26 ++ .../src/fixtures/web-tutorial/routes/user.js | 51 +++ .../src/fixtures/web-tutorial/routes/users.js | 37 ++ test/core-e2e-legacy/src/json-files.spec.js | 128 +++++++ .../src/load-mocks-and-routes.spec.js | 157 ++++++++ .../core-e2e-legacy/src/mock-argument.spec.js | 54 +++ test/core-e2e-legacy/src/mock-setting.spec.js | 170 +++++++++ .../core-e2e-legacy/src/multi-methods.spec.js | 225 +++++++++++ .../core-e2e-legacy/src/path-argument.spec.js | 71 ++++ .../src/path-not-exists.spec.js | 149 ++++++++ test/core-e2e-legacy/src/path-setting.spec.js | 65 ++++ .../src/plain-mocks-and-routes.spec.js | 152 ++++++++ .../src/plugins-legacy-alerts.spec.js | 0 .../src/plugins-legacy-formats.spec.js | 0 test/core-e2e-legacy/src/plugins.spec.js | 358 ++++++++++++++++++ .../core-e2e-legacy/src/port-argument.spec.js | 35 ++ test/core-e2e-legacy/src/port-setting.spec.js | 49 +++ .../core-e2e-legacy/src/route-handler.spec.js | 58 +++ .../src/route-variants.spec.js | 300 +++++++++++++++ test/core-e2e-legacy/src/scaffold.spec.js | 230 +++++++++++ test/core-e2e-legacy/src/support/helpers.js | 188 +++++++++ test/core-e2e-legacy/src/typescript.spec.js | 82 ++++ test/core-e2e-legacy/src/validations.spec.js | 177 +++++++++ .../src/watch-disabled.spec.js | 61 +++ .../core-e2e-legacy/src/watch-enabled.spec.js | 84 ++++ test/core-e2e/jest.config.js | 2 +- ...fig-file-with-custom-route-handler.spec.js | 4 +- test/core-e2e/src/delay-setting.spec.js | 4 +- .../TraceMocksPlugin.js | 21 +- .../TraceRoutesPlugin.js | 21 +- .../mocks/routes/middlewares.js | 20 +- .../mocks/routes/users.js | 32 +- .../CustomRoutesHandler.js | 20 +- .../custom-routes-handler/routes/users.js | 1 + .../src/fixtures/delays/routes/user.js | 4 + .../src/fixtures/delays/routes/users.js | 2 + .../esm-modified/routes/middlewares.es | 20 +- .../src/fixtures/esm-modified/routes/users.es | 29 +- .../src/fixtures/esm/routes/middlewares.es | 20 +- .../core-e2e/src/fixtures/esm/routes/users.es | 28 +- .../fixtures/files-error-mock/routes/user.js | 27 +- .../fixtures/files-error-mock/routes/users.js | 2 + .../files-error-routes/routes/user.js | 27 +- .../src/fixtures/json-files/routes/users.json | 4 + .../middleware-route/routes/tracer.js | 22 +- .../fixtures/middleware-route/routes/user.js | 27 +- .../fixtures/middleware-route/routes/users.js | 2 + .../src/fixtures/multi-methods/routes/user.js | 2 + .../fixtures/multi-methods/routes/users.js | 2 + .../src/fixtures/typescript/routes/books.json | 1 + .../fixtures/typescript/routes/middlewares.ts | 14 +- .../src/fixtures/typescript/routes/users.ts | 28 +- .../routes/routes-valid.js | 1 + .../src/fixtures/validations/routes/users.js | 6 + .../web-tutorial-modified/routes/user.js | 27 +- .../web-tutorial-modified/routes/users.js | 3 + .../src/fixtures/web-tutorial/routes/user.js | 27 +- .../src/fixtures/web-tutorial/routes/users.js | 2 + test/core-e2e/src/multi-methods.spec.js | 8 +- .../src/plain-mocks-and-routes.spec.js | 10 +- test/core-e2e/src/route-handler.spec.js | 10 +- test/core-e2e/src/validations.spec.js | 2 +- .../src/main/interactive-cli.spec.js | 2 +- workspace.json | 1 + 178 files changed, 6993 insertions(+), 226 deletions(-) rename packages/core/src/routes-handlers/{default/DefaultRoutesHandler.js => handlers/Default.js} (95%) create mode 100644 packages/core/src/routes-handlers/handlers/Json.js create mode 100644 packages/core/src/routes-handlers/handlers/Middleware.js rename packages/core/test/routes-handlers/{default/DefaultRoutesHandler.spec.js => handlers/Default.spec.js} (95%) create mode 100644 packages/core/test/routes-handlers/handlers/Json.spec.js create mode 100644 packages/core/test/routes-handlers/handlers/Middleware.spec.js create mode 100644 test/core-e2e-legacy/.gitignore create mode 100644 test/core-e2e-legacy/.vscode/settings.json create mode 100644 test/core-e2e-legacy/README.md create mode 100644 test/core-e2e-legacy/jest.config.js create mode 100644 test/core-e2e-legacy/jest.setup.js create mode 100644 test/core-e2e-legacy/package.json create mode 100644 test/core-e2e-legacy/project.json create mode 100644 test/core-e2e-legacy/src/config-file-with-custom-route-handler.spec.js create mode 100644 test/core-e2e-legacy/src/config-file-with-plugins.spec.js create mode 100644 test/core-e2e-legacy/src/config-file.spec.js create mode 100644 test/core-e2e-legacy/src/cors-argument.spec.js create mode 100644 test/core-e2e-legacy/src/custom-router.spec.js create mode 100644 test/core-e2e-legacy/src/delay-argument.spec.js create mode 100644 test/core-e2e-legacy/src/delay-setting.spec.js create mode 100644 test/core-e2e-legacy/src/esm.spec.js create mode 100644 test/core-e2e-legacy/src/events.spec.js create mode 100644 test/core-e2e-legacy/src/files-error.spec.js create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-no-path/mocks.config.js create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-with-plugins/TraceMocksPlugin.js create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-with-plugins/TraceRoutesPlugin.js create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks-server-with-plugins.config.js create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/mocks.json create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/routes/middlewares.js create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/routes/users.js create mode 100755 test/core-e2e-legacy/src/fixtures/config-file-with-plugins/starter create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/CustomRoutesHandler.js create mode 100644 test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/mocks.config.js create mode 100755 test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/starter create mode 100644 test/core-e2e-legacy/src/fixtures/config-file/mocks.config.js create mode 100755 test/core-e2e-legacy/src/fixtures/config-file/starter create mode 100644 test/core-e2e-legacy/src/fixtures/custom-routes-handler/db/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/custom-routes-handler/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/custom-routes-handler/routes/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/delays/db/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/delays/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/delays/routes/user.js create mode 100644 test/core-e2e-legacy/src/fixtures/delays/routes/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/esm-config/babel.config.js create mode 100644 test/core-e2e-legacy/src/fixtures/esm-config/mocks.config.js create mode 100755 test/core-e2e-legacy/src/fixtures/esm-config/starter create mode 100644 test/core-e2e-legacy/src/fixtures/esm-modified/db/users.es create mode 100644 test/core-e2e-legacy/src/fixtures/esm-modified/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/esm-modified/routes/middlewares.es create mode 100644 test/core-e2e-legacy/src/fixtures/esm-modified/routes/users.es create mode 100644 test/core-e2e-legacy/src/fixtures/esm/db/users.es create mode 100644 test/core-e2e-legacy/src/fixtures/esm/mocks.es create mode 100644 test/core-e2e-legacy/src/fixtures/esm/routes/middlewares.es create mode 100644 test/core-e2e-legacy/src/fixtures/esm/routes/users.es create mode 100644 test/core-e2e-legacy/src/fixtures/files-error-mock/db/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/files-error-mock/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/files-error-mock/routes/user.js create mode 100644 test/core-e2e-legacy/src/fixtures/files-error-mock/routes/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/files-error-routes/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/files-error-routes/routes/user.js create mode 100644 test/core-e2e-legacy/src/fixtures/files-error-routes/routes/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/json-files/mocks.json create mode 100644 test/core-e2e-legacy/src/fixtures/json-files/routes/users.json create mode 100644 test/core-e2e-legacy/src/fixtures/middleware-route/db/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/middleware-route/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/middleware-route/routes/tracer.js create mode 100644 test/core-e2e-legacy/src/fixtures/middleware-route/routes/user.js create mode 100644 test/core-e2e-legacy/src/fixtures/middleware-route/routes/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/multi-methods/db/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/multi-methods/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/multi-methods/routes/user.js create mode 100644 test/core-e2e-legacy/src/fixtures/multi-methods/routes/users.js create mode 100755 test/core-e2e-legacy/src/fixtures/starter create mode 100644 test/core-e2e-legacy/src/fixtures/typescript-config/babel.config.js create mode 100644 test/core-e2e-legacy/src/fixtures/typescript-config/mocks.config.js create mode 100755 test/core-e2e-legacy/src/fixtures/typescript-config/starter create mode 100644 test/core-e2e-legacy/src/fixtures/typescript-custom-config/babel.config.js create mode 100644 test/core-e2e-legacy/src/fixtures/typescript-custom-config/mocks.config.js create mode 100755 test/core-e2e-legacy/src/fixtures/typescript-custom-config/starter create mode 100644 test/core-e2e-legacy/src/fixtures/typescript-imports/mocks.json create mode 100644 test/core-e2e-legacy/src/fixtures/typescript-imports/routes/books.ts create mode 100644 test/core-e2e-legacy/src/fixtures/typescript-imports/routes/middlewares.ts create mode 100644 test/core-e2e-legacy/src/fixtures/typescript-imports/routes/users.ts create mode 100644 test/core-e2e-legacy/src/fixtures/typescript/db/users.ts create mode 100644 test/core-e2e-legacy/src/fixtures/typescript/mocks.ts create mode 100644 test/core-e2e-legacy/src/fixtures/typescript/routes/books.json create mode 100644 test/core-e2e-legacy/src/fixtures/typescript/routes/middlewares.ts create mode 100644 test/core-e2e-legacy/src/fixtures/typescript/routes/users.ts create mode 100644 test/core-e2e-legacy/src/fixtures/validation-not-array/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/validation-not-array/routes/routes-not-valid.js create mode 100644 test/core-e2e-legacy/src/fixtures/validation-not-array/routes/routes-valid.js create mode 100644 test/core-e2e-legacy/src/fixtures/validations/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/validations/routes/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/web-tutorial-modified/db/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/web-tutorial-modified/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/web-tutorial-modified/routes/user.js create mode 100644 test/core-e2e-legacy/src/fixtures/web-tutorial-modified/routes/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/web-tutorial/db/users.js create mode 100644 test/core-e2e-legacy/src/fixtures/web-tutorial/mocks.js create mode 100644 test/core-e2e-legacy/src/fixtures/web-tutorial/routes/user.js create mode 100644 test/core-e2e-legacy/src/fixtures/web-tutorial/routes/users.js create mode 100644 test/core-e2e-legacy/src/json-files.spec.js create mode 100644 test/core-e2e-legacy/src/load-mocks-and-routes.spec.js create mode 100644 test/core-e2e-legacy/src/mock-argument.spec.js create mode 100644 test/core-e2e-legacy/src/mock-setting.spec.js create mode 100644 test/core-e2e-legacy/src/multi-methods.spec.js create mode 100644 test/core-e2e-legacy/src/path-argument.spec.js create mode 100644 test/core-e2e-legacy/src/path-not-exists.spec.js create mode 100644 test/core-e2e-legacy/src/path-setting.spec.js create mode 100644 test/core-e2e-legacy/src/plain-mocks-and-routes.spec.js rename test/{core-e2e => core-e2e-legacy}/src/plugins-legacy-alerts.spec.js (100%) rename test/{core-e2e => core-e2e-legacy}/src/plugins-legacy-formats.spec.js (100%) create mode 100644 test/core-e2e-legacy/src/plugins.spec.js create mode 100644 test/core-e2e-legacy/src/port-argument.spec.js create mode 100644 test/core-e2e-legacy/src/port-setting.spec.js create mode 100644 test/core-e2e-legacy/src/route-handler.spec.js create mode 100644 test/core-e2e-legacy/src/route-variants.spec.js create mode 100644 test/core-e2e-legacy/src/scaffold.spec.js create mode 100644 test/core-e2e-legacy/src/support/helpers.js create mode 100644 test/core-e2e-legacy/src/typescript.spec.js create mode 100644 test/core-e2e-legacy/src/validations.spec.js create mode 100644 test/core-e2e-legacy/src/watch-disabled.spec.js create mode 100644 test/core-e2e-legacy/src/watch-enabled.spec.js diff --git a/.eslintignore b/.eslintignore index fa385b267..2a8ec1a8b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,4 +6,5 @@ /packages/*/dist /packages/*/coverage /test/core-e2e/src/fixtures +/test/core-e2e-legacy/src/fixtures /test/main-e2e/src/fixtures \ No newline at end of file diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index f50ca370a..b7ea98983 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -10,6 +10,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed ### Removed +## [unreleased] + +### Added +- feat(#335): Pass only response property from variants to route variant handlers having the "version" property defined as "4". If it has another value, pass the whole variant object (for backward compatibility) +- feat(#336): Add "Json" and "Middleware" variant handlers. +- feat: Support defining the response preview in the handlers "preview" property. Keep also "plainResponsePreview" for backward compatibility. + +### Changed +- feat: Log requests in the middleware added by the Mock class, so it has not to be logged in every different handler. + ## [3.4.0] - 2022-07-01 ### Added diff --git a/packages/core/jest.config.js b/packages/core/jest.config.js index 2822919e5..cef50e4d8 100644 --- a/packages/core/jest.config.js +++ b/packages/core/jest.config.js @@ -26,7 +26,7 @@ module.exports = { // The glob patterns Jest uses to detect test files testMatch: ["/test/**/*.spec.js"], - // testMatch: ["/test/**/Core.spec.js"], + // testMatch: ["/test/**/mocks/validations.spec.js"], // The test environment that will be used for testing testEnvironment: "node", diff --git a/packages/core/src/mocks/Mock.js b/packages/core/src/mocks/Mock.js index 654e9ef87..761749b8d 100644 --- a/packages/core/src/mocks/Mock.js +++ b/packages/core/src/mocks/Mock.js @@ -29,7 +29,8 @@ class Mock { : [routeVariant.method]; methods.forEach((method) => { const httpMethod = HTTP_METHODS[method.toUpperCase()]; - this._router[httpMethod](routeVariant.url, (_req, _res, next) => { + this._router[httpMethod](routeVariant.url, (req, _res, next) => { + this._logger.info(`Request ${req.method} => ${req.url} | req: ${req.id}`); const delay = routeVariant.delay !== null ? routeVariant.delay : this._getDelay(); if (delay > 0) { this._logger.verbose(`Applying delay of ${delay}ms to route variant "${this._id}"`); diff --git a/packages/core/src/mocks/Mocks.js b/packages/core/src/mocks/Mocks.js index 5c8fb33cf..7c1333ba9 100644 --- a/packages/core/src/mocks/Mocks.js +++ b/packages/core/src/mocks/Mocks.js @@ -108,6 +108,7 @@ class Mocks { mocksDefinitions: this._mocksDefinitions, alerts: this._alertsMocks, logger: this._loggerLoadMocks, + loggerRoutes: this._loggerRoutes, routeVariants: this._routesVariants, getGlobalDelay: this.getDelay, }); @@ -212,6 +213,7 @@ class Mocks { getGlobalDelay: this.getDelay, alerts, logger: this._loggerLoadMocks, + loggerRoutes: this._loggerRoutes, }); } diff --git a/packages/core/src/mocks/helpers.js b/packages/core/src/mocks/helpers.js index 8d755b36e..dbf9780bc 100644 --- a/packages/core/src/mocks/helpers.js +++ b/packages/core/src/mocks/helpers.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { flatten, compact } = require("lodash"); +const { flatten, compact, isUndefined } = require("lodash"); const CustomCore = require("../CustomCore"); const Mock = require("./Mock"); @@ -139,7 +139,10 @@ function getPlainRoutesVariants(routesVariants) { id: routeVariant.variantId, routeId: routeVariant.routeId, handler: routeVariant.constructor.id, - response: routeVariant.plainResponsePreview, + response: !isUndefined(routeVariant.preview) + ? routeVariant.preview + : // TODO, deprecated, remove plainResponsePreview + routeVariant.plainResponsePreview, delay: routeVariant.delay, }; }); @@ -215,15 +218,17 @@ function getVariantHandler({ } try { + const variantArgument = Handler.version === "4" ? variant.response : variant; routeHandler = new Handler( { - ...variant, + ...variantArgument, variantId, url: route.url, method: route.method, }, routeVariantCustomCore ); + // TODO, do not add properties to handler. Store it in "handler" property routeHandler.delay = getRouteHandlerDelay(variant, route); routeHandler.id = variant.id; routeHandler.variantId = variantId; @@ -304,6 +309,7 @@ function getMock({ mocksDefinitions, routeVariants, logger, + loggerRoutes, getGlobalDelay, alerts, }) { @@ -335,7 +341,7 @@ function getMock({ routeVariants, alerts ), - logger, + logger: loggerRoutes, getDelay: getGlobalDelay, }); } catch (error) { @@ -344,7 +350,14 @@ function getMock({ return mock; } -function getMocks({ mocksDefinitions, alerts, logger, routeVariants, getGlobalDelay }) { +function getMocks({ + mocksDefinitions, + alerts, + logger, + loggerRoutes, + routeVariants, + getGlobalDelay, +}) { alerts.clean(); let errorsProcessing = 0; let ids = []; @@ -360,6 +373,7 @@ function getMocks({ mocksDefinitions, alerts, logger, routeVariants, getGlobalDe routeVariants, getGlobalDelay, logger, + loggerRoutes, alerts: alertsMock, }); if (!mock) { diff --git a/packages/core/src/mocks/validations.js b/packages/core/src/mocks/validations.js index ad1df3c58..ca0047263 100644 --- a/packages/core/src/mocks/validations.js +++ b/packages/core/src/mocks/validations.js @@ -117,6 +117,7 @@ const routesSchema = { ], }, }, + // TODO, require "response" in all variants to be an object, do not allow additionalProperties required: ["id"], }, }, @@ -282,17 +283,26 @@ function variantValidationErrors(route, variant, Handler) { return null; } const variantValidator = ajv.compile(Handler.validationSchema); - const isValid = variantValidator(variant); + const isVersion4 = Handler.version === "4"; + const dataToCheck = isVersion4 ? variant.response : variant; + const dataMessage = isVersion4 ? "Invalid 'response' property:" : ""; + const isValid = variantValidator(dataToCheck); if (!isValid) { + let validationMessage; + try { + validationMessage = validationSingleMessage( + Handler.validationSchema, + dataToCheck || {}, + variantValidator.errors + ); + } catch (error) { + validationMessage = " Wrong type"; + } const idTrace = variant && variant.id ? `${traceId(variant.id)} ` : ""; return { message: `Variant ${idTrace}in route ${traceId( route.id - )} is invalid: ${validationSingleMessage( - Handler.validationSchema, - variant, - variantValidator.errors - )}`, + )} is invalid: ${dataMessage}${validationMessage}`, errors: variantValidator.errors, }; } diff --git a/packages/core/src/routes-handlers/RoutesHandlers.js b/packages/core/src/routes-handlers/RoutesHandlers.js index a58bfc7d4..ce2bb34c3 100644 --- a/packages/core/src/routes-handlers/RoutesHandlers.js +++ b/packages/core/src/routes-handlers/RoutesHandlers.js @@ -1,5 +1,5 @@ /* -Copyright 2021 Javier Brea +Copyright 2022 Javier Brea Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -9,13 +9,15 @@ Unless required by applicable law or agreed to in writing, software distributed */ // Default routes handler -const DefaultRoutesHandler = require("./default/DefaultRoutesHandler"); +const DefaultRoutesHandler = require("./handlers/Default"); +const Json = require("./handlers/Json"); +const Middleware = require("./handlers/Middleware"); class RoutesHandlers { constructor({ logger }) { this._logger = logger; this._registeredRouteHandlers = []; - this._routeHandlers = [DefaultRoutesHandler]; + this._routeHandlers = [DefaultRoutesHandler, Json, Middleware]; } add(RoutesHandler) { diff --git a/packages/core/src/routes-handlers/default/DefaultRoutesHandler.js b/packages/core/src/routes-handlers/handlers/Default.js similarity index 95% rename from packages/core/src/routes-handlers/default/DefaultRoutesHandler.js rename to packages/core/src/routes-handlers/handlers/Default.js index 45e4b3ca7..6775fa0d3 100644 --- a/packages/core/src/routes-handlers/default/DefaultRoutesHandler.js +++ b/packages/core/src/routes-handlers/handlers/Default.js @@ -64,7 +64,6 @@ class DefaultRoutesHandler { } middleware(req, res, next) { - this._logger.info(`Request ${req.method} => ${req.url}`); if (isFunction(this._response)) { this._logger.verbose(`Response is a function, executing middleware | req: ${req.id}`); this._response(req, res, next, this._core); @@ -74,7 +73,7 @@ class DefaultRoutesHandler { res.set(this._response.headers); } res.status(this._response.status); - this._logger.debug(`Sending response | req: ${req.id}`); + this._logger.verbose(`Sending response | req: ${req.id}`); res.send(this._response.body); } } diff --git a/packages/core/src/routes-handlers/handlers/Json.js b/packages/core/src/routes-handlers/handlers/Json.js new file mode 100644 index 000000000..5f8b921f0 --- /dev/null +++ b/packages/core/src/routes-handlers/handlers/Json.js @@ -0,0 +1,72 @@ +/* +Copyright 2022 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +"use strict"; + +class Json { + static get id() { + return "json"; + } + + static get version() { + return "4"; + } + + static get validationSchema() { + return { + type: "object", + properties: { + headers: { + type: "object", + }, + status: { + type: "number", + }, + body: { + oneOf: [ + { + type: "object", + }, + { + type: "array", + }, + ], + }, + }, + required: ["status", "body"], + additionalProperties: false, + }; + } + + constructor(response, core) { + this._response = response; + this._logger = core.logger; + this._core = core; + } + + middleware(req, res) { + if (this._response.headers) { + this._logger.debug(`Setting headers | req: ${req.id}`); + res.set(this._response.headers); + } + res.status(this._response.status); + this._logger.verbose(`Sending response | req: ${req.id}`); + res.send(this._response.body); + } + + get preview() { + return { + body: this._response.body, + status: this._response.status, + }; + } +} + +module.exports = Json; diff --git a/packages/core/src/routes-handlers/handlers/Middleware.js b/packages/core/src/routes-handlers/handlers/Middleware.js new file mode 100644 index 000000000..b2e3b2b55 --- /dev/null +++ b/packages/core/src/routes-handlers/handlers/Middleware.js @@ -0,0 +1,51 @@ +/* +Copyright 2022 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +"use strict"; + +class Json { + static get id() { + return "middleware"; + } + + static get version() { + return "4"; + } + + static get validationSchema() { + return { + type: "object", + properties: { + middleware: { + instanceof: "Function", + }, + }, + required: ["middleware"], + additionalProperties: false, + }; + } + + constructor(response, core) { + this._response = response; + this._logger = core.logger; + this._core = core; + } + + middleware(req, res, next) { + this._logger.verbose(`Executing middleware | req: ${req.id}`); + this._response.middleware(req, res, next, this._core); + } + + get preview() { + return null; + } +} + +module.exports = Json; diff --git a/packages/core/src/server/Server.js b/packages/core/src/server/Server.js index 348c6a05f..dd37720a0 100644 --- a/packages/core/src/server/Server.js +++ b/packages/core/src/server/Server.js @@ -184,7 +184,6 @@ class Server { // TODO, Add options to allow to disable or configure it this._express.use(notFound({ logger: this._logger })); - this._express.use(errorHandler({ logger: this._logger })); // Create server diff --git a/packages/core/src/server/middlewares.js b/packages/core/src/server/middlewares.js index 9aaa00c20..85577848e 100644 --- a/packages/core/src/server/middlewares.js +++ b/packages/core/src/server/middlewares.js @@ -24,7 +24,7 @@ const urlEncodedBodyParser = (options) => bodyParser.urlencoded(options); const logRequest = ({ logger }) => (req, _res, next) => { - logger.verbose(`Request received | ${req.method} => ${req.url} | Assigned id: ${req.id}`); + logger.debug(`Request received | ${req.method} => ${req.url} | Assigned id: ${req.id}`); next(); }; diff --git a/packages/core/test/Core.spec.js b/packages/core/test/Core.spec.js index c336f284f..40dbc2e27 100644 --- a/packages/core/test/Core.spec.js +++ b/packages/core/test/Core.spec.js @@ -275,8 +275,8 @@ describe("Core", () => { it("should add Route Handler", () => { core.addRoutesHandler("foo"); // TODO, do not use private properties in testing - expect(core._routesHandlers._routeHandlers.length).toEqual(2); - expect(core._routesHandlers._routeHandlers[1]).toEqual("foo"); + expect(core._routesHandlers._routeHandlers.length).toEqual(4); + expect(core._routesHandlers._routeHandlers[3]).toEqual("foo"); }); }); diff --git a/packages/core/test/mocks/Mocks.spec.js b/packages/core/test/mocks/Mocks.spec.js index a25d8331b..2f7eb8253 100644 --- a/packages/core/test/mocks/Mocks.spec.js +++ b/packages/core/test/mocks/Mocks.spec.js @@ -1,5 +1,5 @@ /* -Copyright 2021 Javier Brea +Copyright 2021-2022 Javier Brea Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -17,7 +17,7 @@ const MockMock = require("./Mock.mock.js"); const Alerts = require("../../src/Alerts"); const Mocks = require("../../src/mocks/Mocks"); const ConfigMock = require("../Config.mocks"); -const DefaultRoutesHandler = require("../../src/routes-handlers/default/DefaultRoutesHandler"); +const DefaultRoutesHandler = require("../../src/routes-handlers/handlers/Default"); describe("Mocks", () => { let configMock; diff --git a/packages/core/test/mocks/helpers.spec.js b/packages/core/test/mocks/helpers.spec.js index 6ebac4d97..e0b3b595d 100644 --- a/packages/core/test/mocks/helpers.spec.js +++ b/packages/core/test/mocks/helpers.spec.js @@ -1,5 +1,5 @@ /* -Copyright 2021 Javier Brea +Copyright 2021-2022 Javier Brea Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -27,7 +27,8 @@ const { getMock, } = require("../../src/mocks/helpers"); const { compileRouteValidator } = require("../../src/mocks/validations"); -const DefaultRoutesHandler = require("../../src/routes-handlers/default/DefaultRoutesHandler"); +const DefaultRoutesHandler = require("../../src/routes-handlers/handlers/Default"); +const JsonRoutesHandler = require("../../src/routes-handlers/handlers/Json"); const Alerts = require("../../src/Alerts"); describe("mocks helpers", () => { @@ -465,7 +466,7 @@ describe("mocks helpers", () => { }); describe("getPlainRoutesVariants", () => { - it("should return routes variants in plain format", () => { + it("should return routes variants in plain format when using legacy plainResponsePreview property", () => { expect( getPlainRoutesVariants([ { @@ -502,6 +503,44 @@ describe("mocks helpers", () => { }, ]); }); + + it("should return routes variants in plain format when using preview property", () => { + expect( + getPlainRoutesVariants([ + { + variantId: "route-1:variant-1", + routeId: "route-1", + constructor: { id: "handler-id-1" }, + preview: "response-preview-1", + delay: "delay-1", + foo: "foo-1", + }, + { + variantId: "route-2:variant-1", + routeId: "route-2", + constructor: { id: "handler-id-2" }, + preview: "response-preview-2", + delay: "delay-2", + foo: "foo-2", + }, + ]) + ).toEqual([ + { + id: "route-1:variant-1", + routeId: "route-1", + handler: "handler-id-1", + response: "response-preview-1", + delay: "delay-1", + }, + { + id: "route-2:variant-1", + routeId: "route-2", + handler: "handler-id-2", + response: "response-preview-2", + delay: "delay-2", + }, + ]); + }); }); describe("addCustomVariant", () => { @@ -674,6 +713,121 @@ describe("mocks helpers", () => { }); }); + describe("getVariantHandler for v4 variant handlers", () => { + it("should add an alert if variant is not valid", () => { + const variantHandler = getVariantHandler({ + route: {}, + variant: { + handler: "json", + response: "foo", + }, + variantIndex: 0, + routeHandlers: [JsonRoutesHandler], + core: coreMocks.stubs.instance, + alerts, + logger, + alertsRoutes, + loggerRoutes, + }); + + expect(variantHandler).toEqual(null); + expect(alerts.flat).toEqual([ + { + collection: "foo:0", + id: "validation", + value: { + message: + "Variant in route with id 'undefined' is invalid: Invalid 'response' property:: type must be object", + }, + }, + ]); + }); + + it("alert should include variant id if it is defined", () => { + const variantHandler = getVariantHandler({ + route: { id: "foo-route" }, + variant: { handler: "json", id: "foo-variant" }, + variantIndex: 0, + routeHandlers: [JsonRoutesHandler], + core: coreMocks.stubs.instance, + alerts, + logger, + alertsRoutes, + loggerRoutes, + }); + + expect(variantHandler).toEqual(null); + expect(alerts.flat).toEqual([ + { + collection: "foo:foo-variant", + id: "validation", + value: { + message: + "Variant with id 'foo-variant' in route with id 'foo-route' is invalid: Invalid 'response' property:: type must be object", + }, + }, + ]); + }); + + it("should return a Handler instance if variant is valid", () => { + const variantHandler = getVariantHandler({ + route: { ...VALID_ROUTE, delay: 3000 }, + variant: { ...VALID_VARIANT, handler: "json" }, + variantIndex: 0, + routeHandlers: [JsonRoutesHandler], + core: coreMocks.stubs.instance, + alerts, + logger, + alertsRoutes, + loggerRoutes, + }); + + expect(variantHandler).toBeInstanceOf(JsonRoutesHandler); + expect(variantHandler.delay).toEqual(3000); + expect(variantHandler.id).toEqual("foo-variant"); + expect(variantHandler.variantId).toEqual("foo-route:foo-variant"); + expect(variantHandler.routeId).toEqual("foo-route"); + expect(variantHandler.url).toEqual("/foo"); + expect(variantHandler.method).toEqual("POST"); + }); + + it("should add an Alert and return null is there is an error instantiating Handler", () => { + const variantHandler = getVariantHandler({ + route: { ...VALID_ROUTE, handler: "json", delay: 3000 }, + variant: { + ...VALID_VARIANT, + handler: "foo-handler", + }, + variantIndex: 0, + routeHandlers: [FooHandler, JsonRoutesHandler], + core: coreMocks.stubs.instance, + alerts, + logger, + alertsRoutes, + loggerRoutes, + }); + + expect(variantHandler).toEqual(null); + expect(alerts.flat[0].id).toEqual("process"); + expect(alerts.flat[0].value.message).toEqual("Error creating variant handler"); + }); + + it("should return variant delay if defined", () => { + const variantHandler = getVariantHandler({ + route: { ...VALID_ROUTE, delay: 3000 }, + variant: { ...VALID_VARIANT, handler: "json", delay: 5000 }, + variantIndex: 0, + routeHandlers: [JsonRoutesHandler], + core: coreMocks.stubs.instance, + alerts, + logger, + alertsRoutes, + loggerRoutes, + }); + expect(variantHandler.delay).toEqual(5000); + }); + }); + describe("getRouteVariants", () => { it("should add an alert if route is not valid", () => { const routeVariants = getRouteVariants({ diff --git a/packages/core/test/mocks/validations.spec.js b/packages/core/test/mocks/validations.spec.js index 1d4ba5d0d..975544c14 100644 --- a/packages/core/test/mocks/validations.spec.js +++ b/packages/core/test/mocks/validations.spec.js @@ -17,7 +17,9 @@ const { mockValidationErrors, mockRouteVariantsValidationErrors, } = require("../../src/mocks/validations"); -const DefaultRoutesHandler = require("../../src/routes-handlers/default/DefaultRoutesHandler"); +const DefaultRoutesHandler = require("../../src/routes-handlers/handlers/Default"); +const JsonRoutesHandler = require("../../src/routes-handlers/handlers/Json"); +const MiddlewareRoutesHandler = require("../../src/routes-handlers/handlers/Middleware"); describe("mocks validations", () => { const VALID_ROUTE = { @@ -241,7 +243,7 @@ describe("mocks validations", () => { }); describe("variantValidationErrors", () => { - it("should return null if Hanlder has not validationSchema", () => { + it("should return null if Handler has not validationSchema", () => { expect(variantValidationErrors({}, {}, {})).toEqual(null); }); }); @@ -306,6 +308,115 @@ describe("mocks validations", () => { }); }); + describe("variantValidationErrors using Json handler schema", () => { + it("should return null if variant is valid", () => { + expect( + variantValidationErrors({ id: "foo-route" }, VALID_VARIANT, JsonRoutesHandler) + ).toEqual(null); + }); + + it("should return error if variant has not response property and it has not id", () => { + const errors = variantValidationErrors( + { id: "foo-route" }, + { ...VALID_VARIANT, id: undefined, response: undefined }, + JsonRoutesHandler + ); + expect(errors.message).toEqual( + "Variant in route with id 'foo-route' is invalid: Invalid 'response' property:: type must be object" + ); + }); + + it("should return error if variant has not response property", () => { + const errors = variantValidationErrors( + { id: "foo-route" }, + { ...VALID_VARIANT, response: undefined }, + JsonRoutesHandler + ); + expect(errors.message).toEqual( + "Variant with id 'foo-variant' in route with id 'foo-route' is invalid: Invalid 'response' property:: type must be object" + ); + }); + + it("should return error if variant response headers is not an object", () => { + const errors = variantValidationErrors( + { id: "foo-route" }, + { + ...VALID_VARIANT, + response: { + headers: "foo", + }, + }, + JsonRoutesHandler + ); + expect(errors.message).toEqual( + "Variant with id 'foo-variant' in route with id 'foo-route' is invalid: Invalid 'response' property: must have required property 'body'. /headers: type must be object" + ); + }); + + it("should not allow defining variant response as a function", () => { + const errors = variantValidationErrors( + { id: "foo-route" }, + { + ...VALID_VARIANT, + response: () => { + // do nothing + }, + }, + JsonRoutesHandler + ); + expect(errors.message).toEqual( + "Variant with id 'foo-variant' in route with id 'foo-route' is invalid: Invalid 'response' property: Wrong type" + ); + }); + }); + + describe("variantValidationErrors using Middleware handler schema", () => { + const EMPTY_MIDDLEWARE = () => {}; + + it("should return null if variant is valid", () => { + expect( + variantValidationErrors( + { id: "foo-route" }, + { ...VALID_VARIANT, response: { middleware: EMPTY_MIDDLEWARE } }, + MiddlewareRoutesHandler + ) + ).toEqual(null); + }); + + it("should return error if variant has not response property and it has not id", () => { + const errors = variantValidationErrors( + { id: "foo-route" }, + { ...VALID_VARIANT, id: undefined, response: undefined }, + MiddlewareRoutesHandler + ); + expect(errors.message).toEqual( + "Variant in route with id 'foo-route' is invalid: Invalid 'response' property:: type must be object" + ); + }); + + it("should return error if variant has not response property", () => { + const errors = variantValidationErrors( + { id: "foo-route" }, + { ...VALID_VARIANT, response: undefined }, + MiddlewareRoutesHandler + ); + expect(errors.message).toEqual( + "Variant with id 'foo-variant' in route with id 'foo-route' is invalid: Invalid 'response' property:: type must be object" + ); + }); + + it("should not allow defining variant response without middleware property", () => { + const errors = variantValidationErrors( + { id: "foo-route" }, + VALID_VARIANT, + MiddlewareRoutesHandler + ); + expect(errors.message).toEqual( + "Variant with id 'foo-variant' in route with id 'foo-route' is invalid: Invalid 'response' property: must have required property 'middleware'" + ); + }); + }); + describe("mockValidationErrors", () => { it("should return null if mock is valid", () => { expect( @@ -416,7 +527,7 @@ describe("mocks validations", () => { ); }); - it("should wotk when no routeVariants are provided in mock", () => { + it("should work when no routeVariants are provided in mock", () => { const errors = mockRouteVariantsValidationErrors({ id: "foo", from: "foo-base", diff --git a/packages/core/test/routes-handlers/default/DefaultRoutesHandler.spec.js b/packages/core/test/routes-handlers/handlers/Default.spec.js similarity index 95% rename from packages/core/test/routes-handlers/default/DefaultRoutesHandler.spec.js rename to packages/core/test/routes-handlers/handlers/Default.spec.js index a5e163a5b..0b7cad625 100644 --- a/packages/core/test/routes-handlers/default/DefaultRoutesHandler.spec.js +++ b/packages/core/test/routes-handlers/handlers/Default.spec.js @@ -1,5 +1,5 @@ /* -Copyright 2021 Javier Brea +Copyright 2021-2022 Javier Brea Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -12,9 +12,9 @@ const sinon = require("sinon"); const { Logger } = require("@mocks-server/logger"); const CoreMocks = require("../../Core.mocks.js"); -const DefaultRoutesHandler = require("../../../src/routes-handlers/default/DefaultRoutesHandler"); +const DefaultRoutesHandler = require("../../../src/routes-handlers/handlers/Default"); -describe("DefaultRoutesHandler", () => { +describe("Default routes handler", () => { const FOO_ROUTE = { variantId: "foo-id", method: "POST", @@ -122,7 +122,7 @@ describe("DefaultRoutesHandler", () => { expect(fooResponseMethod.getCall(0).args[3]).toEqual(coreInstance); }); - it("should return function if response is a function", () => { + it("should return null in plainResponsePreview if response is a function", () => { defaultRoutesHandler = new DefaultRoutesHandler( { ...FOO_ROUTE, diff --git a/packages/core/test/routes-handlers/handlers/Json.spec.js b/packages/core/test/routes-handlers/handlers/Json.spec.js new file mode 100644 index 000000000..4cb5bcd33 --- /dev/null +++ b/packages/core/test/routes-handlers/handlers/Json.spec.js @@ -0,0 +1,91 @@ +/* +Copyright 2021-2022 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const sinon = require("sinon"); + +const CoreMocks = require("../../Core.mocks.js"); +const Json = require("../../../src/routes-handlers/handlers/Json"); + +describe("Json routes handler", () => { + const FOO_VARIANT = { + status: 200, + body: {}, + }; + let sandbox; + let coreMocks; + let coreInstance; + let routesHandler; + let expressStubs; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + expressStubs = { + req: { + id: "foo-request-id", + }, + res: { + status: sandbox.stub(), + send: sandbox.stub(), + set: sandbox.stub(), + }, + next: sandbox.stub(), + }; + coreMocks = new CoreMocks(); + coreInstance = coreMocks.stubs.instance; + routesHandler = new Json(FOO_VARIANT, coreInstance); + }); + + afterEach(() => { + sandbox.restore(); + coreMocks.restore(); + }); + + describe("id", () => { + it("should have json value", () => { + expect(Json.id).toEqual("json"); + }); + }); + + describe("version", () => { + it("should have 4 value", () => { + expect(Json.version).toEqual("4"); + }); + }); + + describe("validationSchema", () => { + it("should be defined", () => { + expect(Json.validationSchema).toBeDefined(); + }); + }); + + describe("preview", () => { + it("should return response body and status", () => { + expect(routesHandler.preview).toEqual({ + body: {}, + status: 200, + }); + }); + }); + + describe("middleware", () => { + it("should return response body and status", () => { + routesHandler.middleware(expressStubs.req, expressStubs.res, expressStubs.next); + expect(expressStubs.res.status.getCall(0).args[0]).toEqual(FOO_VARIANT.status); + expect(expressStubs.res.send.getCall(0).args[0]).toEqual(FOO_VARIANT.body); + }); + + it("should add headers if they are defined in response", () => { + const FOO_HEADERS = { foo: "foo" }; + routesHandler = new Json({ ...FOO_VARIANT, headers: FOO_HEADERS }, coreInstance); + routesHandler.middleware(expressStubs.req, expressStubs.res, expressStubs.next); + expect(expressStubs.res.set.getCall(0).args[0]).toEqual(FOO_HEADERS); + }); + }); +}); diff --git a/packages/core/test/routes-handlers/handlers/Middleware.spec.js b/packages/core/test/routes-handlers/handlers/Middleware.spec.js new file mode 100644 index 000000000..cf9051f20 --- /dev/null +++ b/packages/core/test/routes-handlers/handlers/Middleware.spec.js @@ -0,0 +1,89 @@ +/* +Copyright 2021-2022 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const sinon = require("sinon"); + +const CoreMocks = require("../../Core.mocks.js"); +const Middleware = require("../../../src/routes-handlers/handlers/Middleware"); + +describe("Json routes handler", () => { + const FOO_VARIANT = { + middleware: () => { + // do nothing + }, + }; + let sandbox; + let coreMocks; + let coreInstance; + let routesHandler; + let expressStubs; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + expressStubs = { + req: { + id: "foo-request-id", + }, + res: { + status: sandbox.stub(), + send: sandbox.stub(), + set: sandbox.stub(), + }, + next: sandbox.stub(), + }; + coreMocks = new CoreMocks(); + coreInstance = coreMocks.stubs.instance; + routesHandler = new Middleware(FOO_VARIANT, coreInstance); + }); + + afterEach(() => { + sandbox.restore(); + coreMocks.restore(); + }); + + describe("id", () => { + it("should have middleware value", () => { + expect(Middleware.id).toEqual("middleware"); + }); + }); + + describe("version", () => { + it("should have 4 value", () => { + expect(Middleware.version).toEqual("4"); + }); + }); + + describe("validationSchema", () => { + it("should be defined", () => { + expect(Middleware.validationSchema).toBeDefined(); + }); + }); + + describe("preview", () => { + it("should return null", () => { + expect(routesHandler.preview).toEqual(null); + }); + }); + + describe("middleware", () => { + it("should execute middleware function", () => { + const fooResponseMethod = sandbox.stub(); + routesHandler = new Middleware( + { ...FOO_VARIANT, middleware: fooResponseMethod }, + coreInstance + ); + routesHandler.middleware(expressStubs.req, expressStubs.res, expressStubs.next); + expect(fooResponseMethod.getCall(0).args[0]).toEqual(expressStubs.req); + expect(fooResponseMethod.getCall(0).args[1]).toEqual(expressStubs.res); + expect(fooResponseMethod.getCall(0).args[2]).toEqual(expressStubs.next); + expect(fooResponseMethod.getCall(0).args[3]).toEqual(coreInstance); + }); + }); +}); diff --git a/packages/core/test/server/middlewares.spec.js b/packages/core/test/server/middlewares.spec.js index cc5e0dd85..9f15c2581 100644 --- a/packages/core/test/server/middlewares.spec.js +++ b/packages/core/test/server/middlewares.spec.js @@ -57,7 +57,7 @@ describe("middlewares", () => { let loggerStub; beforeEach(() => { - loggerStub = sandbox.stub(logger, "verbose"); + loggerStub = sandbox.stub(logger, "debug"); }); it("should call to tracer verbose method, printing the request method", () => { diff --git a/packages/plugin-admin-api/jest.config.js b/packages/plugin-admin-api/jest.config.js index b0aef70ab..33305c705 100644 --- a/packages/plugin-admin-api/jest.config.js +++ b/packages/plugin-admin-api/jest.config.js @@ -26,7 +26,7 @@ module.exports = { // The glob patterns Jest uses to detect test files testMatch: ["/test/**/*.spec.js"], - // testMatch: ["/test/**/alerts-api.spec.js"], + // testMatch: ["/test/**/mock-custom-routes-variants-api.spec.js"], // The test environment that will be used for testing testEnvironment: "node", diff --git a/test/core-e2e-legacy/.gitignore b/test/core-e2e-legacy/.gitignore new file mode 100644 index 000000000..e5155b818 --- /dev/null +++ b/test/core-e2e-legacy/.gitignore @@ -0,0 +1,27 @@ +# See https://help.github.com/ignore-files/ for more about ignoring files. + +#environment variables +.env + +# dependencies +/node_modules + +# tests +/src/fixtures/temp +/src/fixtures/unexistant +/src/fixtures/mocks.config.js + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# ides +.idea +.vs diff --git a/test/core-e2e-legacy/.vscode/settings.json b/test/core-e2e-legacy/.vscode/settings.json new file mode 100644 index 000000000..0fffd7c33 --- /dev/null +++ b/test/core-e2e-legacy/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "sonarlint.connectedMode.project": { + "connectionId": "mocks-server", + "projectKey": "mocks-server_main_core-e2e" + } +} diff --git a/test/core-e2e-legacy/README.md b/test/core-e2e-legacy/README.md new file mode 100644 index 000000000..503d81dd0 --- /dev/null +++ b/test/core-e2e-legacy/README.md @@ -0,0 +1,8 @@ +[![Quality Gate][quality-gate-image]][quality-gate-url] + +## Introduction + +E2E tests of the `@mocks-server/core` project + +[quality-gate-image]: https://sonarcloud.io/api/project_badges/measure?project=mocks-server_main_core-e2e&metric=alert_status +[quality-gate-url]: https://sonarcloud.io/dashboard?id=mocks-server_main_core-e2e diff --git a/test/core-e2e-legacy/jest.config.js b/test/core-e2e-legacy/jest.config.js new file mode 100644 index 000000000..5f8d8f9eb --- /dev/null +++ b/test/core-e2e-legacy/jest.config.js @@ -0,0 +1,18 @@ +// For a detailed explanation regarding each configuration property, visit: +// https://jestjs.io/docs/en/configuration.html + +module.exports = { + // Automatically clear mock calls and instances between every test + clearMocks: true, + + testMatch: ["/src/**/*.spec.js"], + // testMatch: ["/src/**/load-mocks-and-routes.spec.js"], + + // Indicates whether the coverage information should be collected while executing the test + collectCoverage: false, + + // The test environment that will be used for testing + testEnvironment: "node", + + setupFilesAfterEnv: ["./jest.setup.js"], +}; diff --git a/test/core-e2e-legacy/jest.setup.js b/test/core-e2e-legacy/jest.setup.js new file mode 100644 index 000000000..024e468bc --- /dev/null +++ b/test/core-e2e-legacy/jest.setup.js @@ -0,0 +1,4 @@ +/* global jest */ +// import "regenerator-runtime/runtime"; + +jest.setTimeout(20000); diff --git a/test/core-e2e-legacy/package.json b/test/core-e2e-legacy/package.json new file mode 100644 index 000000000..7bf0ecd5e --- /dev/null +++ b/test/core-e2e-legacy/package.json @@ -0,0 +1,11 @@ +{ + "private": true, + "scripts": { + "test:e2e": "jest --runInBand --detectOpenHandles" + }, + "dependencies": { + "@mocks-server/core": "workspace:*", + "@mocks-server/cli-runner": "workspace:*", + "express": "4.17.3" + } +} diff --git a/test/core-e2e-legacy/project.json b/test/core-e2e-legacy/project.json new file mode 100644 index 000000000..e309da593 --- /dev/null +++ b/test/core-e2e-legacy/project.json @@ -0,0 +1,6 @@ +{ + "root": "test/core-e2e-legacy/", + "projectType": "application", + "implicitDependencies": [], + "tags": ["type:test"] +} diff --git a/test/core-e2e-legacy/src/config-file-with-custom-route-handler.spec.js b/test/core-e2e-legacy/src/config-file-with-custom-route-handler.spec.js new file mode 100644 index 000000000..edaf3501d --- /dev/null +++ b/test/core-e2e-legacy/src/config-file-with-custom-route-handler.spec.js @@ -0,0 +1,56 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + mocksRunner, + fetch, + waitForServer, + fixturesFolder, + removeConfigFile, +} = require("./support/helpers"); + +describe("when adding route handlers in config file", () => { + let mocks; + + describe("When started", () => { + beforeAll(async () => { + mocks = mocksRunner([], { + cwd: fixturesFolder("config-file-with-routes-handler"), + }); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await mocks.kill(); + }); + + it("should have log level silly", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("[silly]")); + }); + + it("should serve users in /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("custom handler should have traced", async () => { + expect(mocks.logs.current).toEqual( + expect.stringContaining( + 'Responding with custom route handler to route variant "get-users:custom-success"' + ) + ); + expect(mocks.logs.current).toEqual(expect.stringContaining("Custom request GET =>")); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/config-file-with-plugins.spec.js b/test/core-e2e-legacy/src/config-file-with-plugins.spec.js new file mode 100644 index 000000000..bf653bd9f --- /dev/null +++ b/test/core-e2e-legacy/src/config-file-with-plugins.spec.js @@ -0,0 +1,91 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const fsExtra = require("fs-extra"); +const { + mocksRunner, + fetch, + waitForServer, + fixturesFolder, + wait, + removeConfigFile, +} = require("./support/helpers"); + +describe("when adding plugins in config file", () => { + let mocks; + + describe("When started", () => { + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.copy(fixturesFolder("web-tutorial"), fixturesFolder("temp")); + mocks = mocksRunner([], { + cwd: fixturesFolder("config-file-with-plugins"), + }); + await waitForServer(); + await wait(3000); + }); + + afterAll(async () => { + removeConfigFile(); + await fsExtra.remove(fixturesFolder("temp")); + await mocks.kill(); + }); + + it("should have log level silly", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("[silly]")); + }); + + it("should have loaded TraceMocksPlugin", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("traceMocks plugin started")); + expect(mocks.logs.current).toEqual(expect.stringContaining("There are 3 mocks available")); + }); + + it("should have loaded TraceRoutesPlugin", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("traceRoutes plugin started")); + expect(mocks.logs.current).toEqual(expect.stringContaining("There are 2 routes available")); + }); + + it("should serve users in /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 2 in /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should serve user 2 in /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should reload mocks when files are modified", async () => { + await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); + await wait(4000); + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe modified" }, + { id: 2, name: "Jane Doe modified" }, + ]); + }); + + it("should have traced new mocks amount", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("There are 4 mocks available")); + }); + + it("should have traced new routes amount", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("There are 3 routes available")); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/config-file.spec.js b/test/core-e2e-legacy/src/config-file.spec.js new file mode 100644 index 000000000..f3ef8c922 --- /dev/null +++ b/test/core-e2e-legacy/src/config-file.spec.js @@ -0,0 +1,72 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const fsExtra = require("fs-extra"); +const { + mocksRunner, + fetch, + waitForServer, + fixturesFolder, + wait, + removeConfigFile, +} = require("./support/helpers"); + +describe("when using config file", () => { + let mocks; + + describe("When started", () => { + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.copy(fixturesFolder("web-tutorial"), fixturesFolder("temp")); + mocks = mocksRunner([], { + cwd: fixturesFolder("config-file"), + }); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await fsExtra.remove(fixturesFolder("temp")); + await mocks.kill(); + }); + + it("should have log level silly", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("[silly]")); + }); + + it("should serve users in /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 2 in /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should serve user 2 in /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should have watch disabled", async () => { + await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); + await wait(4000); + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/cors-argument.spec.js b/test/core-e2e-legacy/src/cors-argument.spec.js new file mode 100644 index 000000000..ad5137dee --- /dev/null +++ b/test/core-e2e-legacy/src/cors-argument.spec.js @@ -0,0 +1,123 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); + +describe("cors command line argument", () => { + let cli; + + describe("when cors is enabled", () => { + beforeEach(async () => { + cli = mocksRunner(["--files.path=multi-methods"]); + await waitForServer(); + }); + + afterEach(async () => { + removeConfigFile(); + await cli.kill(); + }); + + it("cors middleware should handle OPTIONS requests", async () => { + const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + expect(users.status).toEqual(204); + expect(users.headers.get("access-control-allow-origin")).toEqual("*"); + expect(users.headers.get("access-control-allow-methods")).toEqual( + "GET,HEAD,PUT,PATCH,POST,DELETE" + ); + expect(users.body).toEqual(null); + }); + + it("cors middleware should handle OPTIONS requests to all paths without route", async () => { + const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + expect(users.status).toEqual(204); + expect(users.headers.get("access-control-allow-origin")).toEqual("*"); + expect(users.headers.get("access-control-allow-methods")).toEqual( + "GET,HEAD,PUT,PATCH,POST,DELETE" + ); + expect(users.body).toEqual(null); + }); + + it("Response headers should include cors headers", async () => { + const users = await fetch(`/api/users/1`); + expect(users.headers.get("access-control-allow-origin")).toEqual("*"); + }); + }); + + describe("when cors is disabled", () => { + beforeAll(async () => { + cli = mocksRunner(["--files.path=multi-methods", "--no-server.cors.enabled"]); + await waitForServer(); + }); + + afterAll(async () => { + await cli.kill(); + }); + + it("route middleware should handle OPTIONS request", async () => { + const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + expect(users.status).toEqual(200); + expect(users.headers.get("access-control-allow-origin")).toEqual(null); + expect(users.headers.get("access-control-allow-methods")).toEqual(null); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("cors middleware should not handle OPTIONS requests to paths without route", async () => { + const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + expect(users.status).toEqual(404); + expect(users.headers.get("access-control-allow-origin")).toEqual(null); + expect(users.headers.get("access-control-allow-methods")).toEqual(null); + expect(users.body).toEqual({ error: "Not Found", message: "Not Found", statusCode: 404 }); + }); + + it("Response headers should not include cors headers", async () => { + const users = await fetch(`/api/users/1`); + expect(users.headers.get("access-control-allow-origin")).toEqual(null); + }); + }); + + describe("when cors is enabled but corsPreflight disabled", () => { + beforeEach(async () => { + cli = mocksRunner([ + "--files.path=multi-methods", + '--server.cors.options={"preflightContinue":true}', + ]); + await waitForServer(); + }); + + afterEach(async () => { + await cli.kill(); + }); + + it("route middleware should handle OPTIONS request", async () => { + const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + expect(users.status).toEqual(200); + expect(users.headers.get("access-control-allow-origin")).toEqual("*"); + expect(users.headers.get("access-control-allow-methods")).toEqual( + "GET,HEAD,PUT,PATCH,POST,DELETE" + ); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("cors middleware should not handle OPTIONS requests to paths without route", async () => { + const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + expect(users.status).toEqual(404); + expect(users.headers.get("access-control-allow-origin")).toEqual("*"); + expect(users.headers.get("access-control-allow-methods")).toEqual( + "GET,HEAD,PUT,PATCH,POST,DELETE" + ); + expect(users.body).toEqual({ error: "Not Found", message: "Not Found", statusCode: 404 }); + }); + + it("Response headers should include cors headers", async () => { + const users = await fetch(`/api/users/1`); + expect(users.headers.get("access-control-allow-origin")).toEqual("*"); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/custom-router.spec.js b/test/core-e2e-legacy/src/custom-router.spec.js new file mode 100644 index 000000000..5b6a3314c --- /dev/null +++ b/test/core-e2e-legacy/src/custom-router.spec.js @@ -0,0 +1,114 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const express = require("express"); + +const Core = require("@mocks-server/core"); +const { fetch, fixturesFolder, waitForServer, removeConfigFile } = require("./support/helpers"); + +describe("when using custom router", () => { + const customRouter = express.Router(); + customRouter.get("/", (_req, res) => { + res.status(200); + res.send({ + customRouterListening: true, + }); + }); + let core; + + describe("and registering it before initializating the server", () => { + beforeAll(async () => { + core = new Core(); + core.addRouter("/api/custom", customRouter); + await core.init({ + config: { + readFile: false, + readArguments: false, + readEnvironment: false, + }, + log: "silent", + files: { + path: fixturesFolder("web-tutorial"), + watch: false, + }, + }); + await core.start(); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + it("custom router should be listening", async () => { + const response = await fetch("/api/custom"); + expect(response.body.customRouterListening).toEqual(true); + }); + + it("fixtures routers should be listening", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("custom router should stop listening when is removed", async () => { + await core.removeRouter("/api/custom", customRouter); + const response = await fetch("/api/custom"); + expect(response.status).toEqual(404); + }); + }); + + describe("and registering it after server is started", () => { + beforeAll(async () => { + core = new Core(); + await core.init({ + config: { + readFile: false, + readArguments: false, + readEnvironment: false, + }, + log: "silent", + files: { + path: fixturesFolder("web-tutorial"), + watch: false, + }, + }); + await core.start(); + await waitForServer(); + await core.addRouter("/api/custom", customRouter); + }); + + afterAll(async () => { + await core.stop(); + }); + + it("custom router should be listening", async () => { + const response = await fetch("/api/custom"); + expect(response.body.customRouterListening).toEqual(true); + }); + + it("mocks routers should be listening", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("custom router should stop listening when is removed", async () => { + await core.removeRouter("/api/custom", customRouter); + const response = await fetch("/api/custom"); + expect(response.status).toEqual(404); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/delay-argument.spec.js b/test/core-e2e-legacy/src/delay-argument.spec.js new file mode 100644 index 000000000..167798f9f --- /dev/null +++ b/test/core-e2e-legacy/src/delay-argument.spec.js @@ -0,0 +1,43 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + mocksRunner, + fetch, + waitForServer, + TimeCounter, + removeConfigFile, +} = require("./support/helpers"); + +describe("delay argument", () => { + let mocks; + + beforeAll(async () => { + mocks = mocksRunner(["--files.path=web-tutorial", "--mocks.delay=1000"]); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await mocks.kill(); + }); + + it("should set delay", async () => { + expect.assertions(2); + const timeCounter = new TimeCounter(); + const users = await fetch("/api/users"); + timeCounter.stop(); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + expect(timeCounter.total).toBeGreaterThan(999); + }); +}); diff --git a/test/core-e2e-legacy/src/delay-setting.spec.js b/test/core-e2e-legacy/src/delay-setting.spec.js new file mode 100644 index 000000000..f63de8015 --- /dev/null +++ b/test/core-e2e-legacy/src/delay-setting.spec.js @@ -0,0 +1,164 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + startCore, + fetch, + TimeCounter, + waitForServer, + removeConfigFile, +} = require("./support/helpers"); + +describe("delay setting", () => { + let core; + + beforeAll(async () => { + core = await startCore("delays"); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("When started", () => { + it("should respond with no delay", async () => { + const timeCounter = new TimeCounter(); + await fetch("/api/users"); + timeCounter.stop(); + expect(timeCounter.total).toBeLessThan(400); + }); + }); + + describe("When delay setting is changed", () => { + it("should respond after defined delay", async () => { + core.config.namespace("mocks").option("delay").value = 1000; + const timeCounter = new TimeCounter(); + await fetch("/api/users"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(999); + }); + }); + + describe("When route variant has delay", () => { + it("should respond after route variant defined delay", async () => { + core.mocks.useRouteVariant("get-users:delayed"); + const timeCounter = new TimeCounter(); + await fetch("/api/users"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(1999); + }); + + it("should respond with same delay after setting delay to zero", async () => { + core.config.namespace("mocks").option("delay").value = 0; + const timeCounter = new TimeCounter(); + await fetch("/api/users"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(1999); + }); + + it("should respond with same delay after setting delay to 4000", async () => { + core.config.namespace("mocks").option("delay").value = 4000; + const timeCounter = new TimeCounter(); + await fetch("/api/users"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(1999); + }); + }); + + describe("When route has delay", () => { + it("should respond after route defined delay", async () => { + const timeCounter = new TimeCounter(); + await fetch("/api/users/1"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(999); + }); + + it("should respond with same delay after setting delay to zero", async () => { + core.config.namespace("mocks").option("delay").value = 0; + const timeCounter = new TimeCounter(); + await fetch("/api/users/1"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(999); + }); + }); + + describe("When route has delay and also route variant", () => { + it("should respond after route variant defined delay", async () => { + core.config.namespace("mocks").option("selected").value = "user-2"; + const timeCounter = new TimeCounter(); + await fetch("/api/users/1"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(1999); + }); + }); + + describe("When route has delay and route variant has zero delay", () => { + afterEach(() => { + core.mocks.restoreRoutesVariants(); + }); + + it("should respond with no delay", async () => { + core.mocks.useRouteVariant("get-user:zero-delay"); + const timeCounter = new TimeCounter(); + await fetch("/api/users/1"); + timeCounter.stop(); + expect(timeCounter.total).toBeLessThan(500); + }); + + it("should have zero delay in plain route variant", async () => { + expect(core.mocks.plainRoutesVariants[2]).toEqual({ + handler: "default", + id: "get-user:zero-delay", + delay: 0, + response: { + status: 200, + body: { + id: 1, + name: "John Doe", + }, + }, + routeId: "get-user", + }); + }); + }); + + describe("When route has delay and route variant has null delay", () => { + afterEach(() => { + core.mocks.restoreRoutesVariants(); + }); + + it("should respond with global server delay", async () => { + core.config.namespace("mocks").option("delay").value = 3000; + core.mocks.useRouteVariant("get-user:null-delay"); + const timeCounter = new TimeCounter(); + await fetch("/api/users/1"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(2999); + }); + + it("should have null delay in plain route variant", async () => { + expect(core.mocks.plainRoutesVariants[3]).toEqual({ + handler: "default", + id: "get-user:null-delay", + delay: null, + response: { + status: 200, + body: { + id: 1, + name: "John Doe", + }, + }, + routeId: "get-user", + }); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/esm.spec.js b/test/core-e2e-legacy/src/esm.spec.js new file mode 100644 index 000000000..99f3c2bf0 --- /dev/null +++ b/test/core-e2e-legacy/src/esm.spec.js @@ -0,0 +1,97 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const fsExtra = require("fs-extra"); +const { + mocksRunner, + fetch, + waitForServer, + fixturesFolder, + wait, + removeConfigFile, +} = require("./support/helpers"); + +describe("when babelRegister is enabled and esm files are used", () => { + let mocks; + + describe("When started", () => { + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.copy(fixturesFolder("esm"), fixturesFolder("temp")); + mocks = mocksRunner([], { + cwd: fixturesFolder("esm-config"), + }); + await waitForServer(); + await wait(3000); + }); + + afterAll(async () => { + removeConfigFile(); + await fsExtra.remove(fixturesFolder("temp")); + await mocks.kill(); + }); + + it("should have log level silly", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("[silly]")); + }); + + it("should serve users in /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("middleware should trace request and add headers", async () => { + const users = await fetch("/api/users"); + expect(users.headers.get("x-mocks-server-example")).toEqual("custom-header"); + expect(mocks.logs.current).toEqual( + expect.stringContaining( + "Custom header added by add-headers:enabled route variant middleware" + ) + ); + }); + + it("should serve user 1 in /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 2 in /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should reload mocks when files are modified", async () => { + await fsExtra.copy(fixturesFolder("esm-modified"), fixturesFolder("temp")); + await wait(4000); + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe modified" }, + { id: 2, name: "Jane Doe modified" }, + ]); + }); + + it("should serve new users route", async () => { + const users = await fetch("/api/new-users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe modified" }, + { id: 2, name: "Jane Doe modified" }, + { id: 3, name: "Brand new user" }, + ]); + }); + + it("middleware should be disabled", async () => { + const users = await fetch("/api/users"); + expect(users.headers.get("x-mocks-server-example")).toEqual(null); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/events.spec.js b/test/core-e2e-legacy/src/events.spec.js new file mode 100644 index 000000000..aea9f5d06 --- /dev/null +++ b/test/core-e2e-legacy/src/events.spec.js @@ -0,0 +1,111 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const sinon = require("sinon"); + +const { + startCore, + fetch, + waitForServer, + wait, + fixturesFolder, + waitForServerUrl, + removeConfigFile, +} = require("./support/helpers"); + +describe("events", () => { + let core; + let sandbox; + let spies; + + beforeAll(async () => { + sandbox = sinon.createSandbox(); + spies = { + onChangeMocks: sandbox.spy(), + onChangeSettings: sandbox.spy(), + onChangeAlerts: sandbox.spy(), + }; + core = await startCore("web-tutorial", { mocks: { selected: "base" } }); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + sandbox.restore(); + await core.stop(); + }); + + describe("When started", () => { + it("should load web-tutorial mocks", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); + + describe("When setting is changed", () => { + let option, removeSpy; + + it("should have emitted event", async () => { + option = core.config.option("log"); + removeSpy = option.onChange(spies.onChangeSettings); + option.value = "silly"; + await wait(500); + expect(spies.onChangeSettings.getCall(0).args[0]).toEqual("silly"); + }); + + it("should not execute callback when event listener is removed", async () => { + removeSpy(); + option.value = "debug"; + await wait(500); + expect(spies.onChangeSettings.callCount).toEqual(1); + }); + }); + + describe("When mock is changed", () => { + let option, removeSpy; + + it("should have emitted event", async () => { + option = core.config.namespace("files").option("path"); + removeSpy = core.onChangeMocks(spies.onChangeMocks); + option.value = fixturesFolder("web-tutorial-modified"); + await waitForServerUrl("/api/new-users"); + expect(spies.onChangeMocks.callCount).toEqual(2); + }); + + it("should not execute callback when event listener is removed", async () => { + removeSpy(); + option.value = fixturesFolder("web-tutorial"); + await wait(5000); + expect(spies.onChangeMocks.callCount).toEqual(2); + }); + }); + + describe("When alerts are added", () => { + let option, removeSpy; + + it("should have emitted event", async () => { + option = core.config.namespace("mocks").option("selected"); + removeSpy = core.onChangeAlerts(spies.onChangeAlerts); + option.value = "unexistant"; + await wait(500); + expect(spies.onChangeAlerts.callCount).toEqual(1); + }); + + it("should not execute callback when event listener is removed", async () => { + removeSpy(); + option.value = "unexistant2"; + await wait(500); + expect(spies.onChangeAlerts.callCount).toEqual(1); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/files-error.spec.js b/test/core-e2e-legacy/src/files-error.spec.js new file mode 100644 index 000000000..7a89539b7 --- /dev/null +++ b/test/core-e2e-legacy/src/files-error.spec.js @@ -0,0 +1,101 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { startCore, waitForServer, findAlert, removeConfigFile } = require("./support/helpers"); + +describe("when there is an error loading files", () => { + let core; + + describe("error in mocks file", () => { + beforeAll(async () => { + core = await startCore("files-error-mock"); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + it("should have two routes", async () => { + expect(core.mocks.plainRoutes.length).toEqual(2); + }); + + it("should have four route variants", async () => { + expect(core.mocks.plainRoutesVariants.length).toEqual(5); + }); + + it("should have no mocks", async () => { + expect(core.mocks.plainMocks.length).toEqual(0); + }); + + it("should have added an alert about not mock found", async () => { + expect(findAlert("mocks:empty", core.alerts).message).toEqual("No mocks found"); + }); + + it("should have added an alert about error loading mocks", async () => { + expect(findAlert("files:mocks", core.alerts).message).toEqual( + expect.stringContaining("Error loading mocks from file") + ); + }); + }); + + describe("error in routes folder", () => { + beforeAll(async () => { + core = await startCore("files-error-routes"); + await waitForServer(); + }); + + afterAll(async () => { + await core.stop(); + }); + + it("should have three mocks", async () => { + expect(core.mocks.plainMocks.length).toEqual(3); + }); + + it("should have zero routes", async () => { + expect(core.mocks.plainRoutes.length).toEqual(0); + }); + + it("should have zero route variants", async () => { + expect(core.mocks.plainRoutesVariants.length).toEqual(0); + }); + + it("should have added an alert about error loading routes", async () => { + expect(findAlert("files:routes", core.alerts).message).toEqual( + expect.stringContaining("Error loading routes from folder") + ); + }); + + it("mocks should not have routes variants", () => { + expect(core.mocks.plainMocks).toEqual([ + { + id: "base", + from: null, + routesVariants: ["get-users:success", "get-user:1"], + appliedRoutesVariants: [], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + appliedRoutesVariants: [], + }, + { + id: "user-real", + from: "base", + routesVariants: ["get-user:real"], + appliedRoutesVariants: [], + }, + ]); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/fixtures/config-file-no-path/mocks.config.js b/test/core-e2e-legacy/src/fixtures/config-file-no-path/mocks.config.js new file mode 100644 index 000000000..e1c7a6958 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-no-path/mocks.config.js @@ -0,0 +1,11 @@ +const path = require("path"); + +module.exports = { + log: "silly", + mocks: { + selected: "user-real", + }, + files: { + watch: false, + }, +}; diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/TraceMocksPlugin.js b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/TraceMocksPlugin.js new file mode 100644 index 000000000..405b8b987 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/TraceMocksPlugin.js @@ -0,0 +1,55 @@ +class Plugin { + static get id() { + return "trace-mocks"; + } + + constructor({ core, config }) { + this._traceMocks = config.addOption({ + name: "traceMocks", + type: "boolean", + description: "Trace mocks count", + default: true, + }); + this._traceMocks.onChange(this._onChangeTraceMocks.bind(this)); + + this._core = core; + this._onChangeMocks = this._onChangeMocks.bind(this); + } + + get displayName() { + return "trace-mocks"; + } + + init({ core }) { + this._enabled = this._traceMocks.value; + this._removeChangeMocksListener = core.onChangeMocks(this._onChangeMocks); + core.tracer.debug(`traceMocks initial value is ${this._traceMocks.value}`); + } + + traceMocks() { + if (this._enabled && this._started) { + this._core.tracer.info(`There are ${this._core.mocks.plainMocks.length} mocks available`); + } + } + + start({ core }) { + this._started = true; + core.tracer.debug("traceMocks plugin started"); + this.traceMocks(); + } + + stop({ core }) { + this._started = false; + core.tracer.debug("traceMocks plugin stopped"); + } + + _onChangeTraceMocks() { + this._enabled = this._traceMocks.value; + } + + _onChangeMocks() { + this.traceMocks(); + } +} + +module.exports = Plugin; diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/TraceRoutesPlugin.js b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/TraceRoutesPlugin.js new file mode 100644 index 000000000..2a2b98e55 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/TraceRoutesPlugin.js @@ -0,0 +1,55 @@ +class Plugin { + static get id() { + return "trace-routes"; + } + + constructor({ core, config }) { + this._traceRoutes = config.addOption({ + name: "traceRoutes", + type: "boolean", + description: "Trace routes count", + default: true, + }); + this._traceRoutes.onChange(this._onChangeTraceRoutes.bind(this)); + + this._core = core; + this._onChangeMocks = this._onChangeMocks.bind(this); + } + + get displayName() { + return "trace-routes"; + } + + init({ core }) { + this._enabled = this._traceRoutes.value; + this._removeChangeMocksListener = core.onChangeMocks(this._onChangeMocks); + core.tracer.debug(`traceRoutes initial value is ${this._traceRoutes.value}`); + } + + traceRoutes() { + if (this._enabled && this._started) { + this._core.tracer.info(`There are ${this._core.mocks.plainRoutes.length} routes available`); + } + } + + start({ core }) { + this._started = true; + core.tracer.debug("traceRoutes plugin started"); + this.traceRoutes(); + } + + stop({ core }) { + this._started = false; + core.tracer.debug("traceRoutes plugin stopped"); + } + + _onChangeTraceRoutes() { + this._enabled = this._traceRoutes.value; + } + + _onChangeMocks() { + this.traceRoutes(); + } +} + +module.exports = Plugin; diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks-server-with-plugins.config.js b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks-server-with-plugins.config.js new file mode 100644 index 000000000..46ca0bc97 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks-server-with-plugins.config.js @@ -0,0 +1,15 @@ +const path = require("path"); +const TraceRoutesPlugin = require("./TraceRoutesPlugin"); + +module.exports = { + log: "silly", + files: { + path: path.resolve(__dirname, "..", "temp"), + }, + plugins: { + register: [TraceRoutesPlugin], + }, + mocks: { + selected: "user-2", + }, +}; diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/mocks.json b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/mocks.json new file mode 100644 index 000000000..04f0b6fc5 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/mocks.json @@ -0,0 +1,16 @@ +[ + { + "id": "base", + "routesVariants": ["add-headers:enabled", "get-users:success", "get-user:success"] + }, + { + "id": "no-headers", + "from": "base", + "routesVariants": ["add-headers:disabled"] + }, + { + "id": "user-real", + "from": "no-headers", + "routesVariants": ["get-user:real"] + } +] diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/routes/middlewares.js b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/routes/middlewares.js new file mode 100644 index 000000000..a15baef77 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/routes/middlewares.js @@ -0,0 +1,27 @@ +// Use this file only as a guide for first steps using middlewares. Delete it when no more needed. +// For a detailed explanation about using middlewares, visit: +// https://mocks-server.org/docs/guides-using-middlewares + +module.exports = [ + { + id: "add-headers", + url: "*", + method: "GET", + variants: [ + { + id: "enabled", + response: (req, res, next, mocksServer) => { + res.set("x-mocks-server-example", "some-value"); + mocksServer.tracer.info( + "Custom header added by add-headers:enabled route variant middleware" + ); + next(); + }, + }, + { + id: "disabled", + response: (req, res, next) => next(), + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/routes/users.js b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/routes/users.js new file mode 100644 index 000000000..473241e3c --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/mocks/routes/users.js @@ -0,0 +1,72 @@ +// Use this file only as a guide for first steps. Delete it when you have added your own routes files. +// For a detailed explanation regarding each routes property, visit: +// https://mocks-server.org/docs/get-started-routes + +// users data +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +module.exports = [ + { + id: "get-users", // id of the route + url: "/api/users", // url in express format + method: "GET", // HTTP method + variants: [ + { + id: "success", // id of the variant + response: { + status: 200, // status to send + body: USERS, // body to send + }, + }, + { + id: "error", // id of the variant + response: { + status: 400, // status to send + body: { + // body to send + message: "Error", + }, + }, + }, + ], + }, + { + id: "get-user", // id of the route + url: "/api/users/:id", // url in express format + method: "GET", // HTTP method + variants: [ + { + id: "success", // id of the variant + response: { + status: 200, // status to send + body: USERS[0], // body to send + }, + }, + { + id: "real", // id of the variant + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/starter b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/starter new file mode 100755 index 000000000..ca0e57fa5 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-plugins/starter @@ -0,0 +1,29 @@ +#!/usr/bin/env node +"use strict"; + +const Core = require("@mocks-server/core"); +const TraceMocksPlugin = require("./TraceMocksPlugin"); + +const handleError = (error) => { + console.error(`Error: ${error.message}`); + process.exitCode = 1; +}; + +const start = () => { + try { + const mocksServer = new Core({ + plugins: { + register: [TraceMocksPlugin] + }, + config: { + fileSearchPlaces: ["mocks-server-with-plugins.config.js"], + } + }); + + return mocksServer.start().catch(handleError); + } catch (error) { + return handleError(error); + } +}; + +start(); diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/CustomRoutesHandler.js b/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/CustomRoutesHandler.js new file mode 100644 index 000000000..d28443a10 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/CustomRoutesHandler.js @@ -0,0 +1,43 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +"use strict"; + +class CustomRoutesHandler { + static get id() { + return "custom"; + } + + constructor(route, core) { + this._method = route.method; + this._url = route.url; + this._response = route.response; + this._variantId = route.variantId; + this._core = core; + } + + middleware(req, res, next) { + this._core.tracer.debug( + `Responding with custom route handler to route variant "${this._variantId}" | req: ${req.id}` + ); + this._core.tracer.info(`Custom request ${req.method} => ${req.url} => "${this._variantId}"`); + res.status(this._response.status); + res.send(this._response.body); + } + + get plainResponsePreview() { + return { + body: this._response.body, + status: this._response.status, + }; + } +} + +module.exports = CustomRoutesHandler; diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/mocks.config.js b/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/mocks.config.js new file mode 100644 index 000000000..6fc50863b --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/mocks.config.js @@ -0,0 +1,13 @@ +const path = require("path"); +const CustomRoutesHandler = require("./CustomRoutesHandler"); + +module.exports = { + log: "silly", + routesHandlers: [CustomRoutesHandler], + mocks: { + selected: "custom-users", + }, + files: { + path: path.resolve(__dirname, "..", "custom-routes-handler"), + } +}; diff --git a/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/starter b/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/starter new file mode 100755 index 000000000..05af5e688 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file-with-routes-handler/starter @@ -0,0 +1,21 @@ +#!/usr/bin/env node +"use strict"; + +const Core = require("@mocks-server/core"); + +const handleError = (error) => { + console.error(`Error: ${error.message}`); + process.exitCode = 1; +}; + +const start = () => { + try { + const mocksServer = new Core(); + + return mocksServer.start().catch(handleError); + } catch (error) { + return handleError(error); + } +}; + +start(); diff --git a/test/core-e2e-legacy/src/fixtures/config-file/mocks.config.js b/test/core-e2e-legacy/src/fixtures/config-file/mocks.config.js new file mode 100644 index 000000000..63fadcf32 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file/mocks.config.js @@ -0,0 +1,12 @@ +const path = require("path"); + +module.exports = { + log: "silly", + mocks: { + selected: "user-2", + }, + files: { + path: path.resolve(__dirname, "..", "temp"), + watch: false, + }, +}; diff --git a/test/core-e2e-legacy/src/fixtures/config-file/starter b/test/core-e2e-legacy/src/fixtures/config-file/starter new file mode 100755 index 000000000..05af5e688 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/config-file/starter @@ -0,0 +1,21 @@ +#!/usr/bin/env node +"use strict"; + +const Core = require("@mocks-server/core"); + +const handleError = (error) => { + console.error(`Error: ${error.message}`); + process.exitCode = 1; +}; + +const start = () => { + try { + const mocksServer = new Core(); + + return mocksServer.start().catch(handleError); + } catch (error) { + return handleError(error); + } +}; + +start(); diff --git a/test/core-e2e-legacy/src/fixtures/custom-routes-handler/db/users.js b/test/core-e2e-legacy/src/fixtures/custom-routes-handler/db/users.js new file mode 100644 index 000000000..f05a501f6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/custom-routes-handler/db/users.js @@ -0,0 +1,24 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +module.exports = { + USERS, +}; diff --git a/test/core-e2e-legacy/src/fixtures/custom-routes-handler/mocks.js b/test/core-e2e-legacy/src/fixtures/custom-routes-handler/mocks.js new file mode 100644 index 000000000..fedbddfde --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/custom-routes-handler/mocks.js @@ -0,0 +1,21 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +module.exports = [ + { + id: "base", + routesVariants: ["get-users:success"], + }, + { + id: "custom-users", + from: "base", + routesVariants: ["get-users:custom-success"], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/custom-routes-handler/routes/users.js b/test/core-e2e-legacy/src/fixtures/custom-routes-handler/routes/users.js new file mode 100644 index 000000000..13b7cb0d1 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/custom-routes-handler/routes/users.js @@ -0,0 +1,36 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-users", + url: "/api/users", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: USERS, + }, + }, + { + id: "custom-success", + handler: "custom", + response: { + status: 200, + body: USERS, + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/delays/db/users.js b/test/core-e2e-legacy/src/fixtures/delays/db/users.js new file mode 100644 index 000000000..f05a501f6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/delays/db/users.js @@ -0,0 +1,24 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +module.exports = { + USERS, +}; diff --git a/test/core-e2e-legacy/src/fixtures/delays/mocks.js b/test/core-e2e-legacy/src/fixtures/delays/mocks.js new file mode 100644 index 000000000..e12b01264 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/delays/mocks.js @@ -0,0 +1,21 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +module.exports = [ + { + id: "base", + routesVariants: ["get-users:success", "get-user:1"], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/delays/routes/user.js b/test/core-e2e-legacy/src/fixtures/delays/routes/user.js new file mode 100644 index 000000000..e66bd28d5 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/delays/routes/user.js @@ -0,0 +1,53 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-user", + url: "/api/users/:id", + method: "GET", + delay: 1000, + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + delay: 2000, + response: { + status: 200, + body: USERS[1], + }, + }, + { + id: "zero-delay", + delay: 0, + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "null-delay", + delay: null, + response: { + status: 200, + body: USERS[0], + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/delays/routes/users.js b/test/core-e2e-legacy/src/fixtures/delays/routes/users.js new file mode 100644 index 000000000..4957b82bb --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/delays/routes/users.js @@ -0,0 +1,36 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-users", + url: "/api/users", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: USERS, + }, + }, + { + id: "delayed", + delay: 2000, + response: { + status: 200, + body: USERS, + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/esm-config/babel.config.js b/test/core-e2e-legacy/src/fixtures/esm-config/babel.config.js new file mode 100644 index 000000000..1108df09e --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm-config/babel.config.js @@ -0,0 +1,3 @@ +module.exports = { + presets: ["@babel/env", "@babel/typescript"], +}; diff --git a/test/core-e2e-legacy/src/fixtures/esm-config/mocks.config.js b/test/core-e2e-legacy/src/fixtures/esm-config/mocks.config.js new file mode 100644 index 000000000..b1cb00070 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm-config/mocks.config.js @@ -0,0 +1,15 @@ +const path = require("path"); + +module.exports = { + log: "silly", + mocks: { + selected: "user-real", + }, + files: { + babelRegister: { + enabled: true, + }, + path: path.resolve(__dirname, "..", "temp"), + watch: true, + }, +}; diff --git a/test/core-e2e-legacy/src/fixtures/esm-config/starter b/test/core-e2e-legacy/src/fixtures/esm-config/starter new file mode 100755 index 000000000..05af5e688 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm-config/starter @@ -0,0 +1,21 @@ +#!/usr/bin/env node +"use strict"; + +const Core = require("@mocks-server/core"); + +const handleError = (error) => { + console.error(`Error: ${error.message}`); + process.exitCode = 1; +}; + +const start = () => { + try { + const mocksServer = new Core(); + + return mocksServer.start().catch(handleError); + } catch (error) { + return handleError(error); + } +}; + +start(); diff --git a/test/core-e2e-legacy/src/fixtures/esm-modified/db/users.es b/test/core-e2e-legacy/src/fixtures/esm-modified/db/users.es new file mode 100644 index 000000000..832e17ba6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm-modified/db/users.es @@ -0,0 +1,12 @@ +const USERS = [ + { + id: 1, + name: "John Doe modified", + }, + { + id: 2, + name: "Jane Doe modified", + }, +]; + +export default USERS; diff --git a/test/core-e2e-legacy/src/fixtures/esm-modified/mocks.js b/test/core-e2e-legacy/src/fixtures/esm-modified/mocks.js new file mode 100644 index 000000000..9c876bdbf --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm-modified/mocks.js @@ -0,0 +1,16 @@ +export default [ + { + "id": "base", + "routesVariants": ["add-headers:enabled", "get-users:success", "get-user:success", "get-users-new:success"] + }, + { + "id": "no-headers", + "from": "base", + "routesVariants": ["add-headers:disabled"] + }, + { + "id": "user-real", + "from": "no-headers", + "routesVariants": ["get-user:real"] + }, +] diff --git a/test/core-e2e-legacy/src/fixtures/esm-modified/routes/middlewares.es b/test/core-e2e-legacy/src/fixtures/esm-modified/routes/middlewares.es new file mode 100644 index 000000000..85c4dae1b --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm-modified/routes/middlewares.es @@ -0,0 +1,29 @@ +// Use this file only as a guide for first steps using middlewares. Delete it when no more needed. +// For a detailed explanation about using middlewares, visit: +// https://mocks-server.org/docs/guides-using-middlewares + +const middlewares = [ + { + id: "add-headers", + url: "*", + method: "GET", + variants: [ + { + id: "enabled", + response: (req, res, next, mocksServer) => { + res.set("x-mocks-server-example", "custom-header"); + mocksServer.tracer.info( + "Custom header added by add-headers:enabled route variant middleware" + ); + next(); + }, + }, + { + id: "disabled", + response: (req, res, next) => next(), + }, + ], + }, +]; + +export default middlewares; diff --git a/test/core-e2e-legacy/src/fixtures/esm-modified/routes/users.es b/test/core-e2e-legacy/src/fixtures/esm-modified/routes/users.es new file mode 100644 index 000000000..09a09a886 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm-modified/routes/users.es @@ -0,0 +1,85 @@ +// Use this file only as a guide for first steps. Delete it when you have added your own routes files. +// For a detailed explanation regarding each routes property, visit: +// https://mocks-server.org/docs/get-started-routes + +// users data +import USERS from "../db/users"; + +const routes = [ + { + id: "get-users", // id of the route + url: "/api/users", // url in express format + method: "GET", // HTTP method + variants: [ + { + id: "success", // id of the variant + response: { + status: 200, // status to send + body: USERS, // body to send + }, + }, + { + id: "error", // id of the variant + response: { + status: 400, // status to send + body: { + // body to send + message: "Error", + }, + }, + }, + ], + }, + { + id: "get-user", // id of the route + url: "/api/users/:id", // url in express format + method: "GET", // HTTP method + variants: [ + { + id: "success", // id of the variant + response: { + status: 200, // status to send + body: USERS[0], // body to send + }, + }, + { + id: "real", // id of the variant + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, + { + id: "get-users-new", + url: "/api/new-users", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: [ + ...USERS, + { + id: 3, + name: "Brand new user", + }, + ], + }, + }, + ], + }, +]; + +export default routes; diff --git a/test/core-e2e-legacy/src/fixtures/esm/db/users.es b/test/core-e2e-legacy/src/fixtures/esm/db/users.es new file mode 100644 index 000000000..a1de66fdf --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm/db/users.es @@ -0,0 +1,12 @@ +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +export default USERS; diff --git a/test/core-e2e-legacy/src/fixtures/esm/mocks.es b/test/core-e2e-legacy/src/fixtures/esm/mocks.es new file mode 100644 index 000000000..d2fc2b072 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm/mocks.es @@ -0,0 +1,16 @@ +export default [ + { + "id": "base", + "routesVariants": ["add-headers:enabled", "get-users:success", "get-user:success"] + }, + { + "id": "no-headers", + "from": "base", + "routesVariants": ["add-headers:disabled"] + }, + { + "id": "user-real", + "from": "base", + "routesVariants": ["get-user:real"] + } +] diff --git a/test/core-e2e-legacy/src/fixtures/esm/routes/middlewares.es b/test/core-e2e-legacy/src/fixtures/esm/routes/middlewares.es new file mode 100644 index 000000000..85c4dae1b --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm/routes/middlewares.es @@ -0,0 +1,29 @@ +// Use this file only as a guide for first steps using middlewares. Delete it when no more needed. +// For a detailed explanation about using middlewares, visit: +// https://mocks-server.org/docs/guides-using-middlewares + +const middlewares = [ + { + id: "add-headers", + url: "*", + method: "GET", + variants: [ + { + id: "enabled", + response: (req, res, next, mocksServer) => { + res.set("x-mocks-server-example", "custom-header"); + mocksServer.tracer.info( + "Custom header added by add-headers:enabled route variant middleware" + ); + next(); + }, + }, + { + id: "disabled", + response: (req, res, next) => next(), + }, + ], + }, +]; + +export default middlewares; diff --git a/test/core-e2e-legacy/src/fixtures/esm/routes/users.es b/test/core-e2e-legacy/src/fixtures/esm/routes/users.es new file mode 100644 index 000000000..8957a9edd --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/esm/routes/users.es @@ -0,0 +1,65 @@ +// Use this file only as a guide for first steps. Delete it when you have added your own routes files. +// For a detailed explanation regarding each routes property, visit: +// https://mocks-server.org/docs/get-started-routes + +// users data +import USERS from "../db/users"; + +const routes = [ + { + id: "get-users", // id of the route + url: "/api/users", // url in express format + method: "GET", // HTTP method + variants: [ + { + id: "success", // id of the variant + response: { + status: 200, // status to send + body: USERS, // body to send + }, + }, + { + id: "error", // id of the variant + response: { + status: 400, // status to send + body: { + // body to send + message: "Error", + }, + }, + }, + ], + }, + { + id: "get-user", // id of the route + url: "/api/users/:id", // url in express format + method: "GET", // HTTP method + variants: [ + { + id: "success", // id of the variant + response: { + status: 200, // status to send + body: USERS[0], // body to send + }, + }, + { + id: "real", // id of the variant + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, +]; + +export default routes; diff --git a/test/core-e2e-legacy/src/fixtures/files-error-mock/db/users.js b/test/core-e2e-legacy/src/fixtures/files-error-mock/db/users.js new file mode 100644 index 000000000..f05a501f6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/files-error-mock/db/users.js @@ -0,0 +1,24 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +module.exports = { + USERS, +}; diff --git a/test/core-e2e-legacy/src/fixtures/files-error-mock/mocks.js b/test/core-e2e-legacy/src/fixtures/files-error-mock/mocks.js new file mode 100644 index 000000000..cedaaba04 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/files-error-mock/mocks.js @@ -0,0 +1,11 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +module.exports = foo; diff --git a/test/core-e2e-legacy/src/fixtures/files-error-mock/routes/user.js b/test/core-e2e-legacy/src/fixtures/files-error-mock/routes/user.js new file mode 100644 index 000000000..ac18756e6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/files-error-mock/routes/user.js @@ -0,0 +1,51 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-user", + url: "/api/users/:id", + method: "GET", + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + response: { + status: 200, + body: USERS[1], + }, + }, + { + id: "real", + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/files-error-mock/routes/users.js b/test/core-e2e-legacy/src/fixtures/files-error-mock/routes/users.js new file mode 100644 index 000000000..6c1d2aecd --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/files-error-mock/routes/users.js @@ -0,0 +1,37 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-users", + url: "/api/users", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: USERS, + }, + }, + { + id: "error", + response: { + status: 403, + body: { + message: "Bad data", + }, + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/files-error-routes/mocks.js b/test/core-e2e-legacy/src/fixtures/files-error-routes/mocks.js new file mode 100644 index 000000000..82511e694 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/files-error-routes/mocks.js @@ -0,0 +1,26 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +module.exports = [ + { + id: "base", + routesVariants: ["get-users:success", "get-user:1"], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + }, + { + id: "user-real", + from: "base", + routesVariants: ["get-user:real"], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/files-error-routes/routes/user.js b/test/core-e2e-legacy/src/fixtures/files-error-routes/routes/user.js new file mode 100644 index 000000000..ac18756e6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/files-error-routes/routes/user.js @@ -0,0 +1,51 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-user", + url: "/api/users/:id", + method: "GET", + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + response: { + status: 200, + body: USERS[1], + }, + }, + { + id: "real", + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/files-error-routes/routes/users.js b/test/core-e2e-legacy/src/fixtures/files-error-routes/routes/users.js new file mode 100644 index 000000000..5cbbe07b5 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/files-error-routes/routes/users.js @@ -0,0 +1,13 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.foo; diff --git a/test/core-e2e-legacy/src/fixtures/json-files/mocks.json b/test/core-e2e-legacy/src/fixtures/json-files/mocks.json new file mode 100644 index 000000000..3e5b89d37 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/json-files/mocks.json @@ -0,0 +1,11 @@ +[ + { + "id": "base", + "routesVariants": ["get-users:success", "get-user:1"] + }, + { + "id": "user-2", + "from": "base", + "routesVariants": ["get-user:2"] + } +] diff --git a/test/core-e2e-legacy/src/fixtures/json-files/routes/users.json b/test/core-e2e-legacy/src/fixtures/json-files/routes/users.json new file mode 100644 index 000000000..c43baf0b1 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/json-files/routes/users.json @@ -0,0 +1,61 @@ +[ + { + "id": "get-users", + "url": "/api/users", + "method": "GET", + "variants": [ + { + "id": "success", + "response": { + "status": 200, + "body": [ + { + "id": 1, + "name": "John Doe" + }, + { + "id": 2, + "name": "Jane Doe" + } + ] + } + }, + { + "id": "error", + "response": { + "status": 403, + "body": { + "message": "Bad data" + } + } + } + ] + }, + { + "id": "get-user", + "url": "/api/users/:id", + "method": "GET", + "variants": [ + { + "id": "1", + "response": { + "status": 200, + "body": { + "id": 1, + "name": "John Doe" + } + } + }, + { + "id": "2", + "response": { + "status": 200, + "body": { + "id": 2, + "name": "Jane Doe" + } + } + } + ] + } +] diff --git a/test/core-e2e-legacy/src/fixtures/middleware-route/db/users.js b/test/core-e2e-legacy/src/fixtures/middleware-route/db/users.js new file mode 100644 index 000000000..f05a501f6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/middleware-route/db/users.js @@ -0,0 +1,24 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +module.exports = { + USERS, +}; diff --git a/test/core-e2e-legacy/src/fixtures/middleware-route/mocks.js b/test/core-e2e-legacy/src/fixtures/middleware-route/mocks.js new file mode 100644 index 000000000..86860d90d --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/middleware-route/mocks.js @@ -0,0 +1,26 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +module.exports = [ + { + id: "base", + routesVariants: ["tracer:enabled", "get-users:success", "get-user:1"], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + }, + { + id: "user-real", + from: "base", + routesVariants: ["get-user:real"], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/middleware-route/routes/tracer.js b/test/core-e2e-legacy/src/fixtures/middleware-route/routes/tracer.js new file mode 100644 index 000000000..0aef6f4f8 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/middleware-route/routes/tracer.js @@ -0,0 +1,36 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "tracer", + url: "/api/**", + method: "GET", + variants: [ + { + id: "enabled", + response: (req, res, next, core) => { + core.tracer.info( + `Middleware in request ${req.query.req} => ${req.method} => ${req.url}` + ); + next(); + }, + }, + { + id: "disabled", + response: (req, res, next, core) => { + next(); + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/middleware-route/routes/user.js b/test/core-e2e-legacy/src/fixtures/middleware-route/routes/user.js new file mode 100644 index 000000000..ac18756e6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/middleware-route/routes/user.js @@ -0,0 +1,51 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-user", + url: "/api/users/:id", + method: "GET", + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + response: { + status: 200, + body: USERS[1], + }, + }, + { + id: "real", + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/middleware-route/routes/users.js b/test/core-e2e-legacy/src/fixtures/middleware-route/routes/users.js new file mode 100644 index 000000000..6c1d2aecd --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/middleware-route/routes/users.js @@ -0,0 +1,37 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-users", + url: "/api/users", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: USERS, + }, + }, + { + id: "error", + response: { + status: 403, + body: { + message: "Bad data", + }, + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/multi-methods/db/users.js b/test/core-e2e-legacy/src/fixtures/multi-methods/db/users.js new file mode 100644 index 000000000..f05a501f6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/multi-methods/db/users.js @@ -0,0 +1,24 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +module.exports = { + USERS, +}; diff --git a/test/core-e2e-legacy/src/fixtures/multi-methods/mocks.js b/test/core-e2e-legacy/src/fixtures/multi-methods/mocks.js new file mode 100644 index 000000000..e12b01264 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/multi-methods/mocks.js @@ -0,0 +1,21 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +module.exports = [ + { + id: "base", + routesVariants: ["get-users:success", "get-user:1"], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/multi-methods/routes/user.js b/test/core-e2e-legacy/src/fixtures/multi-methods/routes/user.js new file mode 100644 index 000000000..5000a41fc --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/multi-methods/routes/user.js @@ -0,0 +1,35 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-user", + url: "/api/users/:id", + method: ["PUT", "GET", "PATCH", "POST", "DELETE", "OPTIONS", "HEAD", "TRACE"], + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + response: { + status: 200, + body: USERS[1], + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/multi-methods/routes/users.js b/test/core-e2e-legacy/src/fixtures/multi-methods/routes/users.js new file mode 100644 index 000000000..6c1d2aecd --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/multi-methods/routes/users.js @@ -0,0 +1,37 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-users", + url: "/api/users", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: USERS, + }, + }, + { + id: "error", + response: { + status: 403, + body: { + message: "Bad data", + }, + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/starter b/test/core-e2e-legacy/src/fixtures/starter new file mode 100755 index 000000000..05af5e688 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/starter @@ -0,0 +1,21 @@ +#!/usr/bin/env node +"use strict"; + +const Core = require("@mocks-server/core"); + +const handleError = (error) => { + console.error(`Error: ${error.message}`); + process.exitCode = 1; +}; + +const start = () => { + try { + const mocksServer = new Core(); + + return mocksServer.start().catch(handleError); + } catch (error) { + return handleError(error); + } +}; + +start(); diff --git a/test/core-e2e-legacy/src/fixtures/typescript-config/babel.config.js b/test/core-e2e-legacy/src/fixtures/typescript-config/babel.config.js new file mode 100644 index 000000000..1108df09e --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-config/babel.config.js @@ -0,0 +1,3 @@ +module.exports = { + presets: ["@babel/env", "@babel/typescript"], +}; diff --git a/test/core-e2e-legacy/src/fixtures/typescript-config/mocks.config.js b/test/core-e2e-legacy/src/fixtures/typescript-config/mocks.config.js new file mode 100644 index 000000000..2976a73f8 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-config/mocks.config.js @@ -0,0 +1,14 @@ +const path = require("path"); + +module.exports = { + // mock to use on start + mocks: { selected: "user-real" }, + log: "silly", + files: { + path: path.resolve(__dirname, "..", "typescript"), + watch: true, + babelRegister: { + enabled: true, + }, + }, +}; diff --git a/test/core-e2e-legacy/src/fixtures/typescript-config/starter b/test/core-e2e-legacy/src/fixtures/typescript-config/starter new file mode 100755 index 000000000..05af5e688 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-config/starter @@ -0,0 +1,21 @@ +#!/usr/bin/env node +"use strict"; + +const Core = require("@mocks-server/core"); + +const handleError = (error) => { + console.error(`Error: ${error.message}`); + process.exitCode = 1; +}; + +const start = () => { + try { + const mocksServer = new Core(); + + return mocksServer.start().catch(handleError); + } catch (error) { + return handleError(error); + } +}; + +start(); diff --git a/test/core-e2e-legacy/src/fixtures/typescript-custom-config/babel.config.js b/test/core-e2e-legacy/src/fixtures/typescript-custom-config/babel.config.js new file mode 100644 index 000000000..1108df09e --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-custom-config/babel.config.js @@ -0,0 +1,3 @@ +module.exports = { + presets: ["@babel/env", "@babel/typescript"], +}; diff --git a/test/core-e2e-legacy/src/fixtures/typescript-custom-config/mocks.config.js b/test/core-e2e-legacy/src/fixtures/typescript-custom-config/mocks.config.js new file mode 100644 index 000000000..19ac52227 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-custom-config/mocks.config.js @@ -0,0 +1,26 @@ +const path = require("path"); + +module.exports = { + log: "silly", + mocks: { + selected: "user-real", + }, + files: { + path: path.resolve(__dirname, "..", "typescript-imports"), + watch: true, + babelRegister: { + enabled: true, + options: { + only: [ + (filePath) => { + return ( + filePath.includes(`fixtures${path.sep}typescript${path.sep}`) || + filePath.includes(`fixtures${path.sep}typescript-imports${path.sep}`) + ); + }, + ], + extensions: [".ts", ".js"], + } + }, + }, +}; diff --git a/test/core-e2e-legacy/src/fixtures/typescript-custom-config/starter b/test/core-e2e-legacy/src/fixtures/typescript-custom-config/starter new file mode 100755 index 000000000..05af5e688 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-custom-config/starter @@ -0,0 +1,21 @@ +#!/usr/bin/env node +"use strict"; + +const Core = require("@mocks-server/core"); + +const handleError = (error) => { + console.error(`Error: ${error.message}`); + process.exitCode = 1; +}; + +const start = () => { + try { + const mocksServer = new Core(); + + return mocksServer.start().catch(handleError); + } catch (error) { + return handleError(error); + } +}; + +start(); diff --git a/test/core-e2e-legacy/src/fixtures/typescript-imports/mocks.json b/test/core-e2e-legacy/src/fixtures/typescript-imports/mocks.json new file mode 100644 index 000000000..29a8711da --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-imports/mocks.json @@ -0,0 +1,16 @@ +[ + { + "id": "base", + "routesVariants": ["add-headers:enabled", "get-users:success", "get-user:success", "get-users-new:success", "get-books:success"] + }, + { + "id": "no-headers", + "from": "base", + "routesVariants": ["add-headers:disabled"] + }, + { + "id": "user-real", + "from": "base", + "routesVariants": ["get-user:real"] + } +] diff --git a/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/books.ts b/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/books.ts new file mode 100644 index 000000000..c4b4ca303 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/books.ts @@ -0,0 +1,3 @@ +import books from "../../typescript/routes/books.json"; + +export default books; diff --git a/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/middlewares.ts b/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/middlewares.ts new file mode 100644 index 000000000..f735bc535 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/middlewares.ts @@ -0,0 +1,3 @@ +import middlewares from "../../typescript/routes/middlewares"; + +export default middlewares; diff --git a/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/users.ts b/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/users.ts new file mode 100644 index 000000000..d1c5a1526 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript-imports/routes/users.ts @@ -0,0 +1,3 @@ +import routes from "../../typescript/routes/users"; + +export default routes; diff --git a/test/core-e2e-legacy/src/fixtures/typescript/db/users.ts b/test/core-e2e-legacy/src/fixtures/typescript/db/users.ts new file mode 100644 index 000000000..a1de66fdf --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript/db/users.ts @@ -0,0 +1,12 @@ +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +export default USERS; diff --git a/test/core-e2e-legacy/src/fixtures/typescript/mocks.ts b/test/core-e2e-legacy/src/fixtures/typescript/mocks.ts new file mode 100644 index 000000000..44549919e --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript/mocks.ts @@ -0,0 +1,16 @@ +export default [ + { + "id": "base", + "routesVariants": ["add-headers:enabled", "get-users:success", "get-user:success", "get-users-new:success", "get-books:success"] + }, + { + "id": "no-headers", + "from": "base", + "routesVariants": ["add-headers:disabled"] + }, + { + "id": "user-real", + "from": "base", + "routesVariants": ["get-user:real"] + }, +] diff --git a/test/core-e2e-legacy/src/fixtures/typescript/routes/books.json b/test/core-e2e-legacy/src/fixtures/typescript/routes/books.json new file mode 100644 index 000000000..b83fa8431 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript/routes/books.json @@ -0,0 +1,25 @@ +[ + { + "id": "get-books", + "url": "/api/books", + "method": "GET", + "variants": [ + { + "id": "success", + "response": { + "status": 200, + "body": [ + { + "id": 1, + "title": "1984" + }, + { + "id": 2, + "title": "Brave New World" + } + ] + } + } + ] + } +] diff --git a/test/core-e2e-legacy/src/fixtures/typescript/routes/middlewares.ts b/test/core-e2e-legacy/src/fixtures/typescript/routes/middlewares.ts new file mode 100644 index 000000000..f3adec168 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript/routes/middlewares.ts @@ -0,0 +1,31 @@ +// Use this file only as a guide for first steps using middlewares. Delete it when no more needed. +// For a detailed explanation about using middlewares, visit: +// https://mocks-server.org/docs/guides-using-middlewares + +function middleware(req: Object, res: Object, next: () => void, mocksServer: Object) : void { + res.set("x-mocks-server-example", "custom-header-typescript"); + mocksServer.tracer.info( + "Custom header added by add-headers:enabled route variant middleware using TypeScript" + ); + next(); +} + +const middlewares = [ + { + id: "add-headers", + url: "*", + method: "GET", + variants: [ + { + id: "enabled", + response: middleware, + }, + { + id: "disabled", + response: (req, res, next) => next(), + }, + ], + }, +]; + +export default middlewares; diff --git a/test/core-e2e-legacy/src/fixtures/typescript/routes/users.ts b/test/core-e2e-legacy/src/fixtures/typescript/routes/users.ts new file mode 100644 index 000000000..8957a9edd --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/typescript/routes/users.ts @@ -0,0 +1,65 @@ +// Use this file only as a guide for first steps. Delete it when you have added your own routes files. +// For a detailed explanation regarding each routes property, visit: +// https://mocks-server.org/docs/get-started-routes + +// users data +import USERS from "../db/users"; + +const routes = [ + { + id: "get-users", // id of the route + url: "/api/users", // url in express format + method: "GET", // HTTP method + variants: [ + { + id: "success", // id of the variant + response: { + status: 200, // status to send + body: USERS, // body to send + }, + }, + { + id: "error", // id of the variant + response: { + status: 400, // status to send + body: { + // body to send + message: "Error", + }, + }, + }, + ], + }, + { + id: "get-user", // id of the route + url: "/api/users/:id", // url in express format + method: "GET", // HTTP method + variants: [ + { + id: "success", // id of the variant + response: { + status: 200, // status to send + body: USERS[0], // body to send + }, + }, + { + id: "real", // id of the variant + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, +]; + +export default routes; diff --git a/test/core-e2e-legacy/src/fixtures/validation-not-array/mocks.js b/test/core-e2e-legacy/src/fixtures/validation-not-array/mocks.js new file mode 100644 index 000000000..f053ebf79 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/validation-not-array/mocks.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/core-e2e-legacy/src/fixtures/validation-not-array/routes/routes-not-valid.js b/test/core-e2e-legacy/src/fixtures/validation-not-array/routes/routes-not-valid.js new file mode 100644 index 000000000..f053ebf79 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/validation-not-array/routes/routes-not-valid.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/core-e2e-legacy/src/fixtures/validation-not-array/routes/routes-valid.js b/test/core-e2e-legacy/src/fixtures/validation-not-array/routes/routes-valid.js new file mode 100644 index 000000000..54377a26b --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/validation-not-array/routes/routes-valid.js @@ -0,0 +1,16 @@ +module.exports = [ + { + id: "foo", + method: "GET", + url: "/api/foo", + variants: [ + { + id: "success", + response: { + status: 200, + body: {}, + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/validations/mocks.js b/test/core-e2e-legacy/src/fixtures/validations/mocks.js new file mode 100644 index 000000000..aa4d19788 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/validations/mocks.js @@ -0,0 +1,28 @@ +module.exports = [ + { + id: "base", + routesVariants: ["get-users-invalid:success", "get-user-variant-invalid:1", "get-user:2"], + }, + { + id: "base", + routesVariants: [], + }, + { + id: "invalid-variant", + from: "base", + routesVariants: ["get-user-variant-invalid:2"], + }, + { + id: "invalid-mock", + }, + { + id: "invalid-from", + from: "foo", + routesVariants: ["get-user:2"], + }, + { + id: "duplicated-route", + from: "base", + routesVariants: ["get-user:2", "get-user:1"], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/validations/routes/users.js b/test/core-e2e-legacy/src/fixtures/validations/routes/users.js new file mode 100644 index 000000000..0d11de601 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/validations/routes/users.js @@ -0,0 +1,93 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +module.exports = [ + { + id: "get-user", + url: "/api/users/:id", + method: "GET", + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + response: { + status: 200, + body: USERS[1], + }, + }, + ], + }, + { + id: "get-user-variant-invalid", + url: "/api/invalid-users/:id", + method: "GET", + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + response: null, + }, + ], + }, + { + id: "get-user-variant-invalid", + url: "/api/invalid-users/:id", + method: "GET", + variants: [ + { + id: "2", + response: { + status: 200, + body: USERS[1], + }, + }, + ], + }, + { + id: "get-users-invalid", + url: "/api/invalid-users", + method: "foo", + variants: [ + { + id: "success", + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/db/users.js b/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/db/users.js new file mode 100644 index 000000000..627c6a783 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/db/users.js @@ -0,0 +1,24 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const USERS = [ + { + id: 1, + name: "John Doe modified", + }, + { + id: 2, + name: "Jane Doe modified", + }, +]; + +module.exports = { + USERS, +}; diff --git a/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/mocks.js b/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/mocks.js new file mode 100644 index 000000000..0141b77f9 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/mocks.js @@ -0,0 +1,31 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +module.exports = [ + { + id: "base", + routesVariants: ["get-users:success", "get-user:1", "get-users-new:success"], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + }, + { + id: "user-real", + from: "base", + routesVariants: ["get-user:real"], + }, + { + id: "users-error", + from: "base", + routesVariants: ["get-users:error"], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/routes/user.js b/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/routes/user.js new file mode 100644 index 000000000..ac18756e6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/routes/user.js @@ -0,0 +1,51 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-user", + url: "/api/users/:id", + method: "GET", + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + response: { + status: 200, + body: USERS[1], + }, + }, + { + id: "real", + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/routes/users.js b/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/routes/users.js new file mode 100644 index 000000000..db3f6a9b5 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/web-tutorial-modified/routes/users.js @@ -0,0 +1,61 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-users", + url: "/api/users", + method: "get", + variants: [ + { + id: "success", + response: { + headers: { + "x-custom-header": "foo-header", + "x-another-header": "another-header", + }, + status: 200, + body: USERS, + }, + }, + { + id: "error", + response: { + status: 403, + body: { + message: "Bad data", + }, + }, + }, + ], + }, + { + id: "get-users-new", + url: "/api/new-users", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: [ + ...USERS, + { + id: 3, + name: "Brand new user", + }, + ], + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/web-tutorial/db/users.js b/test/core-e2e-legacy/src/fixtures/web-tutorial/db/users.js new file mode 100644 index 000000000..f05a501f6 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/web-tutorial/db/users.js @@ -0,0 +1,24 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const USERS = [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, +]; + +module.exports = { + USERS, +}; diff --git a/test/core-e2e-legacy/src/fixtures/web-tutorial/mocks.js b/test/core-e2e-legacy/src/fixtures/web-tutorial/mocks.js new file mode 100644 index 000000000..82511e694 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/web-tutorial/mocks.js @@ -0,0 +1,26 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +module.exports = [ + { + id: "base", + routesVariants: ["get-users:success", "get-user:1"], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + }, + { + id: "user-real", + from: "base", + routesVariants: ["get-user:real"], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/web-tutorial/routes/user.js b/test/core-e2e-legacy/src/fixtures/web-tutorial/routes/user.js new file mode 100644 index 000000000..b2f97d438 --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/web-tutorial/routes/user.js @@ -0,0 +1,51 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-user", + url: "/api/users/:id", + method: "get", + variants: [ + { + id: "1", + response: { + status: 200, + body: USERS[0], + }, + }, + { + id: "2", + response: { + status: 200, + body: USERS[1], + }, + }, + { + id: "real", + response: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/fixtures/web-tutorial/routes/users.js b/test/core-e2e-legacy/src/fixtures/web-tutorial/routes/users.js new file mode 100644 index 000000000..6c1d2aecd --- /dev/null +++ b/test/core-e2e-legacy/src/fixtures/web-tutorial/routes/users.js @@ -0,0 +1,37 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { USERS } = require("../db/users"); + +module.exports = [ + { + id: "get-users", + url: "/api/users", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: USERS, + }, + }, + { + id: "error", + response: { + status: 403, + body: { + message: "Bad data", + }, + }, + }, + ], + }, +]; diff --git a/test/core-e2e-legacy/src/json-files.spec.js b/test/core-e2e-legacy/src/json-files.spec.js new file mode 100644 index 000000000..267cdee84 --- /dev/null +++ b/test/core-e2e-legacy/src/json-files.spec.js @@ -0,0 +1,128 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + startCore, + fetch, + waitForServer, + findAlert, + removeConfigFile, +} = require("./support/helpers"); + +describe("json files", () => { + let core, changeMock; + + beforeAll(async () => { + core = await startCore("json-files"); + await waitForServer(); + changeMock = (name) => { + core.config.namespace("mocks").option("selected").value = name; + }; + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("mock by default", () => { + it("should have added an alert about mock was not defined", () => { + expect(findAlert("mocks:settings", core.alerts).message).toEqual( + expect.stringContaining("Option 'mock' was not defined") + ); + }); + + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe('when changing mock to "user-2"', () => { + beforeAll(() => { + changeMock("user-2"); + }); + + it("should have removed alert", () => { + expect(findAlert("mocks:settings", core.alerts)).toEqual(undefined); + }); + + it("should serve users collection mock under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 2 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); + + describe('when changing mock to "foo"', () => { + beforeAll(() => { + changeMock("foo"); + }); + + it("should have added an alert", () => { + expect(findAlert("mocks:settings", core.alerts).message).toEqual( + expect.stringContaining("Mock 'foo' was not found") + ); + }); + + // if mock not exists, it uses the first one found + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/load-mocks-and-routes.spec.js b/test/core-e2e-legacy/src/load-mocks-and-routes.spec.js new file mode 100644 index 000000000..98292dd30 --- /dev/null +++ b/test/core-e2e-legacy/src/load-mocks-and-routes.spec.js @@ -0,0 +1,157 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + startCore, + fetch, + waitForServer, + waitForServerUrl, + removeConfigFile, +} = require("./support/helpers"); + +describe("loadMocks and loadRoutes methods", () => { + let core, changeMockAndWait; + + beforeAll(async () => { + core = await startCore(); + changeMockAndWait = async (mockName) => { + core.config.namespace("mocks").option("selected").value = mockName; + await new Promise((resolve) => { + const interval = setInterval(() => { + if (core.mocks.current === mockName) { + clearInterval(interval); + resolve(); + } + }, 200); + }); + }; + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("When started", () => { + it("should return users", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); + + describe("When routes and mocks are loaded using core methods", () => { + it("should have loaded new mocks and routes", async () => { + core.loadRoutes([ + { + id: "get-books", + url: "/api/books", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: [{ id: 1, title: "1984" }], + }, + }, + { + id: "error", + response: { + status: 403, + body: { + message: "Bad data", + }, + }, + }, + ], + }, + { + id: "get-authors", + url: "/api/authors", + method: "GET", + variants: [ + { + id: "success", + response: { + status: 200, + body: [{ id: 1, name: "George Orwell" }], + }, + }, + { + id: "error", + response: { + status: 403, + body: { + message: "Bad data", + }, + }, + }, + ], + }, + ]); + core.loadMocks([ + { + id: "users-and-library", + from: "base", + routesVariants: ["get-books:success", "get-authors:success"], + }, + { + id: "authors-error", + from: "users-and-library", + routesVariants: ["get-authors:error"], + }, + ]); + core.config.namespace("mocks").option("selected").value = "users-and-library"; + await waitForServerUrl("/api/books"); + + const books = await fetch("/api/books"); + expect(books.body).toEqual([{ id: 1, title: "1984" }]); + + const authors = await fetch("/api/authors"); + expect(authors.body).toEqual([{ id: 1, name: "George Orwell" }]); + + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should be able to change to a new mock", async () => { + await changeMockAndWait("authors-error"); + const books = await fetch("/api/books"); + expect(books.body).toEqual([{ id: 1, title: "1984" }]); + + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + + const authors = await fetch("/api/authors"); + expect(authors.status).toEqual(403); + }); + + it("should keep mocks loaded from files", async () => { + await changeMockAndWait("base"); + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + const authors = await fetch("/api/authors"); + expect(authors.status).toEqual(404); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/mock-argument.spec.js b/test/core-e2e-legacy/src/mock-argument.spec.js new file mode 100644 index 000000000..537beb2ad --- /dev/null +++ b/test/core-e2e-legacy/src/mock-argument.spec.js @@ -0,0 +1,54 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); + +describe("mock argument", () => { + const PATH_OPTION = "--files.path=web-tutorial"; + let mocks; + + afterEach(async () => { + removeConfigFile(); + await mocks.kill(); + }); + + describe("when not provided", () => { + it("should set as current mock the first one found", async () => { + mocks = mocksRunner([PATH_OPTION]); + await waitForServer(); + const users = await fetch("/api/users/2"); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe("when provided and exists", () => { + it("should set current behavior", async () => { + mocks = mocksRunner([PATH_OPTION, "--mocks.selected=user-real"]); + await waitForServer(); + const users = await fetch("/api/users/2"); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); + + describe("when provided and does not exist", () => { + it("should print a warning", async () => { + mocks = mocksRunner([PATH_OPTION, "--mocks.selected=foo"]); + await waitForServer(); + expect(mocks.logs.current).toEqual(expect.stringContaining("Mock 'foo' was not found")); + }); + + it("should set as current mock the first one found", async () => { + mocks = mocksRunner([PATH_OPTION, "--mocks.selected=foo"]); + await waitForServer(); + const users = await fetch("/api/users/2"); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/mock-setting.spec.js b/test/core-e2e-legacy/src/mock-setting.spec.js new file mode 100644 index 000000000..3c41ee9bf --- /dev/null +++ b/test/core-e2e-legacy/src/mock-setting.spec.js @@ -0,0 +1,170 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + startCore, + fetch, + waitForServer, + findAlert, + removeConfigFile, +} = require("./support/helpers"); + +describe("mock setting", () => { + let core, changeMock; + + beforeAll(async () => { + core = await startCore("web-tutorial"); + await waitForServer(); + changeMock = (name) => { + core.config.namespace("mocks").option("selected").value = name; + }; + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("mock by default", () => { + it("should have added an alert about mock was not defined", () => { + expect(findAlert("mocks:settings", core.alerts).message).toEqual( + expect.stringContaining("Option 'mock' was not defined") + ); + }); + + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe('when changing mock to "user-2"', () => { + beforeAll(() => { + changeMock("user-2"); + }); + + it("should have removed alert", () => { + expect(findAlert("mocks:settings", core.alerts)).toEqual(undefined); + }); + + it("should serve users collection mock under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 2 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); + + describe('when changing mock to "user-real"', () => { + beforeAll(() => { + changeMock("user-real"); + }); + + it("should serve users collection mock under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should return not found for /api/users/3 path", async () => { + const users = await fetch("/api/users/3"); + expect(users.status).toEqual(404); + }); + }); + + describe('when changing mock to "foo"', () => { + beforeAll(() => { + changeMock("foo"); + }); + + it("should have added an alert", () => { + expect(findAlert("mocks:settings", core.alerts).message).toEqual( + expect.stringContaining("Mock 'foo' was not found") + ); + }); + + // if mock not exists, it uses the first one found + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe('when changing mock again to "user-real"', () => { + beforeAll(() => { + changeMock("user-real"); + }); + + it("should have removed alert", () => { + expect(findAlert("mocks:settings", core.alerts)).toEqual(undefined); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/multi-methods.spec.js b/test/core-e2e-legacy/src/multi-methods.spec.js new file mode 100644 index 000000000..e16238501 --- /dev/null +++ b/test/core-e2e-legacy/src/multi-methods.spec.js @@ -0,0 +1,225 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { startCore, waitForServer, fetch, removeConfigFile } = require("./support/helpers"); + +describe("when method is defined as array", () => { + let requestNumber = 1; + let core; + + beforeAll(async () => { + // disable cors to test custom options method + core = await startCore("multi-methods", { + server: { cors: { options: { preflightContinue: true } } }, + log: "debug", + }); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("plain mocks and routes", () => { + describe("amounts", () => { + it("should have two mocks", async () => { + expect(core.mocks.plainMocks.length).toEqual(2); + }); + + it("should have two routes", async () => { + expect(core.mocks.plainRoutes.length).toEqual(2); + }); + + it("should have four route variants", async () => { + expect(core.mocks.plainRoutesVariants.length).toEqual(4); + }); + }); + + describe("plainMocks", () => { + it("should return plain mocks", async () => { + expect(core.mocks.plainMocks).toEqual([ + { + id: "base", + from: null, + routesVariants: ["get-users:success", "get-user:1"], + appliedRoutesVariants: ["get-users:success", "get-user:1"], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + appliedRoutesVariants: ["get-users:success", "get-user:2"], + }, + ]); + }); + + it("should return plain routes", async () => { + expect(core.mocks.plainRoutes).toEqual([ + { + id: "get-user", + url: "/api/users/:id", + method: ["PUT", "GET", "PATCH", "POST", "DELETE", "OPTIONS", "HEAD", "TRACE"], + delay: null, + variants: ["get-user:1", "get-user:2"], + }, + { + id: "get-users", + url: "/api/users", + method: "GET", + delay: null, + variants: ["get-users:success", "get-users:error"], + }, + ]); + }); + + it("should return plain routesVariants", async () => { + expect(core.mocks.plainRoutesVariants).toEqual([ + { + id: "get-user:1", + routeId: "get-user", + handler: "default", + response: { + body: { + id: 1, + name: "John Doe", + }, + status: 200, + }, + delay: null, + }, + { + id: "get-user:2", + routeId: "get-user", + handler: "default", + response: { + body: { + id: 2, + name: "Jane Doe", + }, + status: 200, + }, + delay: null, + }, + { + id: "get-users:success", + routeId: "get-users", + handler: "default", + response: { + body: [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, + ], + status: 200, + }, + delay: null, + }, + { + id: "get-users:error", + routeId: "get-users", + handler: "default", + response: { + body: { + message: "Bad data", + }, + status: 403, + }, + delay: null, + }, + ]); + }); + }); + }); + + describe("base mock", () => { + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users?req=1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + const testMethod = (method, id, expectBody = true) => { + it(`should serve user 1 under the /api/users/${id} path with ${method} method`, async () => { + const users = await fetch(`/api/users/${id}?req=${requestNumber}`, { method }); + requestNumber++; + expect(users.status).toEqual(200); + if (expectBody) { + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + } + }); + }; + + testMethod("GET", "1"); + testMethod("GET", "2"); + testMethod("PUT", "1"); + testMethod("PUT", "2"); + testMethod("POST", "1"); + testMethod("POST", "2"); + testMethod("PATCH", "1"); + testMethod("PATCH", "2"); + testMethod("DELETE", "1"); + testMethod("DELETE", "2"); + testMethod("OPTIONS", "1"); + testMethod("OPTIONS", "2"); + testMethod("HEAD", "1", false); + testMethod("HEAD", "2", false); + testMethod("TRACE", "1"); + testMethod("TRACE", "2"); + }); + + describe('when using route variant "get-user:2"', () => { + it("should serve users under the /api/users path", async () => { + core.mocks.useRouteVariant("get-user:2"); + const users = await fetch("/api/users?req=10"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + const testMethod = (method, id, expectBody = true) => { + it(`should serve user 2 under the /api/users/${id} path with ${method} method`, async () => { + const users = await fetch(`/api/users/${id}?req=${requestNumber}`, { method }); + requestNumber++; + expect(users.status).toEqual(200); + if (expectBody) { + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + } + }); + }; + + testMethod("GET", "1"); + testMethod("GET", "2"); + testMethod("PUT", "1"); + testMethod("PUT", "2"); + testMethod("POST", "1"); + testMethod("POST", "2"); + testMethod("PATCH", "1"); + testMethod("PATCH", "2"); + testMethod("DELETE", "1"); + testMethod("DELETE", "2"); + testMethod("OPTIONS", "1"); + testMethod("OPTIONS", "2"); + testMethod("HEAD", "1", false); + testMethod("HEAD", "2", false); + testMethod("TRACE", "1"); + testMethod("TRACE", "2"); + }); +}); diff --git a/test/core-e2e-legacy/src/path-argument.spec.js b/test/core-e2e-legacy/src/path-argument.spec.js new file mode 100644 index 000000000..9855805b9 --- /dev/null +++ b/test/core-e2e-legacy/src/path-argument.spec.js @@ -0,0 +1,71 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const path = require("path"); +const fsExtra = require("fs-extra"); +const { + mocksRunner, + fetch, + fixturesFolder, + waitForServer, + removeConfigFile, +} = require("./support/helpers"); + +describe("path argument", () => { + const FOLDER = "mocks"; + let mocks; + + describe("when no defined", () => { + beforeAll(async () => { + await fsExtra.remove(fixturesFolder(FOLDER)); + mocks = mocksRunner(); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await fsExtra.remove(fixturesFolder(FOLDER)); + await mocks.kill(); + }); + + it("should print a warning about creating folder", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("Mocks folder was not found")); + }); + + it("should have created a mocks folder", async () => { + expect(fsExtra.existsSync(fixturesFolder(FOLDER))).toEqual(true); + }); + + it("should have created scaffold folder", async () => { + expect(fsExtra.existsSync(path.resolve(fixturesFolder(FOLDER), "routes"))).toEqual(true); + expect(fsExtra.existsSync(path.resolve(fixturesFolder(FOLDER), "mocks.json"))).toEqual(true); + }); + }); + + describe("when defined", () => { + beforeAll(async () => { + mocks = mocksRunner(["--files.path=web-tutorial"]); + await waitForServer(); + }); + + afterAll(async () => { + await mocks.kill(); + }); + + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/path-not-exists.spec.js b/test/core-e2e-legacy/src/path-not-exists.spec.js new file mode 100644 index 000000000..c1061ec53 --- /dev/null +++ b/test/core-e2e-legacy/src/path-not-exists.spec.js @@ -0,0 +1,149 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const path = require("path"); +const fsExtra = require("fs-extra"); +const { + startCore, + fetch, + fixturesFolder, + waitForServer, + findAlert, + removeConfigFile, +} = require("./support/helpers"); + +describe("when path not exists", () => { + const FOLDER = "unexistant"; + let core, changeMock; + + beforeAll(async () => { + await fsExtra.remove(fixturesFolder(FOLDER)); + core = await startCore(FOLDER); + changeMock = (name) => { + core.config.namespace("mocks").option("selected").value = name; + }; + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await fsExtra.remove(fixturesFolder(FOLDER)); + await core.stop(); + }); + + describe("scaffold", () => { + it("should have created folder", async () => { + expect(fsExtra.existsSync(fixturesFolder(FOLDER))).toEqual(true); + }); + + it("should have created scaffold folder", async () => { + expect(fsExtra.existsSync(path.resolve(fixturesFolder(FOLDER), "routes"))).toEqual(true); + expect(fsExtra.existsSync(path.resolve(fixturesFolder(FOLDER), "mocks.json"))).toEqual(true); + }); + + it("should have added an alert about folder not found", async () => { + expect(findAlert("scaffold:mocks", core.alerts).message).toEqual( + expect.stringContaining("Mocks folder was not found") + ); + }); + + it("should have three mocks", async () => { + expect(core.mocks.plainMocks.length).toEqual(3); + }); + + it("should have three routes", async () => { + expect(core.mocks.plainRoutes.length).toEqual(3); + }); + + it("should have six routes", async () => { + expect(core.mocks.plainRoutesVariants.length).toEqual(6); + }); + }); + + describe("base mock", () => { + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe("no-headers mock", () => { + it("should not add headers to /api/users", async () => { + changeMock("no-headers"); + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.headers.get("x-mocks-server-example")).toEqual(null); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should not add headers to /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.headers.get("x-mocks-server-example")).toEqual(null); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should not add headers to /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.headers.get("x-mocks-server-example")).toEqual(null); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe("user-real mock", () => { + it("should serve users under the /api/users path", async () => { + changeMock("user-real"); + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.headers.get("x-mocks-server-example")).toEqual(null); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.headers.get("x-mocks-server-example")).toEqual(null); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.headers.get("x-mocks-server-example")).toEqual(null); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/path-setting.spec.js b/test/core-e2e-legacy/src/path-setting.spec.js new file mode 100644 index 000000000..b09ed9723 --- /dev/null +++ b/test/core-e2e-legacy/src/path-setting.spec.js @@ -0,0 +1,65 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + startCore, + fetch, + waitForServer, + waitForServerUrl, + fixturesFolder, + removeConfigFile, +} = require("./support/helpers"); + +describe("path setting", () => { + let core, changePath; + + beforeAll(async () => { + changePath = (name) => { + core.config.namespace("files").option("path").value = name; + }; + core = await startCore(); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("When started", () => { + it("should load web-tutorial mocks", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); + + describe("When path setting is changed", () => { + it("should have loaded new mocks", async () => { + changePath(fixturesFolder("web-tutorial-modified")); + await waitForServerUrl("/api/new-users"); + + const users = await fetch("/api/new-users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe modified" }, + { id: 2, name: "Jane Doe modified" }, + { id: 3, name: "Brand new user" }, + ]); + + const oldUsers = await fetch("/api/users"); + expect(oldUsers.body).toEqual([ + { id: 1, name: "John Doe modified" }, + { id: 2, name: "Jane Doe modified" }, + ]); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/plain-mocks-and-routes.spec.js b/test/core-e2e-legacy/src/plain-mocks-and-routes.spec.js new file mode 100644 index 000000000..ed4d2a518 --- /dev/null +++ b/test/core-e2e-legacy/src/plain-mocks-and-routes.spec.js @@ -0,0 +1,152 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { startCore, waitForServer, removeConfigFile } = require("./support/helpers"); + +describe("mocks and routes", () => { + let core; + + beforeAll(async () => { + core = await startCore("web-tutorial"); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("amounts", () => { + it("should have three mocks", async () => { + expect(core.mocks.plainMocks.length).toEqual(3); + }); + + it("should have two routes", async () => { + expect(core.mocks.plainRoutes.length).toEqual(2); + }); + + it("should have five route variants", async () => { + expect(core.mocks.plainRoutesVariants.length).toEqual(5); + }); + }); + + describe("plainMocks", () => { + it("should return plain mocks", async () => { + expect(core.mocks.plainMocks).toEqual([ + { + id: "base", + from: null, + routesVariants: ["get-users:success", "get-user:1"], + appliedRoutesVariants: ["get-users:success", "get-user:1"], + }, + { + id: "user-2", + from: "base", + routesVariants: ["get-user:2"], + appliedRoutesVariants: ["get-users:success", "get-user:2"], + }, + { + id: "user-real", + from: "base", + routesVariants: ["get-user:real"], + appliedRoutesVariants: ["get-users:success", "get-user:real"], + }, + ]); + }); + + it("should return plain routes", async () => { + expect(core.mocks.plainRoutes).toEqual([ + { + id: "get-user", + url: "/api/users/:id", + method: "get", + delay: null, + variants: ["get-user:1", "get-user:2", "get-user:real"], + }, + { + id: "get-users", + url: "/api/users", + method: "GET", + delay: null, + variants: ["get-users:success", "get-users:error"], + }, + ]); + }); + + it("should return plain routesVariants", async () => { + expect(core.mocks.plainRoutesVariants).toEqual([ + { + id: "get-user:1", + routeId: "get-user", + handler: "default", + response: { + body: { + id: 1, + name: "John Doe", + }, + status: 200, + }, + delay: null, + }, + { + id: "get-user:2", + routeId: "get-user", + handler: "default", + response: { + body: { + id: 2, + name: "Jane Doe", + }, + status: 200, + }, + delay: null, + }, + { + id: "get-user:real", + routeId: "get-user", + handler: "default", + response: null, + delay: null, + }, + { + id: "get-users:success", + routeId: "get-users", + handler: "default", + response: { + body: [ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, + ], + status: 200, + }, + delay: null, + }, + { + id: "get-users:error", + routeId: "get-users", + handler: "default", + response: { + body: { + message: "Bad data", + }, + status: 403, + }, + delay: null, + }, + ]); + }); + }); +}); diff --git a/test/core-e2e/src/plugins-legacy-alerts.spec.js b/test/core-e2e-legacy/src/plugins-legacy-alerts.spec.js similarity index 100% rename from test/core-e2e/src/plugins-legacy-alerts.spec.js rename to test/core-e2e-legacy/src/plugins-legacy-alerts.spec.js diff --git a/test/core-e2e/src/plugins-legacy-formats.spec.js b/test/core-e2e-legacy/src/plugins-legacy-formats.spec.js similarity index 100% rename from test/core-e2e/src/plugins-legacy-formats.spec.js rename to test/core-e2e-legacy/src/plugins-legacy-formats.spec.js diff --git a/test/core-e2e-legacy/src/plugins.spec.js b/test/core-e2e-legacy/src/plugins.spec.js new file mode 100644 index 000000000..b691fe344 --- /dev/null +++ b/test/core-e2e-legacy/src/plugins.spec.js @@ -0,0 +1,358 @@ +/* +Copyright 2019-2022 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const sinon = require("sinon"); +const express = require("express"); + +const filterPluginAlerts = (alerts) => + alerts.filter((alert) => alert.context.indexOf("plugins") === 0); + +const filterLogs = (logs, text) => logs.filter((log) => log.includes(text)); + +const { + startCore, + fetch, + fixturesFolder, + wait, + TimeCounter, + removeConfigFile, +} = require("./support/helpers"); + +describe("plugins", () => { + const FOO_CUSTOM_RESPONSE = { + foo: "foo", + }; + let core; + let customRouter; + let sandbox; + let changeAlertsSpy; + let mocksLoadedSpy; + let initSpy; + let registerSpy; + let configSpy; + let startSpy; + + beforeAll(async () => { + sandbox = sinon.createSandbox(); + mocksLoadedSpy = sandbox.spy(); + changeAlertsSpy = sandbox.spy(); + initSpy = sandbox.spy(); + registerSpy = sandbox.spy(); + configSpy = sandbox.spy(); + startSpy = sandbox.spy(); + customRouter = express.Router(); + customRouter.get("/", (_req, res) => { + res.status(200); + res.send(FOO_CUSTOM_RESPONSE); + }); + }); + + afterAll(() => { + removeConfigFile(); + sandbox.restore(); + }); + + const testPlugin = (description, pluginConstructor, options = {}) => { + describe(`${description}`, () => { + beforeAll(async () => { + core = await startCore("web-tutorial", { + plugins: { + register: [pluginConstructor], + }, + log: "info", + }); + }); + + afterAll(async () => { + await core.stop(); + sandbox.reset(); + }); + + describe("when started", () => { + it("should start server and send responses", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + if (!options.doNotTestConfig) { + it("should have received config", async () => { + expect(configSpy.getCall(0).args[0]).not.toBe(undefined); + }); + } + + it("should have executed register method", async () => { + expect(registerSpy.callCount).toEqual(1); + }); + + it("should have executed logger in register method", () => { + expect( + filterLogs(core.logs, "[plugins:test-plugin] Log from register method").length + ).toEqual(1); + }); + + it("should have executed init method", async () => { + expect(initSpy.callCount).toEqual(1); + }); + + it("should have executed logger in init method", () => { + expect( + filterLogs(core.logs, "[plugins:test-plugin] Log from init method").length + ).toEqual(1); + }); + + it("should have config available when init is called", async () => { + expect(initSpy.getCall(0).args[0]).toEqual(fixturesFolder("web-tutorial")); + expect(initSpy.getCall(0).args[1]).toEqual(3100); + expect(initSpy.getCall(0).args[2]).toEqual(0); + }); + + it("should have executed start method", async () => { + expect(startSpy.callCount).toEqual(1); + }); + + it("should have executed logger in start method", () => { + expect( + filterLogs(core.logs, "[plugins:test-plugin] Log from start method").length + ).toEqual(1); + }); + + it("should respond to custom routes", async () => { + const response = await fetch("/foo-path"); + expect(response.body).toEqual(FOO_CUSTOM_RESPONSE); + }); + + it("should have added two plugin alerts", async () => { + const alerts = filterPluginAlerts(core.alerts); + const registerAlert = alerts.find( + (alert) => alert.context === "plugins:test-plugin:test-register" + ); + const startAlert = alerts.find( + (alert) => alert.context === "plugins:test-plugin:test-start" + ); + expect(alerts.length).toEqual(2); + expect(registerAlert.message).toEqual("Warning registering plugin"); + expect(startAlert.message).toEqual("Warning starting plugin"); + }); + }); + + describe("when emit events", () => { + beforeAll(async () => { + core.config.namespace("files").option("path").value = + fixturesFolder("web-tutorial-modified"); + await wait(5000); + }); + + it("should inform plugin when mocks are loaded", async () => { + expect(mocksLoadedSpy.callCount).toEqual(2); + }); + }); + + describe("when stopped", () => { + beforeAll(async () => { + await core.stop(); + }); + + it("should have removed all alerts", async () => { + expect(filterPluginAlerts(core.alerts)).toEqual([]); + }); + + it("should have executed logger in stop method", () => { + expect( + filterLogs(core.logs, "[plugins:test-plugin] Log from stop method").length + ).toEqual(1); + }); + }); + }); + }; + + const testAsyncPlugin = (description, pluginConstructor, options = {}) => { + describe(`${description} with async methods`, () => { + afterAll(async () => { + sandbox.reset(); + await core.stop(); + }); + + describe("when started", () => { + it("should start server when all initialization is finished", async () => { + const timeCounter = new TimeCounter(); + core = await startCore("web-tutorial", { + plugins: { register: [pluginConstructor] }, + }); + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(3000); + }); + + if (!options.doNotTestConfig) { + it("should have received config", async () => { + expect(configSpy.getCall(0).args[0]).not.toBe(undefined); + }); + } + + it("should have added two alerts", async () => { + expect(filterPluginAlerts(core.alerts)).toEqual([ + { + // Plugin id is still not available in register method + // It should have been renamed when start alert is received using a different context + context: "plugins:test-plugin:test-register", + message: "Warning registering plugin", + error: undefined, + }, + { + context: "plugins:test-plugin:test-start", + message: "Warning starting plugin", + error: undefined, + }, + ]); + }); + }); + }); + }; + + testPlugin( + "created as a Class", + class Plugin { + static get id() { + return "test-plugin"; + } + + constructor({ addRouter, alerts, config, logger }) { + logger.info("Log from register method"); + addRouter("/foo-path", customRouter); + alerts.set("test-register", "Warning registering plugin"); + registerSpy(); + configSpy(config); + } + init({ onChangeAlerts, onChangeMocks, logger, config }) { + logger.info("Log from init method"); + initSpy( + config.root.namespace("files").option("path").value, + config.root.namespace("server").option("port").value, + config.root.namespace("mocks").option("delay").value + ); + config.root.option("log").value = "silly"; + onChangeAlerts(changeAlertsSpy); + onChangeMocks(mocksLoadedSpy); + } + start({ alerts, logger }) { + logger.info("Log from start method"); + alerts.set("test-start", "Warning starting plugin"); + startSpy(); + } + stop({ alerts, logger }) { + logger.info("Log from stop method"); + alerts.clean(); + } + } + ); + + testPlugin( + "created as a Class with register method", + class Plugin { + static get id() { + return "test-plugin"; + } + + register({ addRouter, alerts, config, logger }) { + logger.info("Log from register method"); + addRouter("/foo-path", customRouter); + alerts.set("test-register", "Warning registering plugin"); + registerSpy(); + configSpy(config); + } + init({ logger, config, onChangeAlerts, onChangeMocks }) { + logger.info("Log from init method"); + initSpy( + config.root.namespace("files").option("path").value, + config.root.namespace("server").option("port").value, + config.root.namespace("mocks").option("delay").value + ); + config.root.option("log").value = "silly"; + onChangeAlerts(changeAlertsSpy); + onChangeMocks(mocksLoadedSpy); + } + start({ alerts, logger }) { + logger.info("Log from start method"); + alerts.set("test-start", "Warning starting plugin"); + startSpy(); + } + stop({ alerts, logger }) { + logger.info("Log from stop method"); + alerts.clean(); + } + } + ); + + testPlugin( + "created as a Class with register method and without static id", + class Plugin { + register({ addRouter, alerts, config, logger }) { + logger.info("Log from register method"); + addRouter("/foo-path", customRouter); + alerts.set("test-register", "Warning registering plugin"); + registerSpy(); + configSpy(config); + } + init({ logger, config, onChangeAlerts, onChangeMocks }) { + logger.info("Log from init method"); + initSpy( + config.root.namespace("files").option("path").value, + config.root.namespace("server").option("port").value, + config.root.namespace("mocks").option("delay").value + ); + config.root.option("log").value = "silly"; + onChangeAlerts(changeAlertsSpy); + onChangeMocks(mocksLoadedSpy); + } + start({ alerts, logger }) { + logger.info("Log from start method"); + alerts.set("test-start", "Warning starting plugin"); + startSpy(); + } + stop({ alerts, logger }) { + logger.info("Log from stop method"); + alerts.clean(); + } + get id() { + return "test-plugin"; + } + } + ); + + testAsyncPlugin( + "created as a Class", + class Plugin { + static get id() { + return "test-plugin"; + } + + register({ addRouter, alerts, config }) { + addRouter("/foo-path", customRouter); + alerts.set("test-register", "Warning registering plugin"); + registerSpy(); + configSpy(config); + } + async init() { + await wait(2000); + } + async start({ alerts }) { + alerts.set("test-start", "Warning starting plugin"); + await wait(1000); + } + } + ); +}); diff --git a/test/core-e2e-legacy/src/port-argument.spec.js b/test/core-e2e-legacy/src/port-argument.spec.js new file mode 100644 index 000000000..afbc0b1d5 --- /dev/null +++ b/test/core-e2e-legacy/src/port-argument.spec.js @@ -0,0 +1,35 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); + +describe("port command line argument", () => { + let cli; + + beforeAll(async () => { + cli = mocksRunner(["--files.path=web-tutorial", "--server.port=3005"]); + await waitForServer(3005); + }); + + afterAll(async () => { + removeConfigFile(); + await cli.kill(); + }); + + it("should set server port", async () => { + const users = await fetch("/api/users", { + port: 3005, + }); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); +}); diff --git a/test/core-e2e-legacy/src/port-setting.spec.js b/test/core-e2e-legacy/src/port-setting.spec.js new file mode 100644 index 000000000..ec4897ff8 --- /dev/null +++ b/test/core-e2e-legacy/src/port-setting.spec.js @@ -0,0 +1,49 @@ +/* +Copyright 2019 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { startCore, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); + +describe("port setting", () => { + let core; + + beforeAll(async () => { + core = await startCore(); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("When started", () => { + it("should be listening on port 3100", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); + + describe("When port setting is changed", () => { + it("should be listening on new port", async () => { + core.config.namespace("server").option("port").value = 3005; + await waitForServer(3005); + const users = await fetch("/api/users", { + port: 3005, + }); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/route-handler.spec.js b/test/core-e2e-legacy/src/route-handler.spec.js new file mode 100644 index 000000000..8952e2b8e --- /dev/null +++ b/test/core-e2e-legacy/src/route-handler.spec.js @@ -0,0 +1,58 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + createCore, + startExistingCore, + fetch, + fixturesFolder, + findTrace, + removeConfigFile, +} = require("./support/helpers"); + +const RouteHandler = require("./fixtures/config-file-with-routes-handler/CustomRoutesHandler"); + +describe("when adding route handlers", () => { + describe("When started", () => { + let core; + + beforeAll(async () => { + core = createCore(); + core.addRoutesHandler(RouteHandler); + await startExistingCore(core, fixturesFolder("custom-routes-handler"), { + mocks: { selected: "custom-users" }, + }); + core.tracer.set("debug", "store"); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + it("should serve users in /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("custom handler should have traced", async () => { + expect( + findTrace( + 'Responding with custom route handler to route variant "get-users:custom-success"', + core.tracer.store + ) + ).toBeDefined(); + expect(findTrace("Custom request GET =>", core.tracer.store)).toBeDefined(); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/route-variants.spec.js b/test/core-e2e-legacy/src/route-variants.spec.js new file mode 100644 index 000000000..2a7b526d1 --- /dev/null +++ b/test/core-e2e-legacy/src/route-variants.spec.js @@ -0,0 +1,300 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + startCore, + fetch, + waitForServer, + findTrace, + removeConfigFile, +} = require("./support/helpers"); + +describe("route variants", () => { + let core; + + beforeAll(async () => { + core = await startCore("middleware-route", { + mocks: { selected: "base" }, + }); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + describe("base mock", () => { + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users?req=1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("middleware should have traced request 1", async () => { + expect( + findTrace("Middleware in request 1 => GET => /api/users", core.tracer.store) + ).toBeDefined(); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1?req=2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("middleware should have traced request 2", async () => { + expect( + findTrace("Middleware in request 2 => GET => /api/users/1", core.tracer.store) + ).toBeDefined(); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2?req=3"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("middleware should have traced request 3", async () => { + expect( + findTrace("Middleware in request 3 => GET => /api/users/2", core.tracer.store) + ).toBeDefined(); + }); + }); + + describe('when using route variant "get-user:2"', () => { + it("should serve users under the /api/users path", async () => { + core.mocks.useRouteVariant("get-user:2"); + const users = await fetch("/api/users?req=4"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("middleware should have traced request 4", async () => { + expect( + findTrace("Middleware in request 4 => GET => /api/users", core.tracer.store) + ).toBeDefined(); + }); + + it("should serve user 2 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1?req=5"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("middleware should have traced request when requested user 1 again", async () => { + expect( + findTrace("Middleware in request 5 => GET => /api/users/1", core.tracer.store) + ).toBeDefined(); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2?req=6"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("middleware should have traced request when requested user 2 again", async () => { + expect( + findTrace("Middleware in request 6 => GET => /api/users/2", core.tracer.store) + ).toBeDefined(); + }); + }); + + describe('when using route variant "tracer:disabled"', () => { + it("should serve users under the /api/users path", async () => { + core.mocks.useRouteVariant("tracer:disabled"); + const users = await fetch("/api/users?req=7"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("middleware should have not traced request", async () => { + expect(findTrace("Middleware in request 7", core.tracer.store)).toBeUndefined(); + }); + + it("should serve user 2 under the /api/users/1 path", async () => { + core.mocks.useRouteVariant("get-user:2"); + const users = await fetch("/api/users/1?req=8"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("middleware should have not traced request 8", async () => { + expect(findTrace("Middleware in request 8", core.tracer.store)).toBeUndefined(); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2?req=9"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("middleware should have not traced request 9", async () => { + expect(findTrace("Middleware in request 9", core.tracer.store)).toBeUndefined(); + }); + }); + + describe('when using route variant "tracer:enabled"', () => { + it("should serve users under the /api/users path", async () => { + core.mocks.useRouteVariant("tracer:enabled"); + const users = await fetch("/api/users?req=10"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("middleware should have traced request", async () => { + expect( + findTrace("Middleware in request 10 => GET => /api/users", core.tracer.store) + ).toBeDefined(); + }); + + it("should serve user 2 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1?req=11"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("middleware should have traced request 11", async () => { + expect( + findTrace("Middleware in request 11 => GET => /api/users/1", core.tracer.store) + ).toBeDefined(); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2?req=12"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("middleware should have traced request 12", async () => { + expect( + findTrace("Middleware in request 12 => GET => /api/users/2", core.tracer.store) + ).toBeDefined(); + }); + }); + + describe("when restoring route variants", () => { + it("should serve users under the /api/users path", async () => { + core.mocks.restoreRoutesVariants(); + const users = await fetch("/api/users?req=1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1?req=2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2?req=3"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe('when using route variant "get-user:real"', () => { + it("should serve users under the /api/users path", async () => { + core.mocks.useRouteVariant("get-user:real"); + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); + + describe('when changing mock to "user-2"', () => { + beforeEach(() => { + core.config.namespace("mocks").option("selected").value = "user-2"; + }); + + it("should serve users collection mock under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 2 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); + + describe('when using route variant "get-user:real" again', () => { + it("should serve user 1 under the /api/users/1 path", async () => { + core.mocks.useRouteVariant("get-user:real"); + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); + + describe("when restoring route variants again", () => { + it("should serve user 2 under the /api/users/1 path", async () => { + core.mocks.restoreRoutesVariants(); + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/scaffold.spec.js b/test/core-e2e-legacy/src/scaffold.spec.js new file mode 100644 index 000000000..826315b8f --- /dev/null +++ b/test/core-e2e-legacy/src/scaffold.spec.js @@ -0,0 +1,230 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const path = require("path"); +const fsExtra = require("fs-extra"); +const { + mocksRunner, + fetch, + waitForServer, + fixturesFolder, + removeConfigFile, +} = require("./support/helpers"); + +const configFile = path.resolve(fixturesFolder("temp"), "mocks.config.js"); +const customStarter = path.resolve(fixturesFolder("temp"), "starter"); + +describe("when nor config file nor mocks folder exists", () => { + let mocks; + + describe("When inited", () => { + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.ensureDir(fixturesFolder("temp")); + await fsExtra.copy(fixturesFolder("starter"), customStarter); + mocks = mocksRunner([], { + customBinary: customStarter, + cwd: fixturesFolder("temp"), + }); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await mocks.kill(); + await fsExtra.remove(fixturesFolder("temp")); + }); + + describe("when started for the first time", () => { + it("should have created the mocks scaffold", async () => { + expect( + fsExtra.existsSync(path.resolve(fixturesFolder("temp"), "mocks", "mocks.json")) + ).toEqual(true); + }); + + it("should have created the config file", async () => { + expect(fsExtra.existsSync(configFile)).toEqual(true); + }); + + it("should have base as selected mock in config file", async () => { + const config = require(configFile); + expect(config.mocks.selected).toEqual("base"); + }); + + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe("when stopped and started again", () => { + beforeAll(async () => { + await mocks.kill(); + mocks = mocksRunner([], { + customBinary: customStarter, + cwd: fixturesFolder("temp"), + }); + await waitForServer(); + }); + + it("should have base as selected mock in config file", async () => { + const config = require(configFile); + expect(config.mocks.selected).toEqual("base"); + }); + + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + }); + + describe("When mocks folder already exists", () => { + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.ensureDir(fixturesFolder("temp")); + await fsExtra.copy( + fixturesFolder("files-error-routes"), + path.resolve(fixturesFolder("temp"), "mocks") + ); + await fsExtra.copy(fixturesFolder("starter"), customStarter); + mocks = mocksRunner([], { + customBinary: customStarter, + cwd: fixturesFolder("temp"), + }); + await waitForServer(); + }); + + afterAll(async () => { + await mocks.kill(); + await fsExtra.remove(fixturesFolder("temp")); + }); + + it("should have created the config file", async () => { + expect(fsExtra.existsSync(configFile)).toEqual(true); + }); + + it("should have tried to load provided mocks", async () => { + expect(mocks.logs.all).toEqual(expect.stringContaining("Mock with id 'user-2' is invalid")); + }); + }); + + describe("When readFile is disabled", () => { + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.ensureDir(fixturesFolder("temp")); + await fsExtra.copy(fixturesFolder("starter"), customStarter); + mocks = mocksRunner([], { + customBinary: customStarter, + cwd: fixturesFolder("temp"), + env: { + MOCKS_CONFIG_READ_FILE: false, + }, + }); + await waitForServer(); + }); + + afterAll(async () => { + await mocks.kill(); + await fsExtra.remove(fixturesFolder("temp")); + }); + + it("should have not created the config file", async () => { + expect(fsExtra.existsSync(configFile)).toEqual(false); + }); + + it("should serve users under the /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.status).toEqual(200); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve user 1 under the /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 1 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); + + describe("When config file already exists", () => { + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.ensureDir(fixturesFolder("temp")); + await fsExtra.copy( + path.resolve(fixturesFolder("config-file-no-path"), "mocks.config.js"), + path.resolve(fixturesFolder("temp"), "mocks.config.js") + ); + await fsExtra.copy(fixturesFolder("starter"), customStarter); + mocks = mocksRunner([], { + customBinary: customStarter, + cwd: fixturesFolder("temp"), + }); + await waitForServer(); + }); + + afterAll(async () => { + await mocks.kill(); + await fsExtra.remove(fixturesFolder("temp")); + }); + + it("should have created the mocks scaffold", async () => { + expect( + fsExtra.existsSync(path.resolve(fixturesFolder("temp"), "mocks", "mocks.json")) + ).toEqual(true); + }); + + it("should serve user 2 under the /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.status).toEqual(200); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/support/helpers.js b/test/core-e2e-legacy/src/support/helpers.js new file mode 100644 index 000000000..588513311 --- /dev/null +++ b/test/core-e2e-legacy/src/support/helpers.js @@ -0,0 +1,188 @@ +/* +Copyright 2019 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ +const path = require("path"); + +const deepMerge = require("deepmerge"); +const crossFetch = require("cross-fetch"); +const waitOn = require("wait-on"); +const fsExtra = require("fs-extra"); + +const Core = require("@mocks-server/core"); +const CliRunner = require("@mocks-server/cli-runner"); + +const SERVER_PORT = 3100; +const DEFAULT_BINARY_PATH = "./starter"; +const FIXTURES_PATH = path.resolve(__dirname, "..", "fixtures"); +const CONFIG_FILE = path.resolve(FIXTURES_PATH, "mocks.config.js"); + +const defaultOptions = { + log: "silent", + server: { + port: SERVER_PORT, + }, + files: { + watch: false, + }, +}; + +const defaultRequestOptions = { + method: "GET", + headers: { + "Content-Type": "application/json", + }, +}; + +const fixturesFolder = (folderName) => { + return path.resolve(__dirname, "..", "fixtures", folderName); +}; + +const createCore = (options = {}) => { + return new Core( + deepMerge.all([ + { + config: { + readFile: false, + readArguments: false, + readEnvironment: false, + }, + }, + options, + ]) + ); +}; + +const startExistingCore = (core, mocksPath, options = {}) => { + const mocks = mocksPath || "web-tutorial"; + return core + .init( + deepMerge.all([ + defaultOptions, + { + files: { + ...defaultOptions.files, + path: fixturesFolder(mocks), + }, + }, + options, + ]) + ) + .then(() => { + return core.start().then(() => { + return Promise.resolve(core); + }); + }); +}; + +const startCore = (mocksPath, options = {}) => { + return startExistingCore(createCore(options), mocksPath, options); +}; + +const serverUrl = (port) => { + return `http://127.0.0.1:${port || SERVER_PORT}`; +}; + +const fetch = (uri, options = {}) => { + const requestOptions = { + ...defaultRequestOptions, + ...options, + }; + + return crossFetch(`${serverUrl(options.port)}${uri}`, { + ...requestOptions, + }).then((res) => { + return res + .json() + .then((processedRes) => ({ body: processedRes, status: res.status, headers: res.headers })) + .catch(() => ({ body: null, status: res.status, headers: res.headers })); + }); +}; + +class TimeCounter { + constructor() { + this._startTime = new Date(); + } + + _getMiliseconds() { + this._miliseconds = this._endTime - this._startTime; + } + + get total() { + return this._miliseconds; + } + + stop() { + this._endTime = new Date(); + this._getMiliseconds(); + } +} + +const wait = (time = 1000) => { + return new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, time); + }); +}; + +const waitForServer = (port) => { + return waitOn({ resources: [`tcp:127.0.0.1:${port || SERVER_PORT}`] }); +}; + +const waitForServerUrl = (url) => { + return waitOn({ resources: [`${serverUrl()}${url}`] }); +}; + +const mocksRunner = (args = [], options = {}) => { + const argsToSend = [...args]; + if (!options.customBinary) { + argsToSend.unshift(DEFAULT_BINARY_PATH); + } else { + argsToSend.unshift(options.customBinary); + } + return new CliRunner(argsToSend, { + cwd: FIXTURES_PATH, + ...options, + }); +}; + +const filterAlerts = (alertContextFragment, alerts) => { + return alerts.filter((alert) => alert.context.includes(alertContextFragment)); +}; + +const findAlert = (alertContextFragment, alerts) => { + return alerts.find((alert) => alert.context.includes(alertContextFragment)); +}; + +const findTrace = (traceFragment, traces) => { + return traces.find((trace) => trace.includes(traceFragment)); +}; + +const removeConfigFile = () => { + if (fsExtra.existsSync(CONFIG_FILE)) { + fsExtra.removeSync(CONFIG_FILE); + } +}; + +module.exports = { + createCore, + startExistingCore, + startCore, + fetch, + TimeCounter, + mocksRunner, + wait, + waitForServer, + waitForServerUrl, + fixturesFolder, + filterAlerts, + findAlert, + findTrace, + removeConfigFile, +}; diff --git a/test/core-e2e-legacy/src/typescript.spec.js b/test/core-e2e-legacy/src/typescript.spec.js new file mode 100644 index 000000000..b8e440516 --- /dev/null +++ b/test/core-e2e-legacy/src/typescript.spec.js @@ -0,0 +1,82 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + mocksRunner, + fetch, + waitForServer, + fixturesFolder, + wait, + removeConfigFile, +} = require("./support/helpers"); + +describe("when babelRegister is enabled and typescript files are used", () => { + let mocks; + + const runTests = (config) => { + describe(`When started using ${config} config`, () => { + beforeAll(async () => { + mocks = mocksRunner([], { + cwd: fixturesFolder(config), + }); + await waitForServer(); + await wait(3000); + }); + + afterAll(async () => { + removeConfigFile(); + await mocks.kill(); + }); + + it("should have log level silly", async () => { + expect(mocks.logs.current).toEqual(expect.stringContaining("[silly]")); + }); + + it("should serve users in /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + + it("should serve books in /api/books path", async () => { + const users = await fetch("/api/books"); + expect(users.body).toEqual([ + { id: 1, title: "1984" }, + { id: 2, title: "Brave New World" }, + ]); + }); + + it("middleware should trace request and add headers", async () => { + const users = await fetch("/api/users"); + expect(users.headers.get("x-mocks-server-example")).toEqual("custom-header-typescript"); + expect(mocks.logs.current).toEqual( + expect.stringContaining( + "Custom header added by add-headers:enabled route variant middleware using TypeScript" + ) + ); + }); + + it("should serve user 1 in /api/users/1 path", async () => { + const users = await fetch("/api/users/1"); + expect(users.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should serve user 2 in /api/users/2 path", async () => { + const users = await fetch("/api/users/2"); + expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + }); + }; + + runTests("typescript-config"); + runTests("typescript-custom-config"); +}); diff --git a/test/core-e2e-legacy/src/validations.spec.js b/test/core-e2e-legacy/src/validations.spec.js new file mode 100644 index 000000000..511d29ceb --- /dev/null +++ b/test/core-e2e-legacy/src/validations.spec.js @@ -0,0 +1,177 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const { + startCore, + waitForServer, + findAlert, + filterAlerts, + fetch, + removeConfigFile, +} = require("./support/helpers"); + +describe("mocks and routes validations", () => { + let core; + + describe("when files does not export an array", () => { + beforeAll(async () => { + core = await startCore("validation-not-array"); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await core.stop(); + }); + + it("should have added an alert about route file not exporting array", () => { + expect(findAlert("files:routes:file:", core.alerts).message).toEqual( + expect.stringContaining("File does not export an array") + ); + }); + + it("should have loaded valid routes", () => { + expect(core.mocks.plainRoutes.length).toEqual(1); + }); + + it("should have added an alert about mocks file loading error", () => { + expect(findAlert("files:mocks", core.alerts).error.message).toEqual( + expect.stringContaining("File does not export an array") + ); + }); + + it("should have not loaded mocks", () => { + expect(core.mocks.plainMocks.length).toEqual(0); + }); + }); + + describe("when routes are not valid", () => { + beforeAll(async () => { + core = await startCore("validations"); + await waitForServer(); + }); + + afterAll(async () => { + await core.stop(); + }); + + it("should have added an alert about route variant with duplicated id", () => { + expect( + findAlert("mocks:loadRoutes:get-user-variant-invalid:variants:1:duplicated", core.alerts) + .message + ).toEqual( + "Route variant with duplicated id '1' detected in route 'get-user-variant-invalid'. It has been ignored" + ); + }); + + it("should have added an alert about route variant not valid", () => { + expect( + findAlert("mocks:loadRoutes:get-user-variant-invalid:variants:2:validation", core.alerts) + .message + ).toEqual( + "Variant with id '2' in route with id 'get-user-variant-invalid' is invalid: /response: type must be object. /response: instanceof must pass \"instanceof\" keyword validation. /response: oneOf must match exactly one schema in oneOf" + ); + }); + + it("should have added an alert about route duplicated", () => { + expect( + findAlert("mocks:loadRoutes:get-user-variant-invalid:duplicated", core.alerts).message + ).toEqual( + "Route with duplicated id 'get-user-variant-invalid' detected. It has been ignored" + ); + }); + + it("should have added an alert about route invalid", () => { + expect( + findAlert("mocks:loadRoutes:get-users-invalid:validation", core.alerts).message + ).toEqual( + "Route with id 'get-users-invalid' is invalid: /method: enum must be equal to one of the allowed values. /method: type must be array. /method: oneOf must match exactly one schema in oneOf" + ); + }); + + it("should have added an alert about mock routeVariant not found", () => { + expect(findAlert("mocks:loadMocks:base:variants", core.alerts).message).toEqual( + "Mock with id 'base' is invalid: routeVariant with id 'get-users-invalid:success' was not found, use a valid 'routeId:variantId' identifier" + ); + }); + + it("should have added an alert about mock duplicated", () => { + expect(findAlert("mocks:loadMocks:1:duplicated", core.alerts).message).toEqual( + "Mock with duplicated id 'base' detected. It has been ignored" + ); + }); + + it("should have added an alert about mock routeVariant not found in mock 2", () => { + expect(findAlert("mocks:loadMocks:invalid-variant:variants", core.alerts).message).toEqual( + "Mock with id 'invalid-variant' is invalid: routeVariant with id 'get-user-variant-invalid:2' was not found, use a valid 'routeId:variantId' identifier" + ); + }); + + it("should have added an alert about invalid mock 3", () => { + expect(findAlert("mocks:loadMocks:invalid-mock:validation", core.alerts).message).toEqual( + "Mock with id 'invalid-mock' is invalid: must have required property 'routesVariants'" + ); + }); + + it("should have added an alert about mock with invalid from", () => { + expect(findAlert("mocks:loadMocks:invalid-from:from", core.alerts).message).toEqual( + "Mock with invalid 'from' property detected, 'foo' was not found" + ); + }); + + it("should have added an alert about mock with duplicated routes", () => { + expect(findAlert("mocks:loadMocks:duplicated-route:variants", core.alerts).message).toEqual( + "Mock with id 'duplicated-route' is invalid: route with id 'get-user' is used more than once in the same mock" + ); + }); + + it("should have added an alert about errors processing mocks", () => { + expect(filterAlerts("mocks:loadMocks", core.alerts)[0].message).toEqual( + "Critical errors found while loading mocks: 1" + ); + }); + + it("should have not loaded invalid routes", () => { + expect(core.mocks.plainRoutes.length).toEqual(2); + }); + + it("should have not loaded invalid route variants", () => { + expect(core.mocks.plainRoutesVariants.length).toEqual(3); + }); + + it("should have not loaded invalid mocks", () => { + expect(core.mocks.plainMocks.length).toEqual(4); + }); + + it("should return user 2 at /api/users/1", async () => { + const response = await fetch("/api/users/1"); + expect(response.status).toEqual(200); + expect(response.body).toEqual({ id: 2, name: "Jane Doe" }); + }); + + it("should return user 1 at /api/invalid-users/1", async () => { + const response = await fetch("/api/invalid-users/1"); + expect(response.status).toEqual(200); + expect(response.body).toEqual({ id: 1, name: "John Doe" }); + }); + + it("should return 404 at /api/users", async () => { + const response = await fetch("/api/users"); + expect(response.status).toEqual(404); + }); + + it("should return user 1 at /api/invalid-users/1 when changing to mock with no valid variant", async () => { + core.config.namespace("mocks").option("selected").value = "invalid-variant"; + const response = await fetch("/api/invalid-users/1"); + expect(response.status).toEqual(200); + expect(response.body).toEqual({ id: 1, name: "John Doe" }); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/watch-disabled.spec.js b/test/core-e2e-legacy/src/watch-disabled.spec.js new file mode 100644 index 000000000..e2c516d3c --- /dev/null +++ b/test/core-e2e-legacy/src/watch-disabled.spec.js @@ -0,0 +1,61 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const fsExtra = require("fs-extra"); +const { + mocksRunner, + fetch, + fixturesFolder, + waitForServer, + wait, + removeConfigFile, +} = require("./support/helpers"); + +describe("when files watch is disabled", () => { + let mocks; + + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.copy(fixturesFolder("web-tutorial"), fixturesFolder("temp")); + mocks = mocksRunner(["--files.path=temp", "--no-files.watch"]); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await fsExtra.remove(fixturesFolder("temp")); + await mocks.kill(); + }); + + describe("When started", () => { + it("should serve users", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); + + describe("When files are modified", () => { + beforeAll(async () => { + await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); + await wait(4000); + }); + + it("should serve users in /api/users path", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); +}); diff --git a/test/core-e2e-legacy/src/watch-enabled.spec.js b/test/core-e2e-legacy/src/watch-enabled.spec.js new file mode 100644 index 000000000..832da94d7 --- /dev/null +++ b/test/core-e2e-legacy/src/watch-enabled.spec.js @@ -0,0 +1,84 @@ +/* +Copyright 2021 Javier Brea + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +*/ + +const fsExtra = require("fs-extra"); +const { + mocksRunner, + fetch, + fixturesFolder, + waitForServer, + waitForServerUrl, + wait, + removeConfigFile, +} = require("./support/helpers"); + +describe("when files watch is enabled", () => { + let mocks; + + beforeAll(async () => { + await fsExtra.remove(fixturesFolder("temp")); + await fsExtra.copy(fixturesFolder("web-tutorial"), fixturesFolder("temp")); + mocks = mocksRunner(["--files.path=temp"]); + await waitForServer(); + }); + + afterAll(async () => { + removeConfigFile(); + await fsExtra.remove(fixturesFolder("temp")); + await mocks.kill(); + }); + + describe("When started", () => { + it("should serve users", async () => { + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); + + describe("When files are modified", () => { + it("should serve users modified in /api/users path", async () => { + await wait(2000); + await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); + await waitForServerUrl("/api/new-users"); + const users = await fetch("/api/users"); + expect(users.headers.get("x-custom-header")).toEqual("foo-header"); + expect(users.headers.get("x-another-header")).toEqual("another-header"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe modified" }, + { id: 2, name: "Jane Doe modified" }, + ]); + }); + + it("should serve new users in /api/new-users path", async () => { + const users = await fetch("/api/new-users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe modified" }, + { id: 2, name: "Jane Doe modified" }, + { id: 3, name: "Brand new user" }, + ]); + }); + }); + + describe("When non routes nor mocks files are modified", () => { + it("should serve new data in /api/users path", async () => { + await wait(2000); + await fsExtra.copy(fixturesFolder("web-tutorial/db"), fixturesFolder("temp/db")); + await wait(5000); + const users = await fetch("/api/users"); + expect(users.body).toEqual([ + { id: 1, name: "John Doe" }, + { id: 2, name: "Jane Doe" }, + ]); + }); + }); +}); diff --git a/test/core-e2e/jest.config.js b/test/core-e2e/jest.config.js index 5f8d8f9eb..4ed3b7ca6 100644 --- a/test/core-e2e/jest.config.js +++ b/test/core-e2e/jest.config.js @@ -6,7 +6,7 @@ module.exports = { clearMocks: true, testMatch: ["/src/**/*.spec.js"], - // testMatch: ["/src/**/load-mocks-and-routes.spec.js"], + // testMatch: ["/src/**/validations.spec.js"], // Indicates whether the coverage information should be collected while executing the test collectCoverage: false, diff --git a/test/core-e2e/src/config-file-with-custom-route-handler.spec.js b/test/core-e2e/src/config-file-with-custom-route-handler.spec.js index edaf3501d..f7c4a1d04 100644 --- a/test/core-e2e/src/config-file-with-custom-route-handler.spec.js +++ b/test/core-e2e/src/config-file-with-custom-route-handler.spec.js @@ -46,9 +46,7 @@ describe("when adding route handlers in config file", () => { it("custom handler should have traced", async () => { expect(mocks.logs.current).toEqual( - expect.stringContaining( - 'Responding with custom route handler to route variant "get-users:custom-success"' - ) + expect.stringContaining("Custom request GET => /api/users") ); expect(mocks.logs.current).toEqual(expect.stringContaining("Custom request GET =>")); }); diff --git a/test/core-e2e/src/delay-setting.spec.js b/test/core-e2e/src/delay-setting.spec.js index f63de8015..084c24ce1 100644 --- a/test/core-e2e/src/delay-setting.spec.js +++ b/test/core-e2e/src/delay-setting.spec.js @@ -116,7 +116,7 @@ describe("delay setting", () => { it("should have zero delay in plain route variant", async () => { expect(core.mocks.plainRoutesVariants[2]).toEqual({ - handler: "default", + handler: "json", id: "get-user:zero-delay", delay: 0, response: { @@ -147,7 +147,7 @@ describe("delay setting", () => { it("should have null delay in plain route variant", async () => { expect(core.mocks.plainRoutesVariants[3]).toEqual({ - handler: "default", + handler: "json", id: "get-user:null-delay", delay: null, response: { diff --git a/test/core-e2e/src/fixtures/config-file-with-plugins/TraceMocksPlugin.js b/test/core-e2e/src/fixtures/config-file-with-plugins/TraceMocksPlugin.js index 405b8b987..ede2a6e7e 100644 --- a/test/core-e2e/src/fixtures/config-file-with-plugins/TraceMocksPlugin.js +++ b/test/core-e2e/src/fixtures/config-file-with-plugins/TraceMocksPlugin.js @@ -3,7 +3,7 @@ class Plugin { return "trace-mocks"; } - constructor({ core, config }) { + constructor({ mocks, config, logger }) { this._traceMocks = config.addOption({ name: "traceMocks", type: "boolean", @@ -12,35 +12,36 @@ class Plugin { }); this._traceMocks.onChange(this._onChangeTraceMocks.bind(this)); - this._core = core; + this._mocks = mocks; this._onChangeMocks = this._onChangeMocks.bind(this); + this._logger = logger; } get displayName() { return "trace-mocks"; } - init({ core }) { + init({ onChangeMocks, logger }) { this._enabled = this._traceMocks.value; - this._removeChangeMocksListener = core.onChangeMocks(this._onChangeMocks); - core.tracer.debug(`traceMocks initial value is ${this._traceMocks.value}`); + this._removeChangeMocksListener = onChangeMocks(this._onChangeMocks); + logger.debug(`traceMocks initial value is ${this._traceMocks.value}`); } traceMocks() { if (this._enabled && this._started) { - this._core.tracer.info(`There are ${this._core.mocks.plainMocks.length} mocks available`); + this._logger.info(`There are ${this._mocks.plainMocks.length} mocks available`); } } - start({ core }) { + start() { this._started = true; - core.tracer.debug("traceMocks plugin started"); + this._logger.debug("traceMocks plugin started"); this.traceMocks(); } - stop({ core }) { + stop() { this._started = false; - core.tracer.debug("traceMocks plugin stopped"); + this._logger.debug("traceMocks plugin stopped"); } _onChangeTraceMocks() { diff --git a/test/core-e2e/src/fixtures/config-file-with-plugins/TraceRoutesPlugin.js b/test/core-e2e/src/fixtures/config-file-with-plugins/TraceRoutesPlugin.js index 2a2b98e55..28b4d6fd2 100644 --- a/test/core-e2e/src/fixtures/config-file-with-plugins/TraceRoutesPlugin.js +++ b/test/core-e2e/src/fixtures/config-file-with-plugins/TraceRoutesPlugin.js @@ -3,7 +3,7 @@ class Plugin { return "trace-routes"; } - constructor({ core, config }) { + constructor({ mocks, config, logger }) { this._traceRoutes = config.addOption({ name: "traceRoutes", type: "boolean", @@ -12,35 +12,36 @@ class Plugin { }); this._traceRoutes.onChange(this._onChangeTraceRoutes.bind(this)); - this._core = core; + this._mocks = mocks; this._onChangeMocks = this._onChangeMocks.bind(this); + this._logger = logger; } get displayName() { return "trace-routes"; } - init({ core }) { + init({ onChangeMocks, logger }) { this._enabled = this._traceRoutes.value; - this._removeChangeMocksListener = core.onChangeMocks(this._onChangeMocks); - core.tracer.debug(`traceRoutes initial value is ${this._traceRoutes.value}`); + this._removeChangeMocksListener = onChangeMocks(this._onChangeMocks); + logger.debug(`traceRoutes initial value is ${this._traceRoutes.value}`); } traceRoutes() { if (this._enabled && this._started) { - this._core.tracer.info(`There are ${this._core.mocks.plainRoutes.length} routes available`); + this._logger.info(`There are ${this._mocks.plainRoutes.length} routes available`); } } - start({ core }) { + start() { this._started = true; - core.tracer.debug("traceRoutes plugin started"); + this._logger.debug("traceRoutes plugin started"); this.traceRoutes(); } - stop({ core }) { + stop() { this._started = false; - core.tracer.debug("traceRoutes plugin stopped"); + this._logger.debug("traceRoutes plugin stopped"); } _onChangeTraceRoutes() { diff --git a/test/core-e2e/src/fixtures/config-file-with-plugins/mocks/routes/middlewares.js b/test/core-e2e/src/fixtures/config-file-with-plugins/mocks/routes/middlewares.js index a15baef77..65704bfd0 100644 --- a/test/core-e2e/src/fixtures/config-file-with-plugins/mocks/routes/middlewares.js +++ b/test/core-e2e/src/fixtures/config-file-with-plugins/mocks/routes/middlewares.js @@ -10,17 +10,23 @@ module.exports = [ variants: [ { id: "enabled", - response: (req, res, next, mocksServer) => { - res.set("x-mocks-server-example", "some-value"); - mocksServer.tracer.info( - "Custom header added by add-headers:enabled route variant middleware" - ); - next(); + handler: "middleware", + response: { + middleware: (_req, res, next, mocksServer) => { + res.set("x-mocks-server-example", "some-value"); + mocksServer.tracer.info( + "Custom header added by add-headers:enabled route variant middleware" + ); + next(); + } }, }, { id: "disabled", - response: (req, res, next) => next(), + handler: "middleware", + response: { + middleware: (_req, _res, next) => next(), + }, }, ], }, diff --git a/test/core-e2e/src/fixtures/config-file-with-plugins/mocks/routes/users.js b/test/core-e2e/src/fixtures/config-file-with-plugins/mocks/routes/users.js index 473241e3c..f83b063a9 100644 --- a/test/core-e2e/src/fixtures/config-file-with-plugins/mocks/routes/users.js +++ b/test/core-e2e/src/fixtures/config-file-with-plugins/mocks/routes/users.js @@ -22,6 +22,7 @@ module.exports = [ variants: [ { id: "success", // id of the variant + handler: "json", response: { status: 200, // status to send body: USERS, // body to send @@ -29,6 +30,7 @@ module.exports = [ }, { id: "error", // id of the variant + handler: "json", response: { status: 400, // status to send body: { @@ -46,6 +48,7 @@ module.exports = [ variants: [ { id: "success", // id of the variant + handler: "json", response: { status: 200, // status to send body: USERS[0], // body to send @@ -53,19 +56,22 @@ module.exports = [ }, { id: "real", // id of the variant - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); - } - }, + handler: "middleware", + response: { + middleware:(req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, + } }, ], }, diff --git a/test/core-e2e/src/fixtures/config-file-with-routes-handler/CustomRoutesHandler.js b/test/core-e2e/src/fixtures/config-file-with-routes-handler/CustomRoutesHandler.js index d28443a10..97e9c341b 100644 --- a/test/core-e2e/src/fixtures/config-file-with-routes-handler/CustomRoutesHandler.js +++ b/test/core-e2e/src/fixtures/config-file-with-routes-handler/CustomRoutesHandler.js @@ -15,24 +15,22 @@ class CustomRoutesHandler { return "custom"; } - constructor(route, core) { - this._method = route.method; - this._url = route.url; - this._response = route.response; - this._variantId = route.variantId; + static get version() { + return "4"; + } + + constructor(response, core) { + this._response = response; this._core = core; } - middleware(req, res, next) { - this._core.tracer.debug( - `Responding with custom route handler to route variant "${this._variantId}" | req: ${req.id}` - ); - this._core.tracer.info(`Custom request ${req.method} => ${req.url} => "${this._variantId}"`); + middleware(req, res) { + this._core.logger.info(`Custom request ${req.method} => ${req.url}`); res.status(this._response.status); res.send(this._response.body); } - get plainResponsePreview() { + get preview() { return { body: this._response.body, status: this._response.status, diff --git a/test/core-e2e/src/fixtures/custom-routes-handler/routes/users.js b/test/core-e2e/src/fixtures/custom-routes-handler/routes/users.js index 13b7cb0d1..77261961b 100644 --- a/test/core-e2e/src/fixtures/custom-routes-handler/routes/users.js +++ b/test/core-e2e/src/fixtures/custom-routes-handler/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: USERS, diff --git a/test/core-e2e/src/fixtures/delays/routes/user.js b/test/core-e2e/src/fixtures/delays/routes/user.js index e66bd28d5..a841c0e30 100644 --- a/test/core-e2e/src/fixtures/delays/routes/user.js +++ b/test/core-e2e/src/fixtures/delays/routes/user.js @@ -19,6 +19,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -26,6 +27,7 @@ module.exports = [ }, { id: "2", + handler: "json", delay: 2000, response: { status: 200, @@ -34,6 +36,7 @@ module.exports = [ }, { id: "zero-delay", + handler: "json", delay: 0, response: { status: 200, @@ -42,6 +45,7 @@ module.exports = [ }, { id: "null-delay", + handler: "json", delay: null, response: { status: 200, diff --git a/test/core-e2e/src/fixtures/delays/routes/users.js b/test/core-e2e/src/fixtures/delays/routes/users.js index 4957b82bb..54d4286e4 100644 --- a/test/core-e2e/src/fixtures/delays/routes/users.js +++ b/test/core-e2e/src/fixtures/delays/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: USERS, @@ -25,6 +26,7 @@ module.exports = [ }, { id: "delayed", + handler: "json", delay: 2000, response: { status: 200, diff --git a/test/core-e2e/src/fixtures/esm-modified/routes/middlewares.es b/test/core-e2e/src/fixtures/esm-modified/routes/middlewares.es index 85c4dae1b..3c0798fe9 100644 --- a/test/core-e2e/src/fixtures/esm-modified/routes/middlewares.es +++ b/test/core-e2e/src/fixtures/esm-modified/routes/middlewares.es @@ -10,17 +10,23 @@ const middlewares = [ variants: [ { id: "enabled", - response: (req, res, next, mocksServer) => { - res.set("x-mocks-server-example", "custom-header"); - mocksServer.tracer.info( - "Custom header added by add-headers:enabled route variant middleware" - ); - next(); + handler: "middleware", + response: { + middleware: (req, res, next, mocksServer) => { + res.set("x-mocks-server-example", "custom-header"); + mocksServer.tracer.info( + "Custom header added by add-headers:enabled route variant middleware" + ); + next(); + } }, }, { id: "disabled", - response: (req, res, next) => next(), + handler: "middleware", + response: { + middleware: (req, res, next) => next(), + } }, ], }, diff --git a/test/core-e2e/src/fixtures/esm-modified/routes/users.es b/test/core-e2e/src/fixtures/esm-modified/routes/users.es index 09a09a886..44dce4e23 100644 --- a/test/core-e2e/src/fixtures/esm-modified/routes/users.es +++ b/test/core-e2e/src/fixtures/esm-modified/routes/users.es @@ -13,6 +13,7 @@ const routes = [ variants: [ { id: "success", // id of the variant + handler: "json", response: { status: 200, // status to send body: USERS, // body to send @@ -20,6 +21,7 @@ const routes = [ }, { id: "error", // id of the variant + handler: "json", response: { status: 400, // status to send body: { @@ -37,6 +39,7 @@ const routes = [ variants: [ { id: "success", // id of the variant + handler: "json", response: { status: 200, // status to send body: USERS[0], // body to send @@ -44,17 +47,20 @@ const routes = [ }, { id: "real", // id of the variant - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } } }, }, @@ -67,6 +73,7 @@ const routes = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: [ diff --git a/test/core-e2e/src/fixtures/esm/routes/middlewares.es b/test/core-e2e/src/fixtures/esm/routes/middlewares.es index 85c4dae1b..3c0798fe9 100644 --- a/test/core-e2e/src/fixtures/esm/routes/middlewares.es +++ b/test/core-e2e/src/fixtures/esm/routes/middlewares.es @@ -10,17 +10,23 @@ const middlewares = [ variants: [ { id: "enabled", - response: (req, res, next, mocksServer) => { - res.set("x-mocks-server-example", "custom-header"); - mocksServer.tracer.info( - "Custom header added by add-headers:enabled route variant middleware" - ); - next(); + handler: "middleware", + response: { + middleware: (req, res, next, mocksServer) => { + res.set("x-mocks-server-example", "custom-header"); + mocksServer.tracer.info( + "Custom header added by add-headers:enabled route variant middleware" + ); + next(); + } }, }, { id: "disabled", - response: (req, res, next) => next(), + handler: "middleware", + response: { + middleware: (req, res, next) => next(), + } }, ], }, diff --git a/test/core-e2e/src/fixtures/esm/routes/users.es b/test/core-e2e/src/fixtures/esm/routes/users.es index 8957a9edd..39d718027 100644 --- a/test/core-e2e/src/fixtures/esm/routes/users.es +++ b/test/core-e2e/src/fixtures/esm/routes/users.es @@ -13,6 +13,7 @@ const routes = [ variants: [ { id: "success", // id of the variant + handler: "json", response: { status: 200, // status to send body: USERS, // body to send @@ -20,6 +21,7 @@ const routes = [ }, { id: "error", // id of the variant + handler: "json", response: { status: 400, // status to send body: { @@ -37,6 +39,7 @@ const routes = [ variants: [ { id: "success", // id of the variant + handler: "json", response: { status: 200, // status to send body: USERS[0], // body to send @@ -44,17 +47,20 @@ const routes = [ }, { id: "real", // id of the variant - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } } }, }, diff --git a/test/core-e2e/src/fixtures/files-error-mock/routes/user.js b/test/core-e2e/src/fixtures/files-error-mock/routes/user.js index ac18756e6..c9b2ac50c 100644 --- a/test/core-e2e/src/fixtures/files-error-mock/routes/user.js +++ b/test/core-e2e/src/fixtures/files-error-mock/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -32,17 +34,20 @@ module.exports = [ }, { id: "real", - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } } }, }, diff --git a/test/core-e2e/src/fixtures/files-error-mock/routes/users.js b/test/core-e2e/src/fixtures/files-error-mock/routes/users.js index 6c1d2aecd..f406dac80 100644 --- a/test/core-e2e/src/fixtures/files-error-mock/routes/users.js +++ b/test/core-e2e/src/fixtures/files-error-mock/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: USERS, @@ -25,6 +26,7 @@ module.exports = [ }, { id: "error", + handler: "json", response: { status: 403, body: { diff --git a/test/core-e2e/src/fixtures/files-error-routes/routes/user.js b/test/core-e2e/src/fixtures/files-error-routes/routes/user.js index ac18756e6..c9b2ac50c 100644 --- a/test/core-e2e/src/fixtures/files-error-routes/routes/user.js +++ b/test/core-e2e/src/fixtures/files-error-routes/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -32,17 +34,20 @@ module.exports = [ }, { id: "real", - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } } }, }, diff --git a/test/core-e2e/src/fixtures/json-files/routes/users.json b/test/core-e2e/src/fixtures/json-files/routes/users.json index c43baf0b1..fdd8e4715 100644 --- a/test/core-e2e/src/fixtures/json-files/routes/users.json +++ b/test/core-e2e/src/fixtures/json-files/routes/users.json @@ -6,6 +6,7 @@ "variants": [ { "id": "success", + "handler": "json", "response": { "status": 200, "body": [ @@ -22,6 +23,7 @@ }, { "id": "error", + "handler": "json", "response": { "status": 403, "body": { @@ -38,6 +40,7 @@ "variants": [ { "id": "1", + "handler": "json", "response": { "status": 200, "body": { @@ -48,6 +51,7 @@ }, { "id": "2", + "handler": "json", "response": { "status": 200, "body": { diff --git a/test/core-e2e/src/fixtures/middleware-route/routes/tracer.js b/test/core-e2e/src/fixtures/middleware-route/routes/tracer.js index 0aef6f4f8..788ca9893 100644 --- a/test/core-e2e/src/fixtures/middleware-route/routes/tracer.js +++ b/test/core-e2e/src/fixtures/middleware-route/routes/tracer.js @@ -18,18 +18,24 @@ module.exports = [ variants: [ { id: "enabled", - response: (req, res, next, core) => { - core.tracer.info( - `Middleware in request ${req.query.req} => ${req.method} => ${req.url}` - ); - next(); + handler: "middleware", + response: { + middleware: (req, _res, next, core) => { + core.tracer.info( + `Middleware in request ${req.query.req} => ${req.method} => ${req.url}` + ); + next(); + } }, }, { id: "disabled", - response: (req, res, next, core) => { - next(); - }, + handler: "middleware", + response: { + middleware:(_req, _res, next) => { + next(); + }, + } }, ], }, diff --git a/test/core-e2e/src/fixtures/middleware-route/routes/user.js b/test/core-e2e/src/fixtures/middleware-route/routes/user.js index ac18756e6..c9b2ac50c 100644 --- a/test/core-e2e/src/fixtures/middleware-route/routes/user.js +++ b/test/core-e2e/src/fixtures/middleware-route/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -32,17 +34,20 @@ module.exports = [ }, { id: "real", - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } } }, }, diff --git a/test/core-e2e/src/fixtures/middleware-route/routes/users.js b/test/core-e2e/src/fixtures/middleware-route/routes/users.js index 6c1d2aecd..f406dac80 100644 --- a/test/core-e2e/src/fixtures/middleware-route/routes/users.js +++ b/test/core-e2e/src/fixtures/middleware-route/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: USERS, @@ -25,6 +26,7 @@ module.exports = [ }, { id: "error", + handler: "json", response: { status: 403, body: { diff --git a/test/core-e2e/src/fixtures/multi-methods/routes/user.js b/test/core-e2e/src/fixtures/multi-methods/routes/user.js index 5000a41fc..e9420db21 100644 --- a/test/core-e2e/src/fixtures/multi-methods/routes/user.js +++ b/test/core-e2e/src/fixtures/multi-methods/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], diff --git a/test/core-e2e/src/fixtures/multi-methods/routes/users.js b/test/core-e2e/src/fixtures/multi-methods/routes/users.js index 6c1d2aecd..f406dac80 100644 --- a/test/core-e2e/src/fixtures/multi-methods/routes/users.js +++ b/test/core-e2e/src/fixtures/multi-methods/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: USERS, @@ -25,6 +26,7 @@ module.exports = [ }, { id: "error", + handler: "json", response: { status: 403, body: { diff --git a/test/core-e2e/src/fixtures/typescript/routes/books.json b/test/core-e2e/src/fixtures/typescript/routes/books.json index b83fa8431..a202a6003 100644 --- a/test/core-e2e/src/fixtures/typescript/routes/books.json +++ b/test/core-e2e/src/fixtures/typescript/routes/books.json @@ -6,6 +6,7 @@ "variants": [ { "id": "success", + "handler": "json", "response": { "status": 200, "body": [ diff --git a/test/core-e2e/src/fixtures/typescript/routes/middlewares.ts b/test/core-e2e/src/fixtures/typescript/routes/middlewares.ts index f3adec168..076834986 100644 --- a/test/core-e2e/src/fixtures/typescript/routes/middlewares.ts +++ b/test/core-e2e/src/fixtures/typescript/routes/middlewares.ts @@ -2,9 +2,9 @@ // For a detailed explanation about using middlewares, visit: // https://mocks-server.org/docs/guides-using-middlewares -function middleware(req: Object, res: Object, next: () => void, mocksServer: Object) : void { +function middleware(req: Object, res: Object, next: () => void, core: Object) : void { res.set("x-mocks-server-example", "custom-header-typescript"); - mocksServer.tracer.info( + core.logger.info( "Custom header added by add-headers:enabled route variant middleware using TypeScript" ); next(); @@ -18,11 +18,17 @@ const middlewares = [ variants: [ { id: "enabled", - response: middleware, + handler: "middleware", + response: { + middleware + }, }, { id: "disabled", - response: (req, res, next) => next(), + handler: "middleware", + response: { + middleware: (req, res, next) => next(), + } }, ], }, diff --git a/test/core-e2e/src/fixtures/typescript/routes/users.ts b/test/core-e2e/src/fixtures/typescript/routes/users.ts index 8957a9edd..39d718027 100644 --- a/test/core-e2e/src/fixtures/typescript/routes/users.ts +++ b/test/core-e2e/src/fixtures/typescript/routes/users.ts @@ -13,6 +13,7 @@ const routes = [ variants: [ { id: "success", // id of the variant + handler: "json", response: { status: 200, // status to send body: USERS, // body to send @@ -20,6 +21,7 @@ const routes = [ }, { id: "error", // id of the variant + handler: "json", response: { status: 400, // status to send body: { @@ -37,6 +39,7 @@ const routes = [ variants: [ { id: "success", // id of the variant + handler: "json", response: { status: 200, // status to send body: USERS[0], // body to send @@ -44,17 +47,20 @@ const routes = [ }, { id: "real", // id of the variant - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } } }, }, diff --git a/test/core-e2e/src/fixtures/validation-not-array/routes/routes-valid.js b/test/core-e2e/src/fixtures/validation-not-array/routes/routes-valid.js index 54377a26b..2032dedc0 100644 --- a/test/core-e2e/src/fixtures/validation-not-array/routes/routes-valid.js +++ b/test/core-e2e/src/fixtures/validation-not-array/routes/routes-valid.js @@ -6,6 +6,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: {}, diff --git a/test/core-e2e/src/fixtures/validations/routes/users.js b/test/core-e2e/src/fixtures/validations/routes/users.js index 0d11de601..250c33343 100644 --- a/test/core-e2e/src/fixtures/validations/routes/users.js +++ b/test/core-e2e/src/fixtures/validations/routes/users.js @@ -27,6 +27,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -34,6 +35,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -48,6 +50,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -55,6 +58,7 @@ module.exports = [ }, { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -62,6 +66,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: null, }, ], @@ -73,6 +78,7 @@ module.exports = [ variants: [ { id: "2", + handler: "json", response: { status: 200, body: USERS[1], diff --git a/test/core-e2e/src/fixtures/web-tutorial-modified/routes/user.js b/test/core-e2e/src/fixtures/web-tutorial-modified/routes/user.js index ac18756e6..c9b2ac50c 100644 --- a/test/core-e2e/src/fixtures/web-tutorial-modified/routes/user.js +++ b/test/core-e2e/src/fixtures/web-tutorial-modified/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -32,17 +34,20 @@ module.exports = [ }, { id: "real", - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } } }, }, diff --git a/test/core-e2e/src/fixtures/web-tutorial-modified/routes/users.js b/test/core-e2e/src/fixtures/web-tutorial-modified/routes/users.js index db3f6a9b5..6275e2c85 100644 --- a/test/core-e2e/src/fixtures/web-tutorial-modified/routes/users.js +++ b/test/core-e2e/src/fixtures/web-tutorial-modified/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { headers: { "x-custom-header": "foo-header", @@ -29,6 +30,7 @@ module.exports = [ }, { id: "error", + handler: "json", response: { status: 403, body: { @@ -45,6 +47,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: [ diff --git a/test/core-e2e/src/fixtures/web-tutorial/routes/user.js b/test/core-e2e/src/fixtures/web-tutorial/routes/user.js index b2f97d438..11830e672 100644 --- a/test/core-e2e/src/fixtures/web-tutorial/routes/user.js +++ b/test/core-e2e/src/fixtures/web-tutorial/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -32,17 +34,20 @@ module.exports = [ }, { id: "real", - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } } }, }, diff --git a/test/core-e2e/src/fixtures/web-tutorial/routes/users.js b/test/core-e2e/src/fixtures/web-tutorial/routes/users.js index 6c1d2aecd..f406dac80 100644 --- a/test/core-e2e/src/fixtures/web-tutorial/routes/users.js +++ b/test/core-e2e/src/fixtures/web-tutorial/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: USERS, @@ -25,6 +26,7 @@ module.exports = [ }, { id: "error", + handler: "json", response: { status: 403, body: { diff --git a/test/core-e2e/src/multi-methods.spec.js b/test/core-e2e/src/multi-methods.spec.js index e16238501..b7556c050 100644 --- a/test/core-e2e/src/multi-methods.spec.js +++ b/test/core-e2e/src/multi-methods.spec.js @@ -85,7 +85,7 @@ describe("when method is defined as array", () => { { id: "get-user:1", routeId: "get-user", - handler: "default", + handler: "json", response: { body: { id: 1, @@ -98,7 +98,7 @@ describe("when method is defined as array", () => { { id: "get-user:2", routeId: "get-user", - handler: "default", + handler: "json", response: { body: { id: 2, @@ -111,7 +111,7 @@ describe("when method is defined as array", () => { { id: "get-users:success", routeId: "get-users", - handler: "default", + handler: "json", response: { body: [ { @@ -130,7 +130,7 @@ describe("when method is defined as array", () => { { id: "get-users:error", routeId: "get-users", - handler: "default", + handler: "json", response: { body: { message: "Bad data", diff --git a/test/core-e2e/src/plain-mocks-and-routes.spec.js b/test/core-e2e/src/plain-mocks-and-routes.spec.js index ed4d2a518..14f32aad1 100644 --- a/test/core-e2e/src/plain-mocks-and-routes.spec.js +++ b/test/core-e2e/src/plain-mocks-and-routes.spec.js @@ -85,7 +85,7 @@ describe("mocks and routes", () => { { id: "get-user:1", routeId: "get-user", - handler: "default", + handler: "json", response: { body: { id: 1, @@ -98,7 +98,7 @@ describe("mocks and routes", () => { { id: "get-user:2", routeId: "get-user", - handler: "default", + handler: "json", response: { body: { id: 2, @@ -111,14 +111,14 @@ describe("mocks and routes", () => { { id: "get-user:real", routeId: "get-user", - handler: "default", + handler: "middleware", response: null, delay: null, }, { id: "get-users:success", routeId: "get-users", - handler: "default", + handler: "json", response: { body: [ { @@ -137,7 +137,7 @@ describe("mocks and routes", () => { { id: "get-users:error", routeId: "get-users", - handler: "default", + handler: "json", response: { body: { message: "Bad data", diff --git a/test/core-e2e/src/route-handler.spec.js b/test/core-e2e/src/route-handler.spec.js index 8952e2b8e..4c21c7361 100644 --- a/test/core-e2e/src/route-handler.spec.js +++ b/test/core-e2e/src/route-handler.spec.js @@ -29,7 +29,7 @@ describe("when adding route handlers", () => { await startExistingCore(core, fixturesFolder("custom-routes-handler"), { mocks: { selected: "custom-users" }, }); - core.tracer.set("debug", "store"); + core.logger.setLevel("debug", { transport: "store" }); }); afterAll(async () => { @@ -46,13 +46,7 @@ describe("when adding route handlers", () => { }); it("custom handler should have traced", async () => { - expect( - findTrace( - 'Responding with custom route handler to route variant "get-users:custom-success"', - core.tracer.store - ) - ).toBeDefined(); - expect(findTrace("Custom request GET =>", core.tracer.store)).toBeDefined(); + expect(findTrace("Custom request GET => /api/users", core.logger.globalStore)).toBeDefined(); }); }); }); diff --git a/test/core-e2e/src/validations.spec.js b/test/core-e2e/src/validations.spec.js index 511d29ceb..eb6ec42df 100644 --- a/test/core-e2e/src/validations.spec.js +++ b/test/core-e2e/src/validations.spec.js @@ -76,7 +76,7 @@ describe("mocks and routes validations", () => { findAlert("mocks:loadRoutes:get-user-variant-invalid:variants:2:validation", core.alerts) .message ).toEqual( - "Variant with id '2' in route with id 'get-user-variant-invalid' is invalid: /response: type must be object. /response: instanceof must pass \"instanceof\" keyword validation. /response: oneOf must match exactly one schema in oneOf" + "Variant with id '2' in route with id 'get-user-variant-invalid' is invalid: Invalid 'response' property:: type must be object" ); }); diff --git a/test/plugin-inquirer-cli-e2e/src/main/interactive-cli.spec.js b/test/plugin-inquirer-cli-e2e/src/main/interactive-cli.spec.js index aca7b5b8a..d650190bc 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/interactive-cli.spec.js +++ b/test/plugin-inquirer-cli-e2e/src/main/interactive-cli.spec.js @@ -118,7 +118,7 @@ describe("interactive CLI", () => { await wait(1000); expect(mocks.currentScreen).toEqual(expect.stringContaining("Displaying logs")); expect(mocks.currentScreen).toEqual( - expect.stringContaining("[verbose][server] Request received") + expect.stringContaining("[verbose][mocks:routes:get-users:success] Sending response") ); await mocks.pressEnter(); }); diff --git a/workspace.json b/workspace.json index b89069867..ea9935f71 100644 --- a/workspace.json +++ b/workspace.json @@ -18,6 +18,7 @@ "config-e2e": "test/config-e2e", "core": "packages/core", "core-e2e": "test/core-e2e", + "core-e2e-legacy": "test/core-e2e-legacy", "cypress-commands": "packages/cypress-commands", "cypress-commands-e2e": "test/cypress-commands-e2e", "cypress-commands-e2e-vanilla-app": "mocks/cypress-commands-e2e-vanilla-app", From dc802651b0d2fdbee7e8f72e64a00fc1fdfd1d47 Mon Sep 17 00:00:00 2001 From: javierbrea Date: Mon, 4 Jul 2022 13:31:54 +0200 Subject: [PATCH 02/18] feat: Add proxy-v4 routes handler --- .github/workflows/build.yml | 1 + packages/plugin-proxy/CHANGELOG.md | 5 ++ packages/plugin-proxy/README.md | 53 ++++++++++++++- packages/plugin-proxy/package.json | 2 +- packages/plugin-proxy/src/Plugin.js | 2 + .../plugin-proxy/src/ProxyRoutesHandlerV4.js | 49 ++++++++++++++ .../test/fixtures/delay-legacy/mocks.js | 6 ++ .../fixtures/delay-legacy/routes/proxy.js | 20 ++++++ .../test/fixtures/delay/routes/proxy.js | 13 ++-- .../fixtures/docs-example-legacy/mocks.js | 6 ++ .../docs-example-legacy/routes/proxy.js | 19 ++++++ .../fixtures/docs-example/routes/proxy.js | 13 ++-- .../test/fixtures/filters-legacy/mocks.js | 6 ++ .../fixtures/filters-legacy/routes/proxy.js | 23 +++++++ .../fixtures/filters-legacy/routes/users.js | 19 ++++++ .../test/fixtures/filters/routes/proxy.js | 17 +++-- .../test/fixtures/filters/routes/users.js | 1 + .../fixtures/host-function-legacy/mocks.js | 6 ++ .../host-function-legacy/routes/proxy.js | 36 +++++++++++ .../fixtures/host-function/routes/proxy.js | 20 +++--- .../fixtures/res-decorator-legacy/mocks.js | 6 ++ .../res-decorator-legacy/routes/proxy.js | 38 +++++++++++ .../fixtures/res-decorator/routes/proxy.js | 31 +++++---- .../test/legacy/docsExample.spec.js | 49 ++++++++++++++ .../plugin-proxy/test/legacy/filters.spec.js | 49 ++++++++++++++ .../test/legacy/hostFunction.spec.js | 64 +++++++++++++++++++ .../test/legacy/optionDelay.spec.js | 60 +++++++++++++++++ .../test/legacy/resDecorator.spec.js | 49 ++++++++++++++ .../test/legacy/variantDelay.spec.js | 56 ++++++++++++++++ 29 files changed, 680 insertions(+), 39 deletions(-) create mode 100644 packages/plugin-proxy/src/ProxyRoutesHandlerV4.js create mode 100644 packages/plugin-proxy/test/fixtures/delay-legacy/mocks.js create mode 100644 packages/plugin-proxy/test/fixtures/delay-legacy/routes/proxy.js create mode 100644 packages/plugin-proxy/test/fixtures/docs-example-legacy/mocks.js create mode 100644 packages/plugin-proxy/test/fixtures/docs-example-legacy/routes/proxy.js create mode 100644 packages/plugin-proxy/test/fixtures/filters-legacy/mocks.js create mode 100644 packages/plugin-proxy/test/fixtures/filters-legacy/routes/proxy.js create mode 100644 packages/plugin-proxy/test/fixtures/filters-legacy/routes/users.js create mode 100644 packages/plugin-proxy/test/fixtures/host-function-legacy/mocks.js create mode 100644 packages/plugin-proxy/test/fixtures/host-function-legacy/routes/proxy.js create mode 100644 packages/plugin-proxy/test/fixtures/res-decorator-legacy/mocks.js create mode 100644 packages/plugin-proxy/test/fixtures/res-decorator-legacy/routes/proxy.js create mode 100644 packages/plugin-proxy/test/legacy/docsExample.spec.js create mode 100644 packages/plugin-proxy/test/legacy/filters.spec.js create mode 100644 packages/plugin-proxy/test/legacy/hostFunction.spec.js create mode 100644 packages/plugin-proxy/test/legacy/optionDelay.spec.js create mode 100644 packages/plugin-proxy/test/legacy/resDecorator.spec.js create mode 100644 packages/plugin-proxy/test/legacy/variantDelay.spec.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d664bba72..a5ebba9c3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,6 +6,7 @@ on: branches: - master - release + - feat-variant-formats pull_request: jobs: get-affected: diff --git a/packages/plugin-proxy/CHANGELOG.md b/packages/plugin-proxy/CHANGELOG.md index 51a4c33e8..c6a2b806f 100644 --- a/packages/plugin-proxy/CHANGELOG.md +++ b/packages/plugin-proxy/CHANGELOG.md @@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed ### Removed +## [unreleased] + +### Changed +- feat: Add routes handler "proxy-v4" using new handlers API released on @mocks-server/core@3.5.0. Keep old "legacy" handler for backward compatibility. + ## [2.1.0] ### Changed - feat: Use new custom core API released on v3.2.0 diff --git a/packages/plugin-proxy/README.md b/packages/plugin-proxy/README.md index 173ec3476..534254062 100644 --- a/packages/plugin-proxy/README.md +++ b/packages/plugin-proxy/README.md @@ -17,13 +17,60 @@ Plugin for [Mocks Server][website-url] that provides a [route handler](https://w It uses the [express-http-proxy](https://github.com/villadora/express-http-proxy) package under the hood, and supports all of its options. -## Usage +> Important: From v3.0.0, this plugin includes two route handlers: `proxy` and `proxy-v4`. This was made to allow Mocks Server v3 users to progressively adapt their code to Mocks Server v4 without breaking changes. It is strongly recommended to use the `proxy-v4` handler. In the next major release, backward compatibilty will be removed and the `proxy` handler will be replaced by `proxy-v4`. + +## Usage of `proxy-v4` handler This plugin is included in the [main distribution of the Mocks Server project][main-distribution-url], so you can also read the [official documentation website][website-url]. ### Proxy routes -If you want a [route variant](https://www.mocks-server.org/docs/get-started-routes) to use the `proxy` handler, define its `handler` property as "proxy". Use the `host` property to set the host for the route, and the `options` property to set any of the [express-http-proxy](https://github.com/villadora/express-http-proxy) options. +If you want a [route variant](https://www.mocks-server.org/docs/get-started-routes) to use the `proxy-v4` handler, define its `handler` property as "proxy-v4". Use the `host` property to set the host for the route, and the `options` property to set any of the [express-http-proxy](https://github.com/villadora/express-http-proxy) options. + +```js +module.exports = [ + { + id: "proxy-all", + url: "*", + method: ["GET", "POST", "PATCH", "PUT"], + variants: [ + { + id: "proxy-to-google", + handler: "proxy-v4", // This route variant will use the "proxy" handler from this plugin + response: { + host: "https://www.google.com", // proxy host + options: {}, // Options for express-http-proxy + }, + }, + { + id: "disabled", + handler: "middleware", + response: { + middleware: (req, res, next) => next(), + }, + }, + ], + }, +]; +``` + +### Route variant response + +Here are listed the specific properties of a `proxy-v4` route variant. They must be defined in the `response` property of the variant: + +* __`host`__ _(String|Function)_: The proxy host. Equivalent to the [`express-http-proxy` `host` option](https://github.com/villadora/express-http-proxy#host), so it can also be a function. +* __`options`__ _(Object)_: Object containing any of the [options supported by the `express-http-proxy` package](https://github.com/villadora/express-http-proxy#options). Some of them are: + * __filter__ _(Function)_: [`filter` option](https://github.com/villadora/express-http-proxy#filter-supports-promises) for `express-http-proxy`. + * __userResDecorator__ _(Function)_: [`userResDecorator` option](https://github.com/villadora/express-http-proxy#userresdecorator-was-intercept-supports-promise) for `express-http-proxy`. + * __...__ all other [`express-http-proxy` options](https://github.com/villadora/express-http-proxy#options) are also supported. + +> Tip: Note that the `delay` option is still valid for routes handled by this plugin, so you can use it to simulate that host responses are slow. + +## Usage of `proxy` handler + +### Proxy routes + +If you want a Mocks Server v3 [route variant](https://www.mocks-server.org/docs/get-started-routes) to use the `proxy` handler, define its `handler` property as "proxy". Use the `host` property to set the host for the route, and the `options` property to set any of the [express-http-proxy](https://github.com/villadora/express-http-proxy) options. ```js module.exports = [ @@ -36,7 +83,7 @@ module.exports = [ id: "proxy-to-google", handler: "proxy", // This route variant will use the "proxy" handler from this plugin host: "https://www.google.com", // proxy host - options: {} // Options for express-http-proxy + options: {}, // Options for express-http-proxy }, { id: "disabled", diff --git a/packages/plugin-proxy/package.json b/packages/plugin-proxy/package.json index 078eb5136..ab913bae6 100644 --- a/packages/plugin-proxy/package.json +++ b/packages/plugin-proxy/package.json @@ -33,7 +33,7 @@ "test:unit": "jest --runInBand" }, "peerDependencies": { - "@mocks-server/core": ">=3.2.0 <4.x" + "@mocks-server/core": ">=3.5.0 <4.x" }, "dependencies": { "express-http-proxy": "1.6.3" diff --git a/packages/plugin-proxy/src/Plugin.js b/packages/plugin-proxy/src/Plugin.js index 7ae2bb91b..464c6395d 100644 --- a/packages/plugin-proxy/src/Plugin.js +++ b/packages/plugin-proxy/src/Plugin.js @@ -1,4 +1,5 @@ const ProxyRoutesHandler = require("./ProxyRoutesHandler"); +const ProxyRoutesHandlerV4 = require("./ProxyRoutesHandlerV4"); class Plugin { static get id() { @@ -7,6 +8,7 @@ class Plugin { constructor({ addRoutesHandler }) { addRoutesHandler(ProxyRoutesHandler); + addRoutesHandler(ProxyRoutesHandlerV4); } } diff --git a/packages/plugin-proxy/src/ProxyRoutesHandlerV4.js b/packages/plugin-proxy/src/ProxyRoutesHandlerV4.js new file mode 100644 index 000000000..b710b603d --- /dev/null +++ b/packages/plugin-proxy/src/ProxyRoutesHandlerV4.js @@ -0,0 +1,49 @@ +const httpProxy = require("express-http-proxy"); + +class ProxyRoutesHandler { + static get id() { + return "proxy-v4"; + } + + static get version() { + return "4"; + } + + static get validationSchema() { + return { + type: "object", + properties: { + host: { + oneOf: [ + { + type: "string", + }, + { + instanceof: "Function", + }, + ], + }, + options: { + type: "object", + }, + }, + required: ["host"], + }; + } + + constructor(response, core) { + this._response = response; + this._core = core; + this._host = this._response.host; + this._options = this._response.options; + this.middleware = httpProxy(this._host, this._options); + } + + get plainResponsePreview() { + return { + host: this._host, + }; + } +} + +module.exports = ProxyRoutesHandler; diff --git a/packages/plugin-proxy/test/fixtures/delay-legacy/mocks.js b/packages/plugin-proxy/test/fixtures/delay-legacy/mocks.js new file mode 100644 index 000000000..eefbb4da7 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/delay-legacy/mocks.js @@ -0,0 +1,6 @@ +module.exports = [ + { + id: "base", + routesVariants: ["proxy-all:enabled"], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/delay-legacy/routes/proxy.js b/packages/plugin-proxy/test/fixtures/delay-legacy/routes/proxy.js new file mode 100644 index 000000000..09dfaaa42 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/delay-legacy/routes/proxy.js @@ -0,0 +1,20 @@ +module.exports = [ + { + id: "proxy-all", + url: "*", + method: ["GET", "POST", "PATCH", "PUT"], + variants: [ + { + id: "enabled", + delay: 1000, + handler: "proxy", + host: "http://127.0.0.1:3200", + options: {}, + }, + { + id: "disabled", + response: (_req, _res, next) => next(), + }, + ], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/delay/routes/proxy.js b/packages/plugin-proxy/test/fixtures/delay/routes/proxy.js index 09dfaaa42..b608b995f 100644 --- a/packages/plugin-proxy/test/fixtures/delay/routes/proxy.js +++ b/packages/plugin-proxy/test/fixtures/delay/routes/proxy.js @@ -7,13 +7,18 @@ module.exports = [ { id: "enabled", delay: 1000, - handler: "proxy", - host: "http://127.0.0.1:3200", - options: {}, + handler: "proxy-v4", + response: { + host: "http://127.0.0.1:3200", + options: {}, + }, }, { id: "disabled", - response: (_req, _res, next) => next(), + handler: "middleware", + response: { + middleware: (_req, _res, next) => next(), + }, }, ], }, diff --git a/packages/plugin-proxy/test/fixtures/docs-example-legacy/mocks.js b/packages/plugin-proxy/test/fixtures/docs-example-legacy/mocks.js new file mode 100644 index 000000000..eefbb4da7 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/docs-example-legacy/mocks.js @@ -0,0 +1,6 @@ +module.exports = [ + { + id: "base", + routesVariants: ["proxy-all:enabled"], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/docs-example-legacy/routes/proxy.js b/packages/plugin-proxy/test/fixtures/docs-example-legacy/routes/proxy.js new file mode 100644 index 000000000..c9d66fc6b --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/docs-example-legacy/routes/proxy.js @@ -0,0 +1,19 @@ +module.exports = [ + { + id: "proxy-all", + url: "*", + method: ["GET", "POST", "PATCH", "PUT"], + variants: [ + { + id: "enabled", + handler: "proxy", + host: "http://127.0.0.1:3200", + options: {}, + }, + { + id: "disabled", + response: (_req, _res, next) => next(), + }, + ], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/docs-example/routes/proxy.js b/packages/plugin-proxy/test/fixtures/docs-example/routes/proxy.js index c9d66fc6b..b22d21351 100644 --- a/packages/plugin-proxy/test/fixtures/docs-example/routes/proxy.js +++ b/packages/plugin-proxy/test/fixtures/docs-example/routes/proxy.js @@ -6,13 +6,18 @@ module.exports = [ variants: [ { id: "enabled", - handler: "proxy", - host: "http://127.0.0.1:3200", - options: {}, + handler: "proxy-v4", + response: { + host: "http://127.0.0.1:3200", + options: {}, + }, }, { id: "disabled", - response: (_req, _res, next) => next(), + handler: "middleware", + response: { + middleware: (_req, _res, next) => next(), + }, }, ], }, diff --git a/packages/plugin-proxy/test/fixtures/filters-legacy/mocks.js b/packages/plugin-proxy/test/fixtures/filters-legacy/mocks.js new file mode 100644 index 000000000..1cded4d47 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/filters-legacy/mocks.js @@ -0,0 +1,6 @@ +module.exports = [ + { + id: "base", + routesVariants: ["proxy-all:enabled", "user-2:default"], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/filters-legacy/routes/proxy.js b/packages/plugin-proxy/test/fixtures/filters-legacy/routes/proxy.js new file mode 100644 index 000000000..92ee9f22b --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/filters-legacy/routes/proxy.js @@ -0,0 +1,23 @@ +module.exports = [ + { + id: "proxy-all", + url: "*", + method: ["GET", "POST", "PATCH", "PUT"], + variants: [ + { + id: "enabled", + handler: "proxy", + host: "http://127.0.0.1:3200", + options: { + filter: (req) => { + return !req.url.includes("users/2"); + }, + }, + }, + { + id: "disabled", + response: (_req, _res, next) => next(), + }, + ], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/filters-legacy/routes/users.js b/packages/plugin-proxy/test/fixtures/filters-legacy/routes/users.js new file mode 100644 index 000000000..e79d39c25 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/filters-legacy/routes/users.js @@ -0,0 +1,19 @@ +module.exports = [ + { + id: "user-2", + url: "/api/users/2", + method: ["GET"], + variants: [ + { + id: "default", + response: { + status: 200, + body: { + id: 2, + name: "Mocked User", + }, + }, + }, + ], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/filters/routes/proxy.js b/packages/plugin-proxy/test/fixtures/filters/routes/proxy.js index 92ee9f22b..c499b6a27 100644 --- a/packages/plugin-proxy/test/fixtures/filters/routes/proxy.js +++ b/packages/plugin-proxy/test/fixtures/filters/routes/proxy.js @@ -6,17 +6,22 @@ module.exports = [ variants: [ { id: "enabled", - handler: "proxy", - host: "http://127.0.0.1:3200", - options: { - filter: (req) => { - return !req.url.includes("users/2"); + handler: "proxy-v4", + response: { + host: "http://127.0.0.1:3200", + options: { + filter: (req) => { + return !req.url.includes("users/2"); + }, }, }, }, { id: "disabled", - response: (_req, _res, next) => next(), + handler: "middleware", + response: { + middleware: (_req, _res, next) => next(), + }, }, ], }, diff --git a/packages/plugin-proxy/test/fixtures/filters/routes/users.js b/packages/plugin-proxy/test/fixtures/filters/routes/users.js index e79d39c25..997444e69 100644 --- a/packages/plugin-proxy/test/fixtures/filters/routes/users.js +++ b/packages/plugin-proxy/test/fixtures/filters/routes/users.js @@ -6,6 +6,7 @@ module.exports = [ variants: [ { id: "default", + handler: "json", response: { status: 200, body: { diff --git a/packages/plugin-proxy/test/fixtures/host-function-legacy/mocks.js b/packages/plugin-proxy/test/fixtures/host-function-legacy/mocks.js new file mode 100644 index 000000000..0606f2cc1 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/host-function-legacy/mocks.js @@ -0,0 +1,6 @@ +module.exports = [ + { + id: "base", + routesVariants: ["proxy-user:enabled", "proxy-all:enabled"], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/host-function-legacy/routes/proxy.js b/packages/plugin-proxy/test/fixtures/host-function-legacy/routes/proxy.js new file mode 100644 index 000000000..0b00af479 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/host-function-legacy/routes/proxy.js @@ -0,0 +1,36 @@ +module.exports = [ + { + id: "proxy-all", + url: "*", + method: ["GET", "POST", "PATCH", "PUT"], + variants: [ + { + id: "enabled", + handler: "proxy", + host: () => { + return "http://127.0.0.1:3200"; + }, + options: {}, + }, + { + id: "disabled", + response: (_req, _res, next) => next(), + }, + ], + }, + { + id: "proxy-user", + url: "/api/users/:id", + method: ["GET", "POST", "PATCH", "PUT"], + variants: [ + { + id: "enabled", + handler: "proxy", + host: () => { + return "http://127.0.0.1:3300"; + }, + options: {}, + }, + ], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/host-function/routes/proxy.js b/packages/plugin-proxy/test/fixtures/host-function/routes/proxy.js index 0b00af479..c20b5b11c 100644 --- a/packages/plugin-proxy/test/fixtures/host-function/routes/proxy.js +++ b/packages/plugin-proxy/test/fixtures/host-function/routes/proxy.js @@ -6,11 +6,13 @@ module.exports = [ variants: [ { id: "enabled", - handler: "proxy", - host: () => { - return "http://127.0.0.1:3200"; + handler: "proxy-v4", + response: { + host: () => { + return "http://127.0.0.1:3200"; + }, + options: {}, }, - options: {}, }, { id: "disabled", @@ -25,11 +27,13 @@ module.exports = [ variants: [ { id: "enabled", - handler: "proxy", - host: () => { - return "http://127.0.0.1:3300"; + handler: "proxy-v4", + response: { + host: () => { + return "http://127.0.0.1:3300"; + }, + options: {}, }, - options: {}, }, ], }, diff --git a/packages/plugin-proxy/test/fixtures/res-decorator-legacy/mocks.js b/packages/plugin-proxy/test/fixtures/res-decorator-legacy/mocks.js new file mode 100644 index 000000000..eefbb4da7 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/res-decorator-legacy/mocks.js @@ -0,0 +1,6 @@ +module.exports = [ + { + id: "base", + routesVariants: ["proxy-all:enabled"], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/res-decorator-legacy/routes/proxy.js b/packages/plugin-proxy/test/fixtures/res-decorator-legacy/routes/proxy.js new file mode 100644 index 000000000..e7f8c45d7 --- /dev/null +++ b/packages/plugin-proxy/test/fixtures/res-decorator-legacy/routes/proxy.js @@ -0,0 +1,38 @@ +function modifyUser(user) { + return { + ...user, + name: `Modified ${user.name}`, + }; +} + +module.exports = [ + { + id: "proxy-all", + url: "*", + method: ["GET", "POST", "PATCH", "PUT"], + variants: [ + { + id: "enabled", + handler: "proxy", + host: "http://127.0.0.1:3200", + options: { + userResDecorator: function (_proxyRes, proxyResData) { + const data = JSON.parse(proxyResData.toString("utf8")); + let newData; + if (Array.isArray(data)) { + newData = data.map(modifyUser); + } else { + newData = modifyUser(data); + } + + return JSON.stringify(newData); + }, + }, + }, + { + id: "disabled", + response: (_req, _res, next) => next(), + }, + ], + }, +]; diff --git a/packages/plugin-proxy/test/fixtures/res-decorator/routes/proxy.js b/packages/plugin-proxy/test/fixtures/res-decorator/routes/proxy.js index e7f8c45d7..65f5643e6 100644 --- a/packages/plugin-proxy/test/fixtures/res-decorator/routes/proxy.js +++ b/packages/plugin-proxy/test/fixtures/res-decorator/routes/proxy.js @@ -13,25 +13,30 @@ module.exports = [ variants: [ { id: "enabled", - handler: "proxy", - host: "http://127.0.0.1:3200", - options: { - userResDecorator: function (_proxyRes, proxyResData) { - const data = JSON.parse(proxyResData.toString("utf8")); - let newData; - if (Array.isArray(data)) { - newData = data.map(modifyUser); - } else { - newData = modifyUser(data); - } + handler: "proxy-v4", + response: { + host: "http://127.0.0.1:3200", + options: { + userResDecorator: function (_proxyRes, proxyResData) { + const data = JSON.parse(proxyResData.toString("utf8")); + let newData; + if (Array.isArray(data)) { + newData = data.map(modifyUser); + } else { + newData = modifyUser(data); + } - return JSON.stringify(newData); + return JSON.stringify(newData); + }, }, }, }, { id: "disabled", - response: (_req, _res, next) => next(), + handler: "middleware", + response: { + middleware: (_req, _res, next) => next(), + }, }, ], }, diff --git a/packages/plugin-proxy/test/legacy/docsExample.spec.js b/packages/plugin-proxy/test/legacy/docsExample.spec.js new file mode 100644 index 000000000..c31b073fb --- /dev/null +++ b/packages/plugin-proxy/test/legacy/docsExample.spec.js @@ -0,0 +1,49 @@ +const { + startServer, + fetch, + waitForServer, + waitForHost, + startHost, +} = require("../support/helpers"); + +describe("Docs example", () => { + let server, host; + + beforeAll(async () => { + host = await startHost(); + server = await startServer("docs-example-legacy"); + await waitForHost(); + await waitForServer(); + }); + + afterAll(async () => { + await host.stop(); + await server.stop(); + }); + + describe("get /users", () => { + it("should return users from host", async () => { + const response = await fetch("/api/users"); + expect(response.body).toEqual([ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, + ]); + }); + }); + + describe("get /users/2", () => { + it("should return second user from host", async () => { + const response = await fetch("/api/users/2"); + expect(response.body).toEqual({ + id: 2, + name: "Jane Doe", + }); + }); + }); +}); diff --git a/packages/plugin-proxy/test/legacy/filters.spec.js b/packages/plugin-proxy/test/legacy/filters.spec.js new file mode 100644 index 000000000..7c87d5ea1 --- /dev/null +++ b/packages/plugin-proxy/test/legacy/filters.spec.js @@ -0,0 +1,49 @@ +const { + startServer, + fetch, + waitForServer, + waitForHost, + startHost, +} = require("../support/helpers"); + +describe("when using filter option", () => { + let server, host; + + beforeAll(async () => { + host = await startHost(); + server = await startServer("filters-legacy"); + await waitForHost(); + await waitForServer(); + }); + + afterAll(async () => { + await host.stop(); + await server.stop(); + }); + + describe("get /users", () => { + it("should return users from host", async () => { + const response = await fetch("/api/users"); + expect(response.body).toEqual([ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, + ]); + }); + }); + + describe("get /users/2", () => { + it("should return second user from mock", async () => { + const response = await fetch("/api/users/2"); + expect(response.body).toEqual({ + id: 2, + name: "Mocked User", + }); + }); + }); +}); diff --git a/packages/plugin-proxy/test/legacy/hostFunction.spec.js b/packages/plugin-proxy/test/legacy/hostFunction.spec.js new file mode 100644 index 000000000..0e21a8916 --- /dev/null +++ b/packages/plugin-proxy/test/legacy/hostFunction.spec.js @@ -0,0 +1,64 @@ +const { + startServer, + fetch, + waitForServer, + waitForHost, + waitForHost2, + startHost, + startHost2, +} = require("../support/helpers"); + +describe("when defining host option as a function", () => { + let server, host, host2; + + beforeAll(async () => { + host = await startHost(); + host2 = await startHost2(); + server = await startServer("host-function-legacy"); + await waitForHost(); + await waitForHost2(); + await waitForServer(); + }); + + afterAll(async () => { + await host.stop(); + await host2.stop(); + await server.stop(); + }); + + describe("get /users", () => { + it("should return users from host", async () => { + const response = await fetch("/api/users"); + expect(response.body).toEqual([ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, + ]); + }); + }); + + describe("get /users/1", () => { + it("should return user from second host", async () => { + const response = await fetch("/api/users/1"); + expect(response.body).toEqual({ + id: 1, + name: "John Doe 2", + }); + }); + }); + + describe("get /users/2", () => { + it("should return user from second host", async () => { + const response = await fetch("/api/users/2"); + expect(response.body).toEqual({ + id: 2, + name: "Jane Doe 2", + }); + }); + }); +}); diff --git a/packages/plugin-proxy/test/legacy/optionDelay.spec.js b/packages/plugin-proxy/test/legacy/optionDelay.spec.js new file mode 100644 index 000000000..9befb14c6 --- /dev/null +++ b/packages/plugin-proxy/test/legacy/optionDelay.spec.js @@ -0,0 +1,60 @@ +const { + startServer, + fetch, + TimeCounter, + waitForServer, + waitForHost, + startHost, +} = require("../support/helpers"); + +describe("when using delay option in server", () => { + let server, host; + + beforeAll(async () => { + host = await startHost(); + server = await startServer("docs-example-legacy", { + mocks: { + delay: 1000, + }, + }); + await waitForHost(); + await waitForServer(); + }); + + afterAll(async () => { + await host.stop(); + await server.stop(); + }); + + describe("get /users", () => { + it("should return users from host after 1 second", async () => { + const timeCounter = new TimeCounter(); + const response = await fetch("/api/users"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(999); + expect(response.body).toEqual([ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, + ]); + }); + }); + + describe("get /users/2", () => { + it("should return second user from host after 1 second", async () => { + const timeCounter = new TimeCounter(); + const response = await fetch("/api/users/2"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(999); + expect(response.body).toEqual({ + id: 2, + name: "Jane Doe", + }); + }); + }); +}); diff --git a/packages/plugin-proxy/test/legacy/resDecorator.spec.js b/packages/plugin-proxy/test/legacy/resDecorator.spec.js new file mode 100644 index 000000000..b1329950f --- /dev/null +++ b/packages/plugin-proxy/test/legacy/resDecorator.spec.js @@ -0,0 +1,49 @@ +const { + startServer, + fetch, + waitForServer, + waitForHost, + startHost, +} = require("../support/helpers"); + +describe("when using filter option", () => { + let server, host; + + beforeAll(async () => { + host = await startHost(); + server = await startServer("res-decorator-legacy"); + await waitForHost(); + await waitForServer(); + }); + + afterAll(async () => { + await host.stop(); + await server.stop(); + }); + + describe("get /users", () => { + it("should return modified users from host", async () => { + const response = await fetch("/api/users"); + expect(response.body).toEqual([ + { + id: 1, + name: "Modified John Doe", + }, + { + id: 2, + name: "Modified Jane Doe", + }, + ]); + }); + }); + + describe("get /users/2", () => { + it("should return second user from host", async () => { + const response = await fetch("/api/users/2"); + expect(response.body).toEqual({ + id: 2, + name: "Modified Jane Doe", + }); + }); + }); +}); diff --git a/packages/plugin-proxy/test/legacy/variantDelay.spec.js b/packages/plugin-proxy/test/legacy/variantDelay.spec.js new file mode 100644 index 000000000..1cc5dca5a --- /dev/null +++ b/packages/plugin-proxy/test/legacy/variantDelay.spec.js @@ -0,0 +1,56 @@ +const { + startServer, + fetch, + TimeCounter, + waitForServer, + waitForHost, + startHost, +} = require("../support/helpers"); + +describe("when using delay option in variant", () => { + let server, host; + + beforeAll(async () => { + host = await startHost(); + server = await startServer("delay-legacy"); + await waitForHost(); + await waitForServer(); + }); + + afterAll(async () => { + await host.stop(); + await server.stop(); + }); + + describe("get /users", () => { + it("should return users from host after 1 second", async () => { + const timeCounter = new TimeCounter(); + const response = await fetch("/api/users"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(999); + expect(response.body).toEqual([ + { + id: 1, + name: "John Doe", + }, + { + id: 2, + name: "Jane Doe", + }, + ]); + }); + }); + + describe("get /users/2", () => { + it("should return second user from host after 1 second", async () => { + const timeCounter = new TimeCounter(); + const response = await fetch("/api/users/2"); + timeCounter.stop(); + expect(timeCounter.total).toBeGreaterThan(999); + expect(response.body).toEqual({ + id: 2, + name: "Jane Doe", + }); + }); + }); +}); From e84cc787807719dbbeccff606610782954926168 Mon Sep 17 00:00:00 2001 From: javierbrea Date: Mon, 4 Jul 2022 13:52:13 +0200 Subject: [PATCH 03/18] feat: Use new handlers format in scaffold --- .../src/scaffold/mocks/routes/middlewares.js | 16 ++++++---- .../core/src/scaffold/mocks/routes/users.js | 30 +++++++++++-------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/core/src/scaffold/mocks/routes/middlewares.js b/packages/core/src/scaffold/mocks/routes/middlewares.js index d5a8bd85e..1bf5e421f 100644 --- a/packages/core/src/scaffold/mocks/routes/middlewares.js +++ b/packages/core/src/scaffold/mocks/routes/middlewares.js @@ -10,15 +10,21 @@ module.exports = [ variants: [ { id: "enabled", - response: (_req, res, next, core) => { - res.set("x-mocks-server-example", "some-value"); - core.logger.info("Custom header added by route variant middleware"); - next(); + handler: "middleware", + response: { + middleware: (_req, res, next, core) => { + res.set("x-mocks-server-example", "some-value"); + core.logger.info("Custom header added by route variant middleware"); + next(); + }, }, }, { id: "disabled", - response: (_req, _res, next) => next(), + handler: "middleware", + response: { + middleware: (_req, _res, next) => next(), + }, }, ], }, diff --git a/packages/core/src/scaffold/mocks/routes/users.js b/packages/core/src/scaffold/mocks/routes/users.js index 473241e3c..00384b7a1 100644 --- a/packages/core/src/scaffold/mocks/routes/users.js +++ b/packages/core/src/scaffold/mocks/routes/users.js @@ -22,6 +22,7 @@ module.exports = [ variants: [ { id: "success", // id of the variant + handler: "json", // variant handler response: { status: 200, // status to send body: USERS, // body to send @@ -29,6 +30,7 @@ module.exports = [ }, { id: "error", // id of the variant + handler: "json", // variant handler response: { status: 400, // status to send body: { @@ -46,6 +48,7 @@ module.exports = [ variants: [ { id: "success", // id of the variant + handler: "json", // variant handler response: { status: 200, // status to send body: USERS[0], // body to send @@ -53,18 +56,21 @@ module.exports = [ }, { id: "real", // id of the variant - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); - } + handler: "middleware", // variant handler + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, }, }, ], From 55c0676c6d229e9c0231c8e37b5493713f8d964d Mon Sep 17 00:00:00 2001 From: javierbrea Date: Mon, 4 Jul 2022 13:53:07 +0200 Subject: [PATCH 04/18] chore: Build branch --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a5ebba9c3..ea9f4bf5a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,7 +6,7 @@ on: branches: - master - release - - feat-variant-formats + - feat-variants-format pull_request: jobs: get-affected: From 5fa0d77960c779767740a349a219a0d262a2805f Mon Sep 17 00:00:00 2001 From: javierbrea Date: Mon, 4 Jul 2022 14:11:01 +0200 Subject: [PATCH 05/18] chore: Update pnpm-lock --- pnpm-lock.yaml | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9ce718168..737859100 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -381,6 +381,16 @@ importers: '@mocks-server/core': link:../../packages/core express: 4.17.3 + test/core-e2e-legacy: + specifiers: + '@mocks-server/cli-runner': workspace:* + '@mocks-server/core': workspace:* + express: 4.17.3 + dependencies: + '@mocks-server/cli-runner': link:../../packages/cli-runner + '@mocks-server/core': link:../../packages/core + express: 4.17.3 + test/cypress-commands-e2e: specifiers: '@mocks-server/main': workspace:* @@ -6923,7 +6933,7 @@ packages: dev: true /depd/1.1.2: - resolution: {integrity: sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=} + resolution: {integrity: sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==} engines: {node: '>= 0.6'} /depd/2.0.0: @@ -6939,7 +6949,7 @@ packages: dev: true /destroy/1.0.4: - resolution: {integrity: sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=} + resolution: {integrity: sha512-3NdhDuEXnfun/z7x9GOElY49LoqVHoGScmOKwmxhsS8N5Y+Z8KyPPDnaSzqWgYt/ji4mqwfTS34Htrk0zPIXVg==} /destroy/1.2.0: resolution: {integrity: sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==} @@ -15220,7 +15230,7 @@ packages: dev: true /statuses/1.5.0: - resolution: {integrity: sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=} + resolution: {integrity: sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==} engines: {node: '>= 0.6'} /statuses/2.0.1: From 70797995963016cb91d1ca7113e612eb0a1de418 Mon Sep 17 00:00:00 2001 From: javierbrea Date: Mon, 4 Jul 2022 14:32:50 +0200 Subject: [PATCH 06/18] feat: Increase winston-array-transport maxEventListeners. Fix E2E tests --- packages/logger/CHANGELOG.md | 5 +++++ packages/logger/package.json | 2 +- packages/logger/src/Logger.ts | 2 +- pnpm-lock.yaml | 11 ++++++++-- .../src/fixtures/proxy/mocks/routes/proxy.js | 13 ++++++++---- test/main-e2e/src/proxy.spec.js | 4 ++-- test/main-e2e/src/scaffold.spec.js | 21 ++++++++++++------- test/main-e2e/src/support/helpers.js | 11 +++++++++- 8 files changed, 51 insertions(+), 18 deletions(-) diff --git a/packages/logger/CHANGELOG.md b/packages/logger/CHANGELOG.md index bd51e85f4..95f4cb25b 100644 --- a/packages/logger/CHANGELOG.md +++ b/packages/logger/CHANGELOG.md @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed ### Removed +## [unreleased] + +### Changed +- feat: Increase winston-array-transport maxEventListeners + ## [1.0.0] - 2022-06-27 ### Added diff --git a/packages/logger/package.json b/packages/logger/package.json index 2aec35ac9..b7c226c72 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -31,7 +31,7 @@ "dependencies": { "chalk": "4.1.1", "winston": "3.7.2", - "winston-array-transport": "1.1.9" + "winston-array-transport": "1.1.10" }, "engines": { "node": ">=14.x" diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index e33e7831d..38f50e6ee 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -101,7 +101,7 @@ function createArrayTransport(store: LogsStore, defaultLevel: Level, storeLimit: array: store, limit: storeLimit, level: defaultLevel, - maxListeners: 100, // Array transport does not support setting limit to 0 + maxListeners: 0, format: winston.format.combine( formatTimestamp, formatStore diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 737859100..103908f49 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -278,11 +278,11 @@ importers: specifiers: chalk: 4.1.1 winston: 3.7.2 - winston-array-transport: 1.1.9 + winston-array-transport: 1.1.10 dependencies: chalk: 4.1.1 winston: 3.7.2 - winston-array-transport: 1.1.9 + winston-array-transport: 1.1.10 packages/main: specifiers: @@ -16614,6 +16614,13 @@ packages: dependencies: string-width: 4.2.3 + /winston-array-transport/1.1.10: + resolution: {integrity: sha512-9VQ4NWDWG9MPGh9qdz7hkRVONwp6td2UihpRQlScFdHRp/XrfcgbuhMGS7ddxRVAB3bhVNnyHIi6+XDSaU1ulQ==} + engines: {node: '>=10'} + dependencies: + winston-transport: 4.5.0 + dev: false + /winston-array-transport/1.1.9: resolution: {integrity: sha512-KBh1dlSuP8N3Whq5XQDO1eC9SsUbsukPLsL2rcJx3qFK2fKiQba2PFxoHPhGn1mvrVxAoW8SM3nbEvADj32c6A==} engines: {node: '>=10'} diff --git a/test/main-e2e/src/fixtures/proxy/mocks/routes/proxy.js b/test/main-e2e/src/fixtures/proxy/mocks/routes/proxy.js index c9d66fc6b..b22d21351 100644 --- a/test/main-e2e/src/fixtures/proxy/mocks/routes/proxy.js +++ b/test/main-e2e/src/fixtures/proxy/mocks/routes/proxy.js @@ -6,13 +6,18 @@ module.exports = [ variants: [ { id: "enabled", - handler: "proxy", - host: "http://127.0.0.1:3200", - options: {}, + handler: "proxy-v4", + response: { + host: "http://127.0.0.1:3200", + options: {}, + }, }, { id: "disabled", - response: (_req, _res, next) => next(), + handler: "middleware", + response: { + middleware: (_req, _res, next) => next(), + }, }, ], }, diff --git a/test/main-e2e/src/proxy.spec.js b/test/main-e2e/src/proxy.spec.js index dfe2306e5..3bc03b2c1 100644 --- a/test/main-e2e/src/proxy.spec.js +++ b/test/main-e2e/src/proxy.spec.js @@ -103,7 +103,7 @@ describe("scaffold", () => { { id: "proxy-all:enabled", routeId: "proxy-all", - handler: "proxy", + handler: "proxy-v4", response: { host: "http://127.0.0.1:3200", }, @@ -112,7 +112,7 @@ describe("scaffold", () => { { id: "proxy-all:disabled", routeId: "proxy-all", - handler: "default", + handler: "middleware", response: null, delay: null, }, diff --git a/test/main-e2e/src/scaffold.spec.js b/test/main-e2e/src/scaffold.spec.js index 52392c739..b11769da4 100644 --- a/test/main-e2e/src/scaffold.spec.js +++ b/test/main-e2e/src/scaffold.spec.js @@ -8,13 +8,20 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServerAndCli, wait } = require("./support/helpers"); +const { + mocksRunner, + fetch, + waitForServerAndCli, + wait, + cleanScaffold, +} = require("./support/helpers"); describe("scaffold", () => { jest.setTimeout(15000); let mocks; beforeAll(async () => { + await cleanScaffold(); mocks = mocksRunner(); await waitForServerAndCli(); }); @@ -113,21 +120,21 @@ describe("scaffold", () => { { id: "add-headers:enabled", routeId: "add-headers", - handler: "default", + handler: "middleware", response: null, delay: null, }, { id: "add-headers:disabled", routeId: "add-headers", - handler: "default", + handler: "middleware", response: null, delay: null, }, { id: "get-users:success", routeId: "get-users", - handler: "default", + handler: "json", response: { body: [ { id: 1, name: "John Doe" }, @@ -140,21 +147,21 @@ describe("scaffold", () => { { id: "get-users:error", routeId: "get-users", - handler: "default", + handler: "json", response: { body: { message: "Error" }, status: 400 }, delay: null, }, { id: "get-user:success", routeId: "get-user", - handler: "default", + handler: "json", response: { body: { id: 1, name: "John Doe" }, status: 200 }, delay: null, }, { id: "get-user:real", routeId: "get-user", - handler: "default", + handler: "middleware", response: null, delay: null, }, diff --git a/test/main-e2e/src/support/helpers.js b/test/main-e2e/src/support/helpers.js index f57474441..a24e01d04 100644 --- a/test/main-e2e/src/support/helpers.js +++ b/test/main-e2e/src/support/helpers.js @@ -9,6 +9,7 @@ Unless required by applicable law or agreed to in writing, software distributed */ const path = require("path"); +const fsExtra = require("fs-extra"); const crossFetch = require("cross-fetch"); const waitOn = require("wait-on"); @@ -32,8 +33,15 @@ const fixturesFolder = (folderName) => { return path.resolve(baseFixturesFolder, folderName); }; +const scaffoldFolder = fixturesFolder("scaffold"); + const defaultMocksRunnerOptions = { - cwd: fixturesFolder("scaffold"), + cwd: scaffoldFolder, +}; + +const cleanScaffold = async () => { + await fsExtra.remove(path.resolve(scaffoldFolder, "mocks")); + await fsExtra.remove(path.resolve(scaffoldFolder, "mocks.config.js")); }; const serverUrl = (port) => { @@ -122,4 +130,5 @@ module.exports = { waitForServer, waitForServerAndCli, fixturesFolder, + cleanScaffold, }; From 82665ace6a5f35899dfd440e557fb9cd8bb773ce Mon Sep 17 00:00:00 2001 From: javierbrea Date: Mon, 4 Jul 2022 14:46:05 +0200 Subject: [PATCH 07/18] feat: Deprecate default and proxy handlers. Add an alert whenever they are used --- packages/admin-api-client/test/index.spec.js | 6 ++++-- packages/core/CHANGELOG.md | 1 + packages/core/src/mocks/helpers.js | 8 ++++++++ packages/core/src/routes-handlers/handlers/Default.js | 4 ++++ packages/plugin-admin-api/test/alerts-api.spec.js | 6 ++++-- packages/plugin-proxy/src/ProxyRoutesHandler.js | 4 ++++ 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/admin-api-client/test/index.spec.js b/packages/admin-api-client/test/index.spec.js index d0409b421..6e321be37 100644 --- a/packages/admin-api-client/test/index.spec.js +++ b/packages/admin-api-client/test/index.spec.js @@ -29,7 +29,8 @@ describe("react-admin-client methods used with node", () => { describe("when there are alerts", () => { it("should return alerts", async () => { const alerts = await readAlerts(); - expect(alerts.length).toEqual(1); + // one alert is added due to the usage of default handler + expect(alerts.length).toEqual(2); }); it("should return alert about mock not defined", async () => { @@ -86,7 +87,8 @@ describe("react-admin-client methods used with node", () => { }); await wait(2000); const alerts = await readAlerts(); - expect(alerts.length).toEqual(0); + // one alert is added due to the usage of default handler + expect(alerts.length).toEqual(1); }); }); }); diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index b7ea98983..0d511f0f7 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - feat(#335): Pass only response property from variants to route variant handlers having the "version" property defined as "4". If it has another value, pass the whole variant object (for backward compatibility) - feat(#336): Add "Json" and "Middleware" variant handlers. - feat: Support defining the response preview in the handlers "preview" property. Keep also "plainResponsePreview" for backward compatibility. +- feat: Support "deprecated" property in route handlers. Add an alert whenever any route variant uses a deprecated handler ### Changed - feat: Log requests in the middleware added by the Mock class, so it has not to be logged in every different handler. diff --git a/packages/core/src/mocks/helpers.js b/packages/core/src/mocks/helpers.js index dbf9780bc..54c839a3b 100644 --- a/packages/core/src/mocks/helpers.js +++ b/packages/core/src/mocks/helpers.js @@ -203,6 +203,7 @@ function getVariantHandler({ const variantNamespace = variantId || getVariantId(route.id, variantIndex); const routeVariantLogger = loggerRoutes.namespace(variantNamespace); const routeVariantAlerts = alertsRoutes.collection(variantNamespace); + const handlersAlerts = alertsRoutes.collection("handlers"); const routeVariantCustomCore = new CustomCore({ core, logger: routeVariantLogger, @@ -219,6 +220,12 @@ function getVariantHandler({ try { const variantArgument = Handler.version === "4" ? variant.response : variant; + if (Handler.deprecated) { + handlersAlerts.set( + Handler.id, + `Handler '${Handler.id}' is deprecated and will be removed in next major version. Consider using another handler.` + ); + } routeHandler = new Handler( { ...variantArgument, @@ -253,6 +260,7 @@ function getRouteVariants({ }) { let routeIds = []; alerts.clean(); + alertsRoutes.clean(); return compact( flatten( routesDefinitions.map((route, index) => { diff --git a/packages/core/src/routes-handlers/handlers/Default.js b/packages/core/src/routes-handlers/handlers/Default.js index 6775fa0d3..f3b663804 100644 --- a/packages/core/src/routes-handlers/handlers/Default.js +++ b/packages/core/src/routes-handlers/handlers/Default.js @@ -17,6 +17,10 @@ class DefaultRoutesHandler { return "default"; } + static get deprecated() { + return true; + } + static get validationSchema() { return { type: "object", diff --git a/packages/plugin-admin-api/test/alerts-api.spec.js b/packages/plugin-admin-api/test/alerts-api.spec.js index 39ee863c9..c5d81ba84 100644 --- a/packages/plugin-admin-api/test/alerts-api.spec.js +++ b/packages/plugin-admin-api/test/alerts-api.spec.js @@ -28,7 +28,8 @@ describe("alerts api", () => { describe("when started", () => { it("should return mock not found alert", async () => { const response = await fetch("/admin/alerts"); - expect(response.body.length).toEqual(1); + // one alert is caused by deprecated handler + expect(response.body.length).toEqual(2); }); it("should return specific alert when requested by id", async () => { @@ -65,7 +66,8 @@ describe("alerts api", () => { it("should return no alerts", async () => { const response = await fetch("/admin/alerts"); - expect(response.body.length).toEqual(0); + // one alert is caused by deprecated handler + expect(response.body.length).toEqual(1); }); }); diff --git a/packages/plugin-proxy/src/ProxyRoutesHandler.js b/packages/plugin-proxy/src/ProxyRoutesHandler.js index 5426829d6..8a923aaff 100644 --- a/packages/plugin-proxy/src/ProxyRoutesHandler.js +++ b/packages/plugin-proxy/src/ProxyRoutesHandler.js @@ -5,6 +5,10 @@ class ProxyRoutesHandler { return "proxy"; } + static get deprecated() { + return true; + } + static get validationSchema() { return { type: "object", From 54035d428fafe7fb71a58bc11106de41a16ac690 Mon Sep 17 00:00:00 2001 From: javierbrea Date: Mon, 4 Jul 2022 14:52:10 +0200 Subject: [PATCH 08/18] test: do not use deprecated handlers in mocks --- .../mocks/routes/users.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mocks/admin-api-client-data-provider-e2e-mocks/mocks/routes/users.js b/mocks/admin-api-client-data-provider-e2e-mocks/mocks/routes/users.js index c3e067414..78c3ba7f2 100644 --- a/mocks/admin-api-client-data-provider-e2e-mocks/mocks/routes/users.js +++ b/mocks/admin-api-client-data-provider-e2e-mocks/mocks/routes/users.js @@ -6,6 +6,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: [{ email: "foo@foo.com" }], @@ -13,6 +14,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: [{ email: "foo2@foo2.com" }], From aa3559b66e11c55414844341692d93dfb61bd940 Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 07:13:49 +0200 Subject: [PATCH 09/18] test: fix tests --- .../mocks/routes/users.js | 2 ++ packages/admin-api-client/test/index.spec.js | 12 +++++------- packages/plugin-proxy/README.md | 17 +++-------------- .../src/index.spec.js | 6 +++--- 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/mocks/admin-api-client-unit-mocks/mocks/routes/users.js b/mocks/admin-api-client-unit-mocks/mocks/routes/users.js index c3e067414..78c3ba7f2 100644 --- a/mocks/admin-api-client-unit-mocks/mocks/routes/users.js +++ b/mocks/admin-api-client-unit-mocks/mocks/routes/users.js @@ -6,6 +6,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: [{ email: "foo@foo.com" }], @@ -13,6 +14,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: [{ email: "foo2@foo2.com" }], diff --git a/packages/admin-api-client/test/index.spec.js b/packages/admin-api-client/test/index.spec.js index 6e321be37..81148883a 100644 --- a/packages/admin-api-client/test/index.spec.js +++ b/packages/admin-api-client/test/index.spec.js @@ -29,8 +29,7 @@ describe("react-admin-client methods used with node", () => { describe("when there are alerts", () => { it("should return alerts", async () => { const alerts = await readAlerts(); - // one alert is added due to the usage of default handler - expect(alerts.length).toEqual(2); + expect(alerts.length).toEqual(1); }); it("should return alert about mock not defined", async () => { @@ -87,8 +86,7 @@ describe("react-admin-client methods used with node", () => { }); await wait(2000); const alerts = await readAlerts(); - // one alert is added due to the usage of default handler - expect(alerts.length).toEqual(1); + expect(alerts.length).toEqual(0); }); }); }); @@ -172,14 +170,14 @@ describe("react-admin-client methods used with node", () => { { id: "get-user:1", routeId: "get-user", - handler: "default", + handler: "json", response: { body: [{ email: "foo@foo.com" }], status: 200 }, delay: null, }, { id: "get-user:2", routeId: "get-user", - handler: "default", + handler: "json", response: { body: [{ email: "foo2@foo2.com" }], status: 200 }, delay: null, }, @@ -193,7 +191,7 @@ describe("react-admin-client methods used with node", () => { expect(data).toEqual({ id: "get-user:2", routeId: "get-user", - handler: "default", + handler: "json", response: { body: [{ email: "foo2@foo2.com" }], status: 200 }, delay: null, }); diff --git a/packages/plugin-proxy/README.md b/packages/plugin-proxy/README.md index 534254062..f10305e43 100644 --- a/packages/plugin-proxy/README.md +++ b/packages/plugin-proxy/README.md @@ -42,21 +42,14 @@ module.exports = [ options: {}, // Options for express-http-proxy }, }, - { - id: "disabled", - handler: "middleware", - response: { - middleware: (req, res, next) => next(), - }, - }, ], }, ]; ``` -### Route variant response +### Options -Here are listed the specific properties of a `proxy-v4` route variant. They must be defined in the `response` property of the variant: +Here are listed the specific properties that can be defined in a `proxy-v4` route variant. They must be defined in the `response` property of the variant: * __`host`__ _(String|Function)_: The proxy host. Equivalent to the [`express-http-proxy` `host` option](https://github.com/villadora/express-http-proxy#host), so it can also be a function. * __`options`__ _(Object)_: Object containing any of the [options supported by the `express-http-proxy` package](https://github.com/villadora/express-http-proxy#options). Some of them are: @@ -64,7 +57,7 @@ Here are listed the specific properties of a `proxy-v4` route variant. They must * __userResDecorator__ _(Function)_: [`userResDecorator` option](https://github.com/villadora/express-http-proxy#userresdecorator-was-intercept-supports-promise) for `express-http-proxy`. * __...__ all other [`express-http-proxy` options](https://github.com/villadora/express-http-proxy#options) are also supported. -> Tip: Note that the `delay` option is still valid for routes handled by this plugin, so you can use it to simulate that host responses are slow. +> Tip: Note that the variant `delay` option is still valid for routes handled by this plugin, so you can use it to simulate that host responses are slow. ## Usage of `proxy` handler @@ -85,10 +78,6 @@ module.exports = [ host: "https://www.google.com", // proxy host options: {}, // Options for express-http-proxy }, - { - id: "disabled", - response: (req, res, next) => next(), - }, ], }, ]; diff --git a/test/admin-api-client-nodejs-e2e/src/index.spec.js b/test/admin-api-client-nodejs-e2e/src/index.spec.js index 96d481f6e..ff0334c98 100644 --- a/test/admin-api-client-nodejs-e2e/src/index.spec.js +++ b/test/admin-api-client-nodejs-e2e/src/index.spec.js @@ -154,14 +154,14 @@ describe("react-admin-client methods used with node", () => { { id: "get-user:1", routeId: "get-user", - handler: "default", + handler: "json", response: { body: [{ email: "foo@foo.com" }], status: 200 }, delay: null, }, { id: "get-user:2", routeId: "get-user", - handler: "default", + handler: "json", response: { body: [{ email: "foo2@foo2.com" }], status: 200 }, delay: null, }, @@ -175,7 +175,7 @@ describe("react-admin-client methods used with node", () => { expect(data).toEqual({ id: "get-user:2", routeId: "get-user", - handler: "default", + handler: "json", response: { body: [{ email: "foo2@foo2.com" }], status: 200 }, delay: null, }); From 6692df79f9056e45a607d96797b40010211ba78f Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 08:58:26 +0200 Subject: [PATCH 10/18] test: Adapt E2E tests to new handlers --- .../fixtures/files-error-mock/routes/user.js | 29 +++++++++++-------- .../fixtures/files-error-mock/routes/users.js | 2 ++ .../web-tutorial-modified/routes/user.js | 29 +++++++++++-------- .../web-tutorial-modified/routes/users.js | 3 ++ .../main/fixtures/web-tutorial/routes/user.js | 29 +++++++++++-------- .../fixtures/web-tutorial/routes/users.js | 2 ++ 6 files changed, 58 insertions(+), 36 deletions(-) diff --git a/test/plugin-inquirer-cli-e2e/src/main/fixtures/files-error-mock/routes/user.js b/test/plugin-inquirer-cli-e2e/src/main/fixtures/files-error-mock/routes/user.js index ac18756e6..bdf33726f 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/fixtures/files-error-mock/routes/user.js +++ b/test/plugin-inquirer-cli-e2e/src/main/fixtures/files-error-mock/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -32,18 +34,21 @@ module.exports = [ }, { id: "real", - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); - } + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, }, }, ], diff --git a/test/plugin-inquirer-cli-e2e/src/main/fixtures/files-error-mock/routes/users.js b/test/plugin-inquirer-cli-e2e/src/main/fixtures/files-error-mock/routes/users.js index 6c1d2aecd..f406dac80 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/fixtures/files-error-mock/routes/users.js +++ b/test/plugin-inquirer-cli-e2e/src/main/fixtures/files-error-mock/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: USERS, @@ -25,6 +26,7 @@ module.exports = [ }, { id: "error", + handler: "json", response: { status: 403, body: { diff --git a/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial-modified/routes/user.js b/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial-modified/routes/user.js index ac18756e6..bdf33726f 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial-modified/routes/user.js +++ b/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial-modified/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -32,18 +34,21 @@ module.exports = [ }, { id: "real", - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); - } + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, }, }, ], diff --git a/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial-modified/routes/users.js b/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial-modified/routes/users.js index afedb5fdf..d67bede8a 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial-modified/routes/users.js +++ b/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial-modified/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { headers: { "x-custom-header": "foo-header", @@ -29,6 +30,7 @@ module.exports = [ }, { id: "error", + handler: "json", response: { status: 403, body: { @@ -45,6 +47,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: [ diff --git a/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial/routes/user.js b/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial/routes/user.js index ac18756e6..bdf33726f 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial/routes/user.js +++ b/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial/routes/user.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "1", + handler: "json", response: { status: 200, body: USERS[0], @@ -25,6 +26,7 @@ module.exports = [ }, { id: "2", + handler: "json", response: { status: 200, body: USERS[1], @@ -32,18 +34,21 @@ module.exports = [ }, { id: "real", - response: (req, res) => { - const userId = req.params.id; - const user = USERS.find((userData) => userData.id === Number(userId)); - if (user) { - res.status(200); - res.send(user); - } else { - res.status(404); - res.send({ - message: "User not found", - }); - } + handler: "middleware", + response: { + middleware: (req, res) => { + const userId = req.params.id; + const user = USERS.find((userData) => userData.id === Number(userId)); + if (user) { + res.status(200); + res.send(user); + } else { + res.status(404); + res.send({ + message: "User not found", + }); + } + }, }, }, ], diff --git a/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial/routes/users.js b/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial/routes/users.js index 6c1d2aecd..f406dac80 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial/routes/users.js +++ b/test/plugin-inquirer-cli-e2e/src/main/fixtures/web-tutorial/routes/users.js @@ -18,6 +18,7 @@ module.exports = [ variants: [ { id: "success", + handler: "json", response: { status: 200, body: USERS, @@ -25,6 +26,7 @@ module.exports = [ }, { id: "error", + handler: "json", response: { status: 403, body: { From c3f717ed7255a986e5f608224befb5b23b4a95cb Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 09:44:36 +0200 Subject: [PATCH 11/18] feat: support preview property only in v4 handlers --- packages/core/CHANGELOG.md | 2 +- packages/core/jest.config.js | 2 +- packages/core/src/mocks/helpers.js | 9 ++-- packages/core/src/mocks/validations.js | 7 ++-- packages/core/src/routes-handlers/helpers.js | 23 ++++++++++ packages/core/test/mocks/helpers.spec.js | 42 +++++++++++++++++-- .../plugin-proxy/src/ProxyRoutesHandlerV4.js | 2 +- 7 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 packages/core/src/routes-handlers/helpers.js diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 0d511f0f7..12390efbd 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - feat(#335): Pass only response property from variants to route variant handlers having the "version" property defined as "4". If it has another value, pass the whole variant object (for backward compatibility) - feat(#336): Add "Json" and "Middleware" variant handlers. -- feat: Support defining the response preview in the handlers "preview" property. Keep also "plainResponsePreview" for backward compatibility. +- feat: Support defining the response preview in the v4 handlers as "preview" property. Keep "plainResponsePreview" for backward compatibility in old handlers. - feat: Support "deprecated" property in route handlers. Add an alert whenever any route variant uses a deprecated handler ### Changed diff --git a/packages/core/jest.config.js b/packages/core/jest.config.js index cef50e4d8..d67ec1e8d 100644 --- a/packages/core/jest.config.js +++ b/packages/core/jest.config.js @@ -26,7 +26,7 @@ module.exports = { // The glob patterns Jest uses to detect test files testMatch: ["/test/**/*.spec.js"], - // testMatch: ["/test/**/mocks/validations.spec.js"], + // testMatch: ["/test/**/mocks/helpers.spec.js"], // The test environment that will be used for testing testEnvironment: "node", diff --git a/packages/core/src/mocks/helpers.js b/packages/core/src/mocks/helpers.js index 54c839a3b..bc734c940 100644 --- a/packages/core/src/mocks/helpers.js +++ b/packages/core/src/mocks/helpers.js @@ -10,6 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { flatten, compact, isUndefined } = require("lodash"); +const { getDataFromVariant, getPreview } = require("../routes-handlers/helpers"); const CustomCore = require("../CustomCore"); const Mock = require("./Mock"); const { @@ -135,14 +136,12 @@ function getPlainRoutes(routes, routesVariants) { function getPlainRoutesVariants(routesVariants) { return routesVariants.map((routeVariant) => { + const preview = getPreview(routeVariant); return { id: routeVariant.variantId, routeId: routeVariant.routeId, handler: routeVariant.constructor.id, - response: !isUndefined(routeVariant.preview) - ? routeVariant.preview - : // TODO, deprecated, remove plainResponsePreview - routeVariant.plainResponsePreview, + response: isUndefined(preview) ? null : preview, delay: routeVariant.delay, }; }); @@ -219,7 +218,7 @@ function getVariantHandler({ } try { - const variantArgument = Handler.version === "4" ? variant.response : variant; + const variantArgument = getDataFromVariant(variant, Handler); if (Handler.deprecated) { handlersAlerts.set( Handler.id, diff --git a/packages/core/src/mocks/validations.js b/packages/core/src/mocks/validations.js index ca0047263..4417da1be 100644 --- a/packages/core/src/mocks/validations.js +++ b/packages/core/src/mocks/validations.js @@ -12,6 +12,8 @@ const Ajv = require("ajv"); const { compact } = require("lodash"); const betterAjvErrors = require("better-ajv-errors").default; +const { getDataFromVariant, isVersion4 } = require("../routes-handlers/helpers"); + const ajv = new Ajv({ allErrors: true }); const HTTP_METHODS = { @@ -283,9 +285,8 @@ function variantValidationErrors(route, variant, Handler) { return null; } const variantValidator = ajv.compile(Handler.validationSchema); - const isVersion4 = Handler.version === "4"; - const dataToCheck = isVersion4 ? variant.response : variant; - const dataMessage = isVersion4 ? "Invalid 'response' property:" : ""; + const dataToCheck = getDataFromVariant(variant, Handler); + const dataMessage = isVersion4(Handler) ? "Invalid 'response' property:" : ""; const isValid = variantValidator(dataToCheck); if (!isValid) { let validationMessage; diff --git a/packages/core/src/routes-handlers/helpers.js b/packages/core/src/routes-handlers/helpers.js new file mode 100644 index 000000000..94e0c815a --- /dev/null +++ b/packages/core/src/routes-handlers/helpers.js @@ -0,0 +1,23 @@ +function isVersion4(Handler) { + return Handler.version === "4"; +} + +function getDataFromVariant(variant, Handler) { + if (isVersion4(Handler)) { + return variant.response; + } + return variant; +} + +function getPreview(variantInstance) { + if (isVersion4(variantInstance.constructor)) { + return variantInstance.preview; + } + return variantInstance.plainResponsePreview; +} + +module.exports = { + isVersion4, + getDataFromVariant, + getPreview, +}; diff --git a/packages/core/test/mocks/helpers.spec.js b/packages/core/test/mocks/helpers.spec.js index e0b3b595d..2cd9151d1 100644 --- a/packages/core/test/mocks/helpers.spec.js +++ b/packages/core/test/mocks/helpers.spec.js @@ -504,13 +504,13 @@ describe("mocks helpers", () => { ]); }); - it("should return routes variants in plain format when using preview property", () => { + it("should return routes variants in plain format when constructor is v4", () => { expect( getPlainRoutesVariants([ { variantId: "route-1:variant-1", routeId: "route-1", - constructor: { id: "handler-id-1" }, + constructor: { id: "handler-id-1", version: "4" }, preview: "response-preview-1", delay: "delay-1", foo: "foo-1", @@ -518,7 +518,7 @@ describe("mocks helpers", () => { { variantId: "route-2:variant-1", routeId: "route-2", - constructor: { id: "handler-id-2" }, + constructor: { id: "handler-id-2", version: "4" }, preview: "response-preview-2", delay: "delay-2", foo: "foo-2", @@ -541,6 +541,42 @@ describe("mocks helpers", () => { }, ]); }); + + it("should return null in response when no preview is defined", () => { + expect( + getPlainRoutesVariants([ + { + variantId: "route-1:variant-1", + routeId: "route-1", + constructor: { id: "handler-id-1", version: "4" }, + delay: "delay-1", + foo: "foo-1", + }, + { + variantId: "route-2:variant-1", + routeId: "route-2", + constructor: { id: "handler-id-2", version: "4" }, + delay: "delay-2", + foo: "foo-2", + }, + ]) + ).toEqual([ + { + id: "route-1:variant-1", + routeId: "route-1", + handler: "handler-id-1", + response: null, + delay: "delay-1", + }, + { + id: "route-2:variant-1", + routeId: "route-2", + handler: "handler-id-2", + response: null, + delay: "delay-2", + }, + ]); + }); }); describe("addCustomVariant", () => { diff --git a/packages/plugin-proxy/src/ProxyRoutesHandlerV4.js b/packages/plugin-proxy/src/ProxyRoutesHandlerV4.js index b710b603d..6e4d4cfa4 100644 --- a/packages/plugin-proxy/src/ProxyRoutesHandlerV4.js +++ b/packages/plugin-proxy/src/ProxyRoutesHandlerV4.js @@ -39,7 +39,7 @@ class ProxyRoutesHandler { this.middleware = httpProxy(this._host, this._options); } - get plainResponsePreview() { + get preview() { return { host: this._host, }; From f1590bc3c94b6dad43bd633a29aaed6b9346ee18 Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 09:52:14 +0200 Subject: [PATCH 12/18] chore: Update eslint dependency --- packages/admin-api-client/CHANGELOG.md | 5 +++++ packages/admin-api-client/src/entities.js | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/admin-api-client/CHANGELOG.md b/packages/admin-api-client/CHANGELOG.md index 26f6cd62e..072afb235 100644 --- a/packages/admin-api-client/CHANGELOG.md +++ b/packages/admin-api-client/CHANGELOG.md @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Removed ### Breaking change +## [unreleased] + +### Changed +- refactor: Rename fetch variable into crossFetch + ## [5.0.1] - 2022-06-03 ### Changed diff --git a/packages/admin-api-client/src/entities.js b/packages/admin-api-client/src/entities.js index 0f8eeecf5..dd83ce2be 100644 --- a/packages/admin-api-client/src/entities.js +++ b/packages/admin-api-client/src/entities.js @@ -1,4 +1,4 @@ -import fetch from "cross-fetch"; +import crossFetch from "cross-fetch"; import { DEFAULT_BASE_PATH, @@ -47,11 +47,11 @@ class Fetcher { } _read() { - return fetch(this.url).then(handleResponse); + return crossFetch(this.url).then(handleResponse); } _patch(data) { - return fetch(this.url, { + return crossFetch(this.url, { method: "PATCH", body: JSON.stringify(data), headers: { @@ -61,13 +61,13 @@ class Fetcher { } _delete() { - return fetch(this.url, { + return crossFetch(this.url, { method: "DELETE", }).then(handleResponse); } _create(data) { - return fetch(this.url, { + return crossFetch(this.url, { method: "POST", body: JSON.stringify(data), headers: { From ecb85134fb9e6efe1bd1ce1edfddb23691fde596 Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 09:56:24 +0200 Subject: [PATCH 13/18] chore: Report affected packages before running lint in order to generate graph --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ea9f4bf5a..0b3a3194f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -85,6 +85,8 @@ jobs: ${{ runner.os }}-pnpm-store- - name: Install dependencies run: pnpm install + - name: Report affected + run: pnpm run affected:report - name: Lint run: pnpm run lint test-unit: From 30a3090f53a8277781ef39d543ffee3209dc9a5e Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 10:02:38 +0200 Subject: [PATCH 14/18] chore: Generate graph --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0b3a3194f..45ed2f4a8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -85,8 +85,8 @@ jobs: ${{ runner.os }}-pnpm-store- - name: Install dependencies run: pnpm install - - name: Report affected - run: pnpm run affected:report + - name: Generate graph + run: pnpm run graph -- --file=graph.json - name: Lint run: pnpm run lint test-unit: From 6f0ec3eaed9ff171fdcb9476a023ffbb33bfa83e Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 10:37:17 +0200 Subject: [PATCH 15/18] chore: Update eslint --- .eslintrc.cjs | 1 + package.json | 2 +- .../plugin-admin-api/test/about-api.spec.js | 4 +- .../plugin-admin-api/test/alerts-api.spec.js | 16 +- packages/plugin-admin-api/test/cors.spec.js | 10 +- .../mock-custom-routes-variants-api.spec.js | 22 +- .../plugin-admin-api/test/mocks-api.spec.js | 10 +- .../test/plugin-options.spec.js | 4 +- .../plugin-admin-api/test/routes-api.spec.js | 10 +- .../test/routes-variants-api.spec.js | 10 +- .../test/settings-api.spec.js | 56 ++-- .../plugin-admin-api/test/stop-plugin.spec.js | 16 +- .../plugin-admin-api/test/support/helpers.js | 4 +- .../plugin-proxy/test/docsExample.spec.js | 12 +- packages/plugin-proxy/test/filters.spec.js | 12 +- .../plugin-proxy/test/hostFunction.spec.js | 8 +- .../test/legacy/docsExample.spec.js | 6 +- .../plugin-proxy/test/legacy/filters.spec.js | 6 +- .../test/legacy/hostFunction.spec.js | 8 +- .../test/legacy/optionDelay.spec.js | 6 +- .../test/legacy/resDecorator.spec.js | 6 +- .../test/legacy/variantDelay.spec.js | 6 +- .../plugin-proxy/test/optionDelay.spec.js | 6 +- .../plugin-proxy/test/resDecorator.spec.js | 12 +- packages/plugin-proxy/test/support/helpers.js | 4 +- .../plugin-proxy/test/variantDelay.spec.js | 6 +- pnpm-lock.yaml | 277 ++++++++---------- ...fig-file-with-custom-route-handler.spec.js | 4 +- .../src/config-file-with-plugins.spec.js | 10 +- test/core-e2e-legacy/src/config-file.spec.js | 10 +- .../core-e2e-legacy/src/cors-argument.spec.js | 20 +- .../core-e2e-legacy/src/custom-router.spec.js | 14 +- .../src/delay-argument.spec.js | 4 +- .../core-e2e-legacy/src/delay-setting.spec.js | 22 +- test/core-e2e-legacy/src/esm.spec.js | 16 +- test/core-e2e-legacy/src/events.spec.js | 4 +- test/core-e2e-legacy/src/json-files.spec.js | 20 +- .../src/load-mocks-and-routes.spec.js | 20 +- .../core-e2e-legacy/src/mock-argument.spec.js | 8 +- test/core-e2e-legacy/src/mock-setting.spec.js | 28 +- .../core-e2e-legacy/src/multi-methods.spec.js | 10 +- .../core-e2e-legacy/src/path-argument.spec.js | 4 +- .../src/path-not-exists.spec.js | 20 +- test/core-e2e-legacy/src/path-setting.spec.js | 8 +- .../src/plugins-legacy-alerts.spec.js | 8 +- .../src/plugins-legacy-formats.spec.js | 8 +- test/core-e2e-legacy/src/plugins.spec.js | 8 +- .../core-e2e-legacy/src/port-argument.spec.js | 4 +- test/core-e2e-legacy/src/port-setting.spec.js | 6 +- .../core-e2e-legacy/src/route-handler.spec.js | 4 +- .../src/route-variants.spec.js | 52 ++-- test/core-e2e-legacy/src/scaffold.spec.js | 22 +- test/core-e2e-legacy/src/support/helpers.js | 4 +- test/core-e2e-legacy/src/typescript.spec.js | 12 +- test/core-e2e-legacy/src/validations.spec.js | 10 +- .../src/watch-disabled.spec.js | 6 +- .../core-e2e-legacy/src/watch-enabled.spec.js | 10 +- ...fig-file-with-custom-route-handler.spec.js | 4 +- .../src/config-file-with-plugins.spec.js | 10 +- test/core-e2e/src/config-file.spec.js | 10 +- test/core-e2e/src/cors-argument.spec.js | 20 +- test/core-e2e/src/custom-router.spec.js | 14 +- test/core-e2e/src/delay-argument.spec.js | 4 +- test/core-e2e/src/delay-setting.spec.js | 22 +- test/core-e2e/src/esm.spec.js | 16 +- test/core-e2e/src/events.spec.js | 4 +- test/core-e2e/src/json-files.spec.js | 20 +- .../src/load-mocks-and-routes.spec.js | 20 +- test/core-e2e/src/mock-argument.spec.js | 8 +- test/core-e2e/src/mock-setting.spec.js | 28 +- test/core-e2e/src/multi-methods.spec.js | 10 +- test/core-e2e/src/path-argument.spec.js | 4 +- test/core-e2e/src/path-not-exists.spec.js | 20 +- test/core-e2e/src/path-setting.spec.js | 8 +- test/core-e2e/src/plugins.spec.js | 8 +- test/core-e2e/src/port-argument.spec.js | 4 +- test/core-e2e/src/port-setting.spec.js | 6 +- test/core-e2e/src/route-handler.spec.js | 4 +- test/core-e2e/src/route-variants.spec.js | 52 ++-- test/core-e2e/src/scaffold.spec.js | 22 +- test/core-e2e/src/support/helpers.js | 4 +- test/core-e2e/src/typescript.spec.js | 12 +- test/core-e2e/src/validations.spec.js | 10 +- test/core-e2e/src/watch-disabled.spec.js | 6 +- test/core-e2e/src/watch-enabled.spec.js | 10 +- test/main-e2e/src/proxy.spec.js | 32 +- test/main-e2e/src/scaffold.spec.js | 60 ++-- test/main-e2e/src/support/helpers.js | 4 +- .../src/main/arguments.spec.js | 12 +- .../src/main/disabled.spec.js | 10 +- .../src/main/files-watch.spec.js | 28 +- .../src/main/interactive-cli.spec.js | 20 +- .../src/main/support/helpers.js | 4 +- .../src/main/web-tutorial.spec.js | 38 +-- 94 files changed, 743 insertions(+), 741 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 2b7d57825..37cd05be5 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -57,6 +57,7 @@ module.exports = { describe: true, expect: true, it: true, + fetch: false, }, plugins: ["jest", "no-only-tests"], extends: ["plugin:jest/recommended"], diff --git a/package.json b/package.json index 88289e0ac..761818218 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "cypress": "9.7.0", "cypress-fail-fast": "3.4.1", "deepmerge": "4.2.2", - "eslint": "8.15.0", + "eslint": "8.19.0", "eslint-config-prettier": "8.5.0", "eslint-plugin-jest": "26.4.6", "eslint-plugin-no-only-tests": "2.6.0", diff --git a/packages/plugin-admin-api/test/about-api.spec.js b/packages/plugin-admin-api/test/about-api.spec.js index 0b0956f80..2ce4c6a8b 100644 --- a/packages/plugin-admin-api/test/about-api.spec.js +++ b/packages/plugin-admin-api/test/about-api.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, waitForServer } = require("./support/helpers"); +const { startServer, doFetch, waitForServer } = require("./support/helpers"); const { version } = require("../package.json"); describe("about api", () => { @@ -24,7 +24,7 @@ describe("about api", () => { describe("get /", () => { it("should return current version", async () => { - const response = await fetch("/admin/about"); + const response = await doFetch("/admin/about"); expect(response.body).toEqual({ version, }); diff --git a/packages/plugin-admin-api/test/alerts-api.spec.js b/packages/plugin-admin-api/test/alerts-api.spec.js index c5d81ba84..c49b81364 100644 --- a/packages/plugin-admin-api/test/alerts-api.spec.js +++ b/packages/plugin-admin-api/test/alerts-api.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, waitForServer, wait, fixturesFolder } = require("./support/helpers"); +const { startServer, doFetch, waitForServer, wait, fixturesFolder } = require("./support/helpers"); describe("alerts api", () => { let server; @@ -27,13 +27,13 @@ describe("alerts api", () => { describe("when started", () => { it("should return mock not found alert", async () => { - const response = await fetch("/admin/alerts"); + const response = await doFetch("/admin/alerts"); // one alert is caused by deprecated handler expect(response.body.length).toEqual(2); }); it("should return specific alert when requested by id", async () => { - const response = await fetch("/admin/alerts/mocks%3Asettings"); + const response = await doFetch("/admin/alerts/mocks%3Asettings"); expect(response.body).toEqual({ id: "mocks:settings", context: "mocks:settings", @@ -43,7 +43,7 @@ describe("alerts api", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -53,7 +53,7 @@ describe("alerts api", () => { describe("when mock is modified", () => { beforeAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { mocks: { @@ -65,7 +65,7 @@ describe("alerts api", () => { }, 10000); it("should return no alerts", async () => { - const response = await fetch("/admin/alerts"); + const response = await doFetch("/admin/alerts"); // one alert is caused by deprecated handler expect(response.body.length).toEqual(1); }); @@ -73,7 +73,7 @@ describe("alerts api", () => { describe("when there is an error loading files", () => { beforeAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { files: { @@ -85,7 +85,7 @@ describe("alerts api", () => { }, 10000); it("should return alert containing error", async () => { - const response = await fetch("/admin/alerts"); + const response = await doFetch("/admin/alerts"); expect(response.body.length).toEqual(4); expect(response.body[3].error.message).toEqual( expect.stringContaining("Cannot find module '../db/users'") diff --git a/packages/plugin-admin-api/test/cors.spec.js b/packages/plugin-admin-api/test/cors.spec.js index 8660dda7b..2deb1e7e3 100644 --- a/packages/plugin-admin-api/test/cors.spec.js +++ b/packages/plugin-admin-api/test/cors.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, waitForServer } = require("./support/helpers"); +const { startServer, doFetch, waitForServer } = require("./support/helpers"); describe("cors middleware", () => { let server; @@ -24,7 +24,7 @@ describe("cors middleware", () => { }); it("should add cors headers to admin api routes", async () => { - const response = await fetch("/admin/about", { method: "OPTIONS" }); + const response = await doFetch("/admin/about", { method: "OPTIONS" }); expect(response.headers.get("access-control-allow-origin")).toEqual("*"); expect(response.headers.get("access-control-allow-methods")).toEqual( "GET,HEAD,PUT,PATCH,POST,DELETE" @@ -32,7 +32,7 @@ describe("cors middleware", () => { }); it("should add cors headers to mock routes", async () => { - const response = await fetch("/api/users/2", { method: "OPTIONS" }); + const response = await doFetch("/api/users/2", { method: "OPTIONS" }); expect(response.headers.get("access-control-allow-origin")).toEqual("*"); expect(response.headers.get("access-control-allow-methods")).toEqual( "GET,HEAD,PUT,PATCH,POST,DELETE" @@ -53,7 +53,7 @@ describe("cors middleware", () => { }); it("should add cors headers to admin api routes", async () => { - const response = await fetch("/admin/about", { method: "OPTIONS" }); + const response = await doFetch("/admin/about", { method: "OPTIONS" }); expect(response.headers.get("access-control-allow-origin")).toEqual("*"); expect(response.headers.get("access-control-allow-methods")).toEqual( "GET,HEAD,PUT,PATCH,POST,DELETE" @@ -61,7 +61,7 @@ describe("cors middleware", () => { }); it("should disable cors headers in mock routes", async () => { - const response = await fetch("/api/users/2", { method: "OPTIONS" }); + const response = await doFetch("/api/users/2", { method: "OPTIONS" }); expect(response.headers.get("access-control-allow-origin")).toEqual(null); expect(response.headers.get("access-control-allow-methods")).toEqual(null); }); diff --git a/packages/plugin-admin-api/test/mock-custom-routes-variants-api.spec.js b/packages/plugin-admin-api/test/mock-custom-routes-variants-api.spec.js index dd4cf063a..ee85500f6 100644 --- a/packages/plugin-admin-api/test/mock-custom-routes-variants-api.spec.js +++ b/packages/plugin-admin-api/test/mock-custom-routes-variants-api.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, waitForServer } = require("./support/helpers"); +const { startServer, doFetch, waitForServer } = require("./support/helpers"); describe("mock custom routes variants api", () => { let server; @@ -24,58 +24,58 @@ describe("mock custom routes variants api", () => { describe("get /", () => { it("should return mock custom routes variants", async () => { - const response = await fetch("/admin/mock-custom-routes-variants"); + const response = await doFetch("/admin/mock-custom-routes-variants"); expect(response.body).toEqual([]); }); }); describe("post /", () => { it("should add mock custom route variant", async () => { - await fetch("/admin/mock-custom-routes-variants", { + await doFetch("/admin/mock-custom-routes-variants", { method: "POST", body: { id: "get-user:2", }, }); - const response = await fetch("/admin/mock-custom-routes-variants"); + const response = await doFetch("/admin/mock-custom-routes-variants"); expect(response.body).toEqual(["get-user:2"]); }); it("should have changed user response", async () => { - const response = await fetch("/api/users/1"); + const response = await doFetch("/api/users/1"); expect(response.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); describe("delete /", () => { it("should restore mock routes variants", async () => { - await fetch("/admin/mock-custom-routes-variants", { + await doFetch("/admin/mock-custom-routes-variants", { method: "DELETE", }); - const response = await fetch("/admin/mock-custom-routes-variants"); + const response = await doFetch("/admin/mock-custom-routes-variants"); expect(response.body).toEqual([]); }); it("should have changed user response", async () => { - const response = await fetch("/api/users/1"); + const response = await doFetch("/api/users/1"); expect(response.body).toEqual({ id: 1, name: "John Doe" }); }); }); describe("when trying to set an unexistent route variant", () => { it("should not add mock custom route variant", async () => { - await fetch("/admin/mock-custom-routes-variants", { + await doFetch("/admin/mock-custom-routes-variants", { method: "POST", body: { id: "foo", }, }); - const response = await fetch("/admin/mock-custom-routes-variants"); + const response = await doFetch("/admin/mock-custom-routes-variants"); expect(response.body).toEqual([]); }); it("should not have changed user response", async () => { - const response = await fetch("/api/users/1"); + const response = await doFetch("/api/users/1"); expect(response.body).toEqual({ id: 1, name: "John Doe" }); }); }); diff --git a/packages/plugin-admin-api/test/mocks-api.spec.js b/packages/plugin-admin-api/test/mocks-api.spec.js index 9c3e1c086..dc4c3dff9 100644 --- a/packages/plugin-admin-api/test/mocks-api.spec.js +++ b/packages/plugin-admin-api/test/mocks-api.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, waitForServer } = require("./support/helpers"); +const { startServer, doFetch, waitForServer } = require("./support/helpers"); describe("mocks api", () => { let server; @@ -24,7 +24,7 @@ describe("mocks api", () => { describe("get /", () => { it("should return current mocks", async () => { - const response = await fetch("/admin/mocks"); + const response = await doFetch("/admin/mocks"); expect(response.body).toEqual([ { id: "base", @@ -50,7 +50,7 @@ describe("mocks api", () => { describe("get /base", () => { it("should return base mock", async () => { - const response = await fetch("/admin/mocks/base"); + const response = await doFetch("/admin/mocks/base"); expect(response.body).toEqual({ id: "base", from: null, @@ -62,7 +62,7 @@ describe("mocks api", () => { describe("get /user-2", () => { it("should return user-2 mock", async () => { - const response = await fetch("/admin/mocks/user-2"); + const response = await doFetch("/admin/mocks/user-2"); expect(response.body).toEqual({ id: "user-2", from: "base", @@ -74,7 +74,7 @@ describe("mocks api", () => { describe("get unexistant mock", () => { it("should return a not found error", async () => { - const response = await fetch("/admin/mocks/foo"); + const response = await doFetch("/admin/mocks/foo"); expect(response.status).toEqual(404); expect(response.body.message).toEqual('Mock with id "foo" was not found'); }); diff --git a/packages/plugin-admin-api/test/plugin-options.spec.js b/packages/plugin-admin-api/test/plugin-options.spec.js index 6927e4460..644f395fc 100644 --- a/packages/plugin-admin-api/test/plugin-options.spec.js +++ b/packages/plugin-admin-api/test/plugin-options.spec.js @@ -7,7 +7,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, waitForServer } = require("./support/helpers"); +const { startServer, doFetch, waitForServer } = require("./support/helpers"); describe("plugin options", () => { let server; @@ -29,7 +29,7 @@ describe("plugin options", () => { }); it("should change the administration api path", async () => { - const adminResponse = await fetch("/foo/settings"); + const adminResponse = await doFetch("/foo/settings"); expect(adminResponse.body.plugins.adminApi.path).toEqual("/foo"); }); }); diff --git a/packages/plugin-admin-api/test/routes-api.spec.js b/packages/plugin-admin-api/test/routes-api.spec.js index 42fa0f99a..7e16b78ef 100644 --- a/packages/plugin-admin-api/test/routes-api.spec.js +++ b/packages/plugin-admin-api/test/routes-api.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, waitForServer } = require("./support/helpers"); +const { startServer, doFetch, waitForServer } = require("./support/helpers"); describe("routes api", () => { let server; @@ -24,7 +24,7 @@ describe("routes api", () => { describe("get /", () => { it("should return current routes", async () => { - const response = await fetch("/admin/routes"); + const response = await doFetch("/admin/routes"); expect(response.body).toEqual([ { id: "get-user", @@ -46,7 +46,7 @@ describe("routes api", () => { describe("get /get-user", () => { it("should return route with id get-user", async () => { - const response = await fetch("/admin/routes/get-user"); + const response = await doFetch("/admin/routes/get-user"); expect(response.body).toEqual({ id: "get-user", delay: null, @@ -59,7 +59,7 @@ describe("routes api", () => { describe("get /get-users", () => { it("should return route with id get-users", async () => { - const response = await fetch("/admin/routes/get-users"); + const response = await doFetch("/admin/routes/get-users"); expect(response.body).toEqual({ id: "get-users", delay: null, @@ -72,7 +72,7 @@ describe("routes api", () => { describe("get unexistant route", () => { it("should return a not found error", async () => { - const response = await fetch("/admin/routes/foo"); + const response = await doFetch("/admin/routes/foo"); expect(response.status).toEqual(404); expect(response.body.message).toEqual('Route with id "foo" was not found'); }); diff --git a/packages/plugin-admin-api/test/routes-variants-api.spec.js b/packages/plugin-admin-api/test/routes-variants-api.spec.js index 45126297a..2445e379e 100644 --- a/packages/plugin-admin-api/test/routes-variants-api.spec.js +++ b/packages/plugin-admin-api/test/routes-variants-api.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, waitForServer } = require("./support/helpers"); +const { startServer, doFetch, waitForServer } = require("./support/helpers"); describe("routes variants api", () => { let server; @@ -24,7 +24,7 @@ describe("routes variants api", () => { describe("get /", () => { it("should return routes variants", async () => { - const response = await fetch("/admin/routes-variants"); + const response = await doFetch("/admin/routes-variants"); expect(response.body).toEqual([ { id: "get-user:1", @@ -73,7 +73,7 @@ describe("routes variants api", () => { describe("get /get-user:1", () => { it("should return route variant with id get-user:1", async () => { - const response = await fetch("/admin/routes-variants/get-user:1"); + const response = await doFetch("/admin/routes-variants/get-user:1"); expect(response.body).toEqual({ id: "get-user:1", routeId: "get-user", @@ -86,7 +86,7 @@ describe("routes variants api", () => { describe("get /get-user:2", () => { it("should return route variant with id get-user:2", async () => { - const response = await fetch("/admin/routes-variants/get-user:2"); + const response = await doFetch("/admin/routes-variants/get-user:2"); expect(response.body).toEqual({ id: "get-user:2", routeId: "get-user", @@ -99,7 +99,7 @@ describe("routes variants api", () => { describe("get unexistant route variant", () => { it("should return a not found error", async () => { - const response = await fetch("/admin/routes-variants/foo"); + const response = await doFetch("/admin/routes-variants/foo"); expect(response.status).toEqual(404); expect(response.body.message).toEqual('Route variant with id "foo" was not found'); }); diff --git a/packages/plugin-admin-api/test/settings-api.spec.js b/packages/plugin-admin-api/test/settings-api.spec.js index 7440c41d9..33fa6be39 100644 --- a/packages/plugin-admin-api/test/settings-api.spec.js +++ b/packages/plugin-admin-api/test/settings-api.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startServer, - fetch, + doFetch, fixturesFolder, TimeCounter, wait, @@ -30,7 +30,7 @@ describe("settings api", () => { describe("get", () => { it("should return current settings", async () => { - const settingsResponse = await fetch("/admin/settings"); + const settingsResponse = await doFetch("/admin/settings"); expect(settingsResponse.body).toEqual({ config: { allowUnknownArguments: true, @@ -85,7 +85,7 @@ describe("settings api", () => { describe("when changing an unexistant option", () => { it("should response with a bad request containing errors", async () => { expect.assertions(4); - const settingsResponse = await fetch("/admin/settings", { + const settingsResponse = await doFetch("/admin/settings", { method: "PATCH", body: { foo: "foo-value", @@ -103,14 +103,14 @@ describe("settings api", () => { it("should not apply any change if request contains any error", async () => { expect.assertions(3); - const settingsUpdateResponse = await fetch("/admin/settings", { + const settingsUpdateResponse = await doFetch("/admin/settings", { method: "PATCH", body: { foo: "foo-value", delay: 1000, }, }); - const settingsResponse = await fetch("/admin/settings"); + const settingsResponse = await doFetch("/admin/settings"); expect(settingsUpdateResponse.status).toEqual(400); expect(settingsUpdateResponse.body.message).toEqual(expect.stringContaining("foo")); expect(settingsResponse.body.mocks.delay).toEqual(0); @@ -120,14 +120,14 @@ describe("settings api", () => { describe("when changing delay option", () => { it("should respond with no delay", async () => { const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeLessThan(200); }); it("should set delay option and have effect on server response time", async () => { const timeCounter = new TimeCounter(); - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { mocks: { @@ -135,14 +135,14 @@ describe("settings api", () => { }, }, }); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(2000); }); it("should set delay option to 0", async () => { const timeCounter = new TimeCounter(); - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { mocks: { @@ -150,7 +150,7 @@ describe("settings api", () => { }, }, }); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeLessThan(200); }); @@ -159,19 +159,19 @@ describe("settings api", () => { describe("when changing mock option", () => { describe("without changing it", () => { it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); describe('changing it to "user-2"', () => { beforeAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { mocks: { @@ -182,17 +182,17 @@ describe("settings api", () => { }); it("should return new mock when getting settings", async () => { - const settingsResponse = await fetch("/admin/settings"); + const settingsResponse = await doFetch("/admin/settings"); expect(settingsResponse.body.mocks.selected).toEqual("user-2"); }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); @@ -200,7 +200,7 @@ describe("settings api", () => { describe("when changing path option", () => { beforeAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { files: { @@ -212,7 +212,7 @@ describe("settings api", () => { }); afterAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { files: { @@ -224,12 +224,12 @@ describe("settings api", () => { }); it("should return new path option when getting settings", async () => { - const settingsResponse = await fetch("/admin/settings"); + const settingsResponse = await doFetch("/admin/settings"); expect(settingsResponse.body.files.path).toEqual(fixturesFolder("web-tutorial-modified")); }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -239,7 +239,7 @@ describe("settings api", () => { describe("when changing port option", () => { beforeAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { server: { @@ -251,7 +251,7 @@ describe("settings api", () => { }); afterAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { port: 3101, method: "PATCH", body: { @@ -264,14 +264,14 @@ describe("settings api", () => { }); it("should return new port option when getting settings, using new port", async () => { - const settingsResponse = await fetch("/admin/settings", { + const settingsResponse = await doFetch("/admin/settings", { port: 3101, }); expect(settingsResponse.body.server.port).toEqual(3101); }); it("should serve user 2 under the /api/users/1 path using new port", async () => { - const users = await fetch("/api/users/1", { + const users = await doFetch("/api/users/1", { port: 3101, }); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); @@ -280,7 +280,7 @@ describe("settings api", () => { describe("when changing adminApiPath option", () => { beforeAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { plugins: { @@ -294,7 +294,7 @@ describe("settings api", () => { }); afterAll(async () => { - await fetch("/administration/settings", { + await doFetch("/administration/settings", { method: "PATCH", body: { plugins: { @@ -308,12 +308,12 @@ describe("settings api", () => { }); it("should return new port adminApiPath when getting settings, using new admin api path", async () => { - const settingsResponse = await fetch("/administration/settings"); + const settingsResponse = await doFetch("/administration/settings"); expect(settingsResponse.body.plugins.adminApi.path).toEqual("/administration"); }); it("should return not found adminApiPath when getting settings in old admin api path", async () => { - const settingsResponse = await fetch("/admin/settings"); + const settingsResponse = await doFetch("/admin/settings"); expect(settingsResponse.status).toEqual(404); }); }); diff --git a/packages/plugin-admin-api/test/stop-plugin.spec.js b/packages/plugin-admin-api/test/stop-plugin.spec.js index de00b5659..de0bad408 100644 --- a/packages/plugin-admin-api/test/stop-plugin.spec.js +++ b/packages/plugin-admin-api/test/stop-plugin.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startServer, fetch, fixturesFolder, wait, waitForServer } = require("./support/helpers"); +const { startServer, doFetch, fixturesFolder, wait, waitForServer } = require("./support/helpers"); describe("when stopping plugin", () => { let server; @@ -23,7 +23,7 @@ describe("when stopping plugin", () => { describe("when started", () => { it("should return current settings", async () => { - const response = await fetch("/admin/settings"); + const response = await doFetch("/admin/settings"); expect(response.body.files.path).toEqual(fixturesFolder("web-tutorial")); }); }); @@ -32,13 +32,13 @@ describe("when stopping plugin", () => { it("should respond not found when requesting setting", async () => { await server._stopPlugins(); await waitForServer(); - const response = await fetch("/admin/settings"); + const response = await doFetch("/admin/settings"); expect(response.status).toEqual(404); }); it("should respond to mocks requests", async () => { await server._stopPlugins(); - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -60,7 +60,7 @@ describe("when stopping plugin", () => { }, }); await wait(1000); - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Jane Doe", @@ -70,7 +70,7 @@ describe("when stopping plugin", () => { it("should have not started the plugin", async () => { await server._stopPlugins(); await waitForServer(); - const response = await fetch("/admin/settings"); + const response = await doFetch("/admin/settings"); expect(response.status).toEqual(404); }); }); @@ -79,7 +79,7 @@ describe("when stopping plugin", () => { it("should respond with same mock", async () => { await server._startPlugins(); await waitForServer(); - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Jane Doe", @@ -87,7 +87,7 @@ describe("when stopping plugin", () => { }); it("should have started the plugin", async () => { - const response = await fetch("/admin/settings"); + const response = await doFetch("/admin/settings"); expect(response.body.files.path).toEqual(fixturesFolder("web-tutorial")); }); }); diff --git a/packages/plugin-admin-api/test/support/helpers.js b/packages/plugin-admin-api/test/support/helpers.js index 8eb8f693f..74e1b8c88 100644 --- a/packages/plugin-admin-api/test/support/helpers.js +++ b/packages/plugin-admin-api/test/support/helpers.js @@ -83,7 +83,7 @@ const serverUrl = (port) => { return `http://127.0.0.1:${port || SERVER_PORT}`; }; -const fetch = (uri, options = {}) => { +const doFetch = (uri, options = {}) => { const requestOptions = { ...defaultRequestOptions, ...options, @@ -155,7 +155,7 @@ module.exports = { createCore, startExistingCore, startServer, - fetch, + doFetch, TimeCounter, wait, waitForServer, diff --git a/packages/plugin-proxy/test/docsExample.spec.js b/packages/plugin-proxy/test/docsExample.spec.js index 5d26f0269..197208894 100644 --- a/packages/plugin-proxy/test/docsExample.spec.js +++ b/packages/plugin-proxy/test/docsExample.spec.js @@ -1,4 +1,10 @@ -const { startServer, fetch, waitForServer, waitForHost, startHost } = require("./support/helpers"); +const { + startServer, + doFetch, + waitForServer, + waitForHost, + startHost, +} = require("./support/helpers"); describe("Docs example", () => { let server, host; @@ -17,7 +23,7 @@ describe("Docs example", () => { describe("get /users", () => { it("should return users from host", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -33,7 +39,7 @@ describe("Docs example", () => { describe("get /users/2", () => { it("should return second user from host", async () => { - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Jane Doe", diff --git a/packages/plugin-proxy/test/filters.spec.js b/packages/plugin-proxy/test/filters.spec.js index c7cc93dc4..e3a9bc78d 100644 --- a/packages/plugin-proxy/test/filters.spec.js +++ b/packages/plugin-proxy/test/filters.spec.js @@ -1,4 +1,10 @@ -const { startServer, fetch, waitForServer, waitForHost, startHost } = require("./support/helpers"); +const { + startServer, + doFetch, + waitForServer, + waitForHost, + startHost, +} = require("./support/helpers"); describe("when using filter option", () => { let server, host; @@ -17,7 +23,7 @@ describe("when using filter option", () => { describe("get /users", () => { it("should return users from host", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -33,7 +39,7 @@ describe("when using filter option", () => { describe("get /users/2", () => { it("should return second user from mock", async () => { - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Mocked User", diff --git a/packages/plugin-proxy/test/hostFunction.spec.js b/packages/plugin-proxy/test/hostFunction.spec.js index c4251fe68..8c040f0af 100644 --- a/packages/plugin-proxy/test/hostFunction.spec.js +++ b/packages/plugin-proxy/test/hostFunction.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, waitForServer, waitForHost, waitForHost2, @@ -28,7 +28,7 @@ describe("when defining host option as a function", () => { describe("get /users", () => { it("should return users from host", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -44,7 +44,7 @@ describe("when defining host option as a function", () => { describe("get /users/1", () => { it("should return user from second host", async () => { - const response = await fetch("/api/users/1"); + const response = await doFetch("/api/users/1"); expect(response.body).toEqual({ id: 1, name: "John Doe 2", @@ -54,7 +54,7 @@ describe("when defining host option as a function", () => { describe("get /users/2", () => { it("should return user from second host", async () => { - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Jane Doe 2", diff --git a/packages/plugin-proxy/test/legacy/docsExample.spec.js b/packages/plugin-proxy/test/legacy/docsExample.spec.js index c31b073fb..07d64c3aa 100644 --- a/packages/plugin-proxy/test/legacy/docsExample.spec.js +++ b/packages/plugin-proxy/test/legacy/docsExample.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, waitForServer, waitForHost, startHost, @@ -23,7 +23,7 @@ describe("Docs example", () => { describe("get /users", () => { it("should return users from host", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -39,7 +39,7 @@ describe("Docs example", () => { describe("get /users/2", () => { it("should return second user from host", async () => { - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Jane Doe", diff --git a/packages/plugin-proxy/test/legacy/filters.spec.js b/packages/plugin-proxy/test/legacy/filters.spec.js index 7c87d5ea1..e484159fe 100644 --- a/packages/plugin-proxy/test/legacy/filters.spec.js +++ b/packages/plugin-proxy/test/legacy/filters.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, waitForServer, waitForHost, startHost, @@ -23,7 +23,7 @@ describe("when using filter option", () => { describe("get /users", () => { it("should return users from host", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -39,7 +39,7 @@ describe("when using filter option", () => { describe("get /users/2", () => { it("should return second user from mock", async () => { - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Mocked User", diff --git a/packages/plugin-proxy/test/legacy/hostFunction.spec.js b/packages/plugin-proxy/test/legacy/hostFunction.spec.js index 0e21a8916..b5b6db4e3 100644 --- a/packages/plugin-proxy/test/legacy/hostFunction.spec.js +++ b/packages/plugin-proxy/test/legacy/hostFunction.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, waitForServer, waitForHost, waitForHost2, @@ -28,7 +28,7 @@ describe("when defining host option as a function", () => { describe("get /users", () => { it("should return users from host", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -44,7 +44,7 @@ describe("when defining host option as a function", () => { describe("get /users/1", () => { it("should return user from second host", async () => { - const response = await fetch("/api/users/1"); + const response = await doFetch("/api/users/1"); expect(response.body).toEqual({ id: 1, name: "John Doe 2", @@ -54,7 +54,7 @@ describe("when defining host option as a function", () => { describe("get /users/2", () => { it("should return user from second host", async () => { - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Jane Doe 2", diff --git a/packages/plugin-proxy/test/legacy/optionDelay.spec.js b/packages/plugin-proxy/test/legacy/optionDelay.spec.js index 9befb14c6..1f7245bb3 100644 --- a/packages/plugin-proxy/test/legacy/optionDelay.spec.js +++ b/packages/plugin-proxy/test/legacy/optionDelay.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, TimeCounter, waitForServer, waitForHost, @@ -29,7 +29,7 @@ describe("when using delay option in server", () => { describe("get /users", () => { it("should return users from host after 1 second", async () => { const timeCounter = new TimeCounter(); - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); expect(response.body).toEqual([ @@ -48,7 +48,7 @@ describe("when using delay option in server", () => { describe("get /users/2", () => { it("should return second user from host after 1 second", async () => { const timeCounter = new TimeCounter(); - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); expect(response.body).toEqual({ diff --git a/packages/plugin-proxy/test/legacy/resDecorator.spec.js b/packages/plugin-proxy/test/legacy/resDecorator.spec.js index b1329950f..d723e193e 100644 --- a/packages/plugin-proxy/test/legacy/resDecorator.spec.js +++ b/packages/plugin-proxy/test/legacy/resDecorator.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, waitForServer, waitForHost, startHost, @@ -23,7 +23,7 @@ describe("when using filter option", () => { describe("get /users", () => { it("should return modified users from host", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -39,7 +39,7 @@ describe("when using filter option", () => { describe("get /users/2", () => { it("should return second user from host", async () => { - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Modified Jane Doe", diff --git a/packages/plugin-proxy/test/legacy/variantDelay.spec.js b/packages/plugin-proxy/test/legacy/variantDelay.spec.js index 1cc5dca5a..dd05652d9 100644 --- a/packages/plugin-proxy/test/legacy/variantDelay.spec.js +++ b/packages/plugin-proxy/test/legacy/variantDelay.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, TimeCounter, waitForServer, waitForHost, @@ -25,7 +25,7 @@ describe("when using delay option in variant", () => { describe("get /users", () => { it("should return users from host after 1 second", async () => { const timeCounter = new TimeCounter(); - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); expect(response.body).toEqual([ @@ -44,7 +44,7 @@ describe("when using delay option in variant", () => { describe("get /users/2", () => { it("should return second user from host after 1 second", async () => { const timeCounter = new TimeCounter(); - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); expect(response.body).toEqual({ diff --git a/packages/plugin-proxy/test/optionDelay.spec.js b/packages/plugin-proxy/test/optionDelay.spec.js index 6cb6a0baf..1311745ff 100644 --- a/packages/plugin-proxy/test/optionDelay.spec.js +++ b/packages/plugin-proxy/test/optionDelay.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, TimeCounter, waitForServer, waitForHost, @@ -29,7 +29,7 @@ describe("when using delay option in server", () => { describe("get /users", () => { it("should return users from host after 1 second", async () => { const timeCounter = new TimeCounter(); - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); expect(response.body).toEqual([ @@ -48,7 +48,7 @@ describe("when using delay option in server", () => { describe("get /users/2", () => { it("should return second user from host after 1 second", async () => { const timeCounter = new TimeCounter(); - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); expect(response.body).toEqual({ diff --git a/packages/plugin-proxy/test/resDecorator.spec.js b/packages/plugin-proxy/test/resDecorator.spec.js index 4783eef63..408c0425f 100644 --- a/packages/plugin-proxy/test/resDecorator.spec.js +++ b/packages/plugin-proxy/test/resDecorator.spec.js @@ -1,4 +1,10 @@ -const { startServer, fetch, waitForServer, waitForHost, startHost } = require("./support/helpers"); +const { + startServer, + doFetch, + waitForServer, + waitForHost, + startHost, +} = require("./support/helpers"); describe("when using filter option", () => { let server, host; @@ -17,7 +23,7 @@ describe("when using filter option", () => { describe("get /users", () => { it("should return modified users from host", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.body).toEqual([ { id: 1, @@ -33,7 +39,7 @@ describe("when using filter option", () => { describe("get /users/2", () => { it("should return second user from host", async () => { - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); expect(response.body).toEqual({ id: 2, name: "Modified Jane Doe", diff --git a/packages/plugin-proxy/test/support/helpers.js b/packages/plugin-proxy/test/support/helpers.js index ffe769275..ccf8633c8 100644 --- a/packages/plugin-proxy/test/support/helpers.js +++ b/packages/plugin-proxy/test/support/helpers.js @@ -84,7 +84,7 @@ const serverUrl = (port) => { return `http://127.0.0.1:${port || SERVER_PORT}`; }; -const fetch = (uri, options = {}) => { +const doFetch = (uri, options = {}) => { const requestOptions = { ...defaultRequestOptions, ...options, @@ -164,7 +164,7 @@ module.exports = { createCore, startExistingCore, startServer, - fetch, + doFetch, TimeCounter, wait, waitForServer, diff --git a/packages/plugin-proxy/test/variantDelay.spec.js b/packages/plugin-proxy/test/variantDelay.spec.js index eeb1cfebe..415c8aaa3 100644 --- a/packages/plugin-proxy/test/variantDelay.spec.js +++ b/packages/plugin-proxy/test/variantDelay.spec.js @@ -1,6 +1,6 @@ const { startServer, - fetch, + doFetch, TimeCounter, waitForServer, waitForHost, @@ -25,7 +25,7 @@ describe("when using delay option in variant", () => { describe("get /users", () => { it("should return users from host after 1 second", async () => { const timeCounter = new TimeCounter(); - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); expect(response.body).toEqual([ @@ -44,7 +44,7 @@ describe("when using delay option in variant", () => { describe("get /users/2", () => { it("should return second user from host after 1 second", async () => { const timeCounter = new TimeCounter(); - const response = await fetch("/api/users/2"); + const response = await doFetch("/api/users/2"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); expect(response.body).toEqual({ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 103908f49..eff8700f1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,7 +32,7 @@ importers: cypress: 9.7.0 cypress-fail-fast: 3.4.1 deepmerge: 4.2.2 - eslint: 8.15.0 + eslint: 8.19.0 eslint-config-prettier: 8.5.0 eslint-plugin-jest: 26.4.6 eslint-plugin-no-only-tests: 2.6.0 @@ -63,21 +63,21 @@ importers: webpack: 5.72.1 devDependencies: '@babel/core': 7.17.10 - '@babel/eslint-parser': 7.17.0_035b3d23fd8620ffc8b252e0ab28a400 + '@babel/eslint-parser': 7.17.0_3c2913dc964bdec7549823e008b23a3d '@babel/preset-env': 7.17.10_@babel+core@7.17.10 '@babel/preset-react': 7.16.7_@babel+core@7.17.10 '@babel/preset-typescript': 7.16.7_@babel+core@7.17.10 '@cypress/webpack-preprocessor': 5.11.1_2b030c791a152f30c4d5c7cb2c1233d2 '@nrwl/cli': 13.8.3 - '@nrwl/eslint-plugin-nx': 13.8.3_8ff38a159b4074fa07fa71ddd1d8c577 + '@nrwl/eslint-plugin-nx': 13.8.3_f39a8c661f5e30fe878d73f8dec389c9 '@nrwl/tao': 13.8.3 - '@nrwl/workspace': 13.8.3_b34b2322de60a64ff6afc92143922fbf + '@nrwl/workspace': 13.8.3_b2ec15dc988d1a9e82de192537d064b3 '@rollup/plugin-babel': 5.3.1_e931bf24091652160a0c732af432d11d '@rollup/plugin-commonjs': 21.1.0_rollup@2.73.0 '@rollup/plugin-node-resolve': 13.3.0_rollup@2.73.0 '@testing-library/cypress': 8.0.2_cypress@9.7.0 - '@typescript-eslint/eslint-plugin': 5.26.0_48ca7bd73a577de4baa676be600aa4d0 - '@typescript-eslint/parser': 5.27.0_eslint@8.15.0+typescript@4.6.4 + '@typescript-eslint/eslint-plugin': 5.26.0_ebf6867741f3398a69467d87ff4602a6 + '@typescript-eslint/parser': 5.27.0_eslint@8.19.0+typescript@4.6.4 babel-jest: 27.5.1_@babel+core@7.17.10 babel-loader: 8.2.5_b1389f604d9ba7860f667cc6c5a95a2a babel-plugin-module-resolver: 4.1.0 @@ -90,11 +90,11 @@ importers: cypress: 9.7.0 cypress-fail-fast: 3.4.1_cypress@9.7.0 deepmerge: 4.2.2 - eslint: 8.15.0 - eslint-config-prettier: 8.5.0_eslint@8.15.0 - eslint-plugin-jest: 26.4.6_4afd50158952fe6648498b887074a9fa + eslint: 8.19.0 + eslint-config-prettier: 8.5.0_eslint@8.19.0 + eslint-plugin-jest: 26.4.6_eb5745a3ff661b59c06237bbf65c0a4d eslint-plugin-no-only-tests: 2.6.0 - eslint-plugin-prettier: 4.0.0_440b30a60bbe5bb6e3ad0057150b2782 + eslint-plugin-prettier: 4.0.0_428a0cccbe1cf97f1433c4a63e9b23a0 fs-extra: 10.1.0 handlebars: 4.7.7 husky: 7.0.4 @@ -493,7 +493,7 @@ packages: transitivePeerDependencies: - supports-color - /@babel/eslint-parser/7.17.0_035b3d23fd8620ffc8b252e0ab28a400: + /@babel/eslint-parser/7.17.0_3c2913dc964bdec7549823e008b23a3d: resolution: {integrity: sha512-PUEJ7ZBXbRkbq3qqM/jZ2nIuakUBqCYc7Qf52Lj7dlZ6zERnqisdHioL0l4wwQZnmskMeasqUNzLBFKs3nylXA==} engines: {node: ^10.13.0 || ^12.13.0 || >=14.0.0} peerDependencies: @@ -501,7 +501,7 @@ packages: eslint: ^7.5.0 || ^8.0.0 dependencies: '@babel/core': 7.17.10 - eslint: 8.15.0 + eslint: 8.19.0 eslint-scope: 5.1.1 eslint-visitor-keys: 2.1.0 semver: 6.3.0 @@ -2072,7 +2072,7 @@ packages: ajv: 6.12.6 debug: 4.3.4 espree: 7.3.1 - globals: 13.12.1 + globals: 13.16.0 ignore: 4.0.6 import-fresh: 3.3.0 js-yaml: 3.14.1 @@ -2082,14 +2082,14 @@ packages: - supports-color dev: true - /@eslint/eslintrc/1.2.3: - resolution: {integrity: sha512-uGo44hIwoLGNyduRpjdEpovcbMdd+Nv7amtmJxnKmI8xj6yd5LncmSwDa5NgX/41lIFJtkjD6YdVfgEzPfJ5UA==} + /@eslint/eslintrc/1.3.0: + resolution: {integrity: sha512-UWW0TMTmk2d7hLcWD1/e2g5HDM/HQ3csaLSqXCfqwh4uNDuNqlaKWXmEsL4Cs41Z0KnILNvwbHAah3C2yt06kw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dependencies: ajv: 6.12.6 debug: 4.3.4 espree: 9.3.2 - globals: 13.12.1 + globals: 13.16.0 ignore: 5.2.0 import-fresh: 3.3.0 js-yaml: 4.1.0 @@ -2234,7 +2234,7 @@ packages: ansi-escapes: 4.3.2 chalk: 4.1.2 exit: 0.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-changed-files: 26.6.2 jest-config: 26.6.3 jest-haste-map: 26.6.2 @@ -2280,7 +2280,7 @@ packages: chalk: 4.1.2 emittery: 0.8.1 exit: 0.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-changed-files: 27.5.1 jest-config: 27.5.1 jest-haste-map: 27.5.1 @@ -2381,7 +2381,7 @@ packages: collect-v8-coverage: 1.0.1 exit: 0.1.2 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 istanbul-lib-coverage: 3.2.0 istanbul-lib-instrument: 4.0.3 istanbul-lib-report: 3.0.0 @@ -2420,7 +2420,7 @@ packages: collect-v8-coverage: 1.0.1 exit: 0.1.2 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 istanbul-lib-coverage: 3.2.0 istanbul-lib-instrument: 4.0.3 istanbul-lib-report: 3.0.0 @@ -2458,7 +2458,7 @@ packages: collect-v8-coverage: 1.0.1 exit: 0.1.2 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 istanbul-lib-coverage: 3.2.0 istanbul-lib-instrument: 5.1.0 istanbul-lib-report: 3.0.0 @@ -2482,7 +2482,7 @@ packages: engines: {node: '>= 10.14.2'} dependencies: callsites: 3.1.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 source-map: 0.6.1 dev: true @@ -2491,7 +2491,7 @@ packages: engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} dependencies: callsites: 3.1.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 source-map: 0.6.1 dev: true @@ -2530,7 +2530,7 @@ packages: engines: {node: '>= 10.14.2'} dependencies: '@jest/test-result': 26.6.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-haste-map: 26.6.2 jest-runner: 26.6.3 jest-runtime: 26.6.3 @@ -2547,7 +2547,7 @@ packages: engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} dependencies: '@jest/test-result': 27.5.1 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-haste-map: 27.5.1 jest-runtime: 27.5.1 transitivePeerDependencies: @@ -2564,7 +2564,7 @@ packages: chalk: 4.1.2 convert-source-map: 1.8.0 fast-json-stable-stringify: 2.1.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-haste-map: 26.6.2 jest-regex-util: 26.0.0 jest-util: 26.6.2 @@ -2704,7 +2704,7 @@ packages: tslib: 2.3.1 dev: true - /@nrwl/eslint-plugin-nx/13.8.3_8ff38a159b4074fa07fa71ddd1d8c577: + /@nrwl/eslint-plugin-nx/13.8.3_f39a8c661f5e30fe878d73f8dec389c9: resolution: {integrity: sha512-fiDGULPfFYFkjIORBCFTkzpUQNlW48/32C3xWVbZlmmnO1KJ5ql3QyQSaDpaentE5nASM8TbgXeVMRqhYkZbOw==} peerDependencies: '@typescript-eslint/parser': ~5.10.0 @@ -2714,13 +2714,13 @@ packages: optional: true dependencies: '@nrwl/devkit': 13.8.3 - '@nrwl/workspace': 13.8.3_b34b2322de60a64ff6afc92143922fbf + '@nrwl/workspace': 13.8.3_b2ec15dc988d1a9e82de192537d064b3 '@swc-node/register': 1.4.2 - '@typescript-eslint/experimental-utils': 5.10.2_eslint@8.15.0+typescript@4.6.4 - '@typescript-eslint/parser': 5.27.0_eslint@8.15.0+typescript@4.6.4 + '@typescript-eslint/experimental-utils': 5.10.2_eslint@8.19.0+typescript@4.6.4 + '@typescript-eslint/parser': 5.27.0_eslint@8.19.0+typescript@4.6.4 chalk: 4.1.0 confusing-browser-globals: 1.0.11 - eslint-config-prettier: 8.5.0_eslint@8.15.0 + eslint-config-prettier: 8.5.0_eslint@8.19.0 tsconfig-paths: 3.12.0 optionalDependencies: '@swc/core-linux-arm64-gnu': 1.2.146 @@ -2762,7 +2762,7 @@ packages: - utf-8-validate dev: true - /@nrwl/linter/13.8.3_eslint@8.15.0+typescript@4.6.4: + /@nrwl/linter/13.8.3_eslint@8.19.0+typescript@4.6.4: resolution: {integrity: sha512-1C60ic0VwHrTPEMbUkDmu5e5hHRL6/XORA6hKiKDAhSYczPX9qNdTHuCaS0paSHRfc0YT4s20/jC/gtqy+UbEA==} peerDependencies: eslint: ^8.0.0 @@ -2773,7 +2773,7 @@ packages: '@nrwl/devkit': 13.8.3 '@nrwl/jest': 13.8.3 '@phenomnomnominal/tsquery': 4.1.1_typescript@4.6.4 - eslint: 8.15.0 + eslint: 8.19.0 tmp: 0.2.1 tslib: 2.3.1 transitivePeerDependencies: @@ -2805,7 +2805,7 @@ packages: yargs-parser: 20.0.0 dev: true - /@nrwl/workspace/13.8.3_b34b2322de60a64ff6afc92143922fbf: + /@nrwl/workspace/13.8.3_b2ec15dc988d1a9e82de192537d064b3: resolution: {integrity: sha512-B2LpbJjxT+WqW0PMxzdy8rmyY0woQytOi4BazPzpcFqaO7zO5/XvLF7txhcvYgRn8n6o0lPsMD0P0Pgeri8oaQ==} peerDependencies: prettier: ^2.5.1 @@ -2816,7 +2816,7 @@ packages: '@nrwl/cli': 13.8.3 '@nrwl/devkit': 13.8.3 '@nrwl/jest': 13.8.3 - '@nrwl/linter': 13.8.3_eslint@8.15.0+typescript@4.6.4 + '@nrwl/linter': 13.8.3_eslint@8.19.0+typescript@4.6.4 '@parcel/watcher': 2.0.4 chalk: 4.1.0 chokidar: 3.5.3 @@ -3665,7 +3665,7 @@ packages: - supports-color dev: true - /@typescript-eslint/eslint-plugin/5.26.0_48ca7bd73a577de4baa676be600aa4d0: + /@typescript-eslint/eslint-plugin/5.26.0_ebf6867741f3398a69467d87ff4602a6: resolution: {integrity: sha512-oGCmo0PqnRZZndr+KwvvAUvD3kNE4AfyoGCwOZpoCncSh4MVD06JTE8XQa2u9u+NX5CsyZMBTEc2C72zx38eYA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -3676,12 +3676,12 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/parser': 5.27.0_eslint@8.15.0+typescript@4.6.4 + '@typescript-eslint/parser': 5.27.0_eslint@8.19.0+typescript@4.6.4 '@typescript-eslint/scope-manager': 5.26.0 - '@typescript-eslint/type-utils': 5.26.0_eslint@8.15.0+typescript@4.6.4 - '@typescript-eslint/utils': 5.26.0_eslint@8.15.0+typescript@4.6.4 + '@typescript-eslint/type-utils': 5.26.0_eslint@8.19.0+typescript@4.6.4 + '@typescript-eslint/utils': 5.26.0_eslint@8.19.0+typescript@4.6.4 debug: 4.3.4 - eslint: 8.15.0 + eslint: 8.19.0 functional-red-black-tree: 1.0.1 ignore: 5.2.0 regexpp: 3.2.0 @@ -3727,14 +3727,14 @@ packages: - typescript dev: true - /@typescript-eslint/experimental-utils/5.10.2_eslint@8.15.0+typescript@4.6.4: + /@typescript-eslint/experimental-utils/5.10.2_eslint@8.19.0+typescript@4.6.4: resolution: {integrity: sha512-stRnIlxDduzxtaVLtEohESoXI1k7J6jvJHGyIkOT2pvXbg5whPM6f9tzJ51bJJxaJTdmvwgVFDNCopFRb2F5Gw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 dependencies: - '@typescript-eslint/utils': 5.10.2_eslint@8.15.0+typescript@4.6.4 - eslint: 8.15.0 + '@typescript-eslint/utils': 5.10.2_eslint@8.19.0+typescript@4.6.4 + eslint: 8.19.0 transitivePeerDependencies: - supports-color - typescript @@ -3760,7 +3760,7 @@ packages: - supports-color dev: true - /@typescript-eslint/parser/5.27.0_eslint@8.15.0+typescript@4.6.4: + /@typescript-eslint/parser/5.27.0_eslint@8.19.0+typescript@4.6.4: resolution: {integrity: sha512-8oGjQF46c52l7fMiPPvX4It3u3V3JipssqDfHQ2hcR0AeR8Zge+OYyKUCm5b70X72N1qXt0qgHenwN6Gc2SXZA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -3774,7 +3774,7 @@ packages: '@typescript-eslint/types': 5.27.0 '@typescript-eslint/typescript-estree': 5.27.0_typescript@4.6.4 debug: 4.3.4 - eslint: 8.15.0 + eslint: 8.19.0 typescript: 4.6.4 transitivePeerDependencies: - supports-color @@ -3812,7 +3812,7 @@ packages: '@typescript-eslint/visitor-keys': 5.27.0 dev: true - /@typescript-eslint/type-utils/5.26.0_eslint@8.15.0+typescript@4.6.4: + /@typescript-eslint/type-utils/5.26.0_eslint@8.19.0+typescript@4.6.4: resolution: {integrity: sha512-7ccbUVWGLmcRDSA1+ADkDBl5fP87EJt0fnijsMFTVHXKGduYMgienC/i3QwoVhDADUAPoytgjbZbCOMj4TY55A==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -3822,9 +3822,9 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/utils': 5.26.0_eslint@8.15.0+typescript@4.6.4 + '@typescript-eslint/utils': 5.26.0_eslint@8.19.0+typescript@4.6.4 debug: 4.3.4 - eslint: 8.15.0 + eslint: 8.19.0 tsutils: 3.21.0_typescript@4.6.4 typescript: 4.6.4 transitivePeerDependencies: @@ -3962,7 +3962,7 @@ packages: - supports-color dev: true - /@typescript-eslint/utils/5.10.2_eslint@8.15.0+typescript@4.6.4: + /@typescript-eslint/utils/5.10.2_eslint@8.19.0+typescript@4.6.4: resolution: {integrity: sha512-vuJaBeig1NnBRkf7q9tgMLREiYD7zsMrsN1DA3wcoMDvr3BTFiIpKjGiYZoKPllfEwN7spUjv7ZqD+JhbVjEPg==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -3972,15 +3972,15 @@ packages: '@typescript-eslint/scope-manager': 5.10.2 '@typescript-eslint/types': 5.10.2 '@typescript-eslint/typescript-estree': 5.10.2_typescript@4.6.4 - eslint: 8.15.0 + eslint: 8.19.0 eslint-scope: 5.1.1 - eslint-utils: 3.0.0_eslint@8.15.0 + eslint-utils: 3.0.0_eslint@8.19.0 transitivePeerDependencies: - supports-color - typescript dev: true - /@typescript-eslint/utils/5.26.0_eslint@8.15.0+typescript@4.6.4: + /@typescript-eslint/utils/5.26.0_eslint@8.19.0+typescript@4.6.4: resolution: {integrity: sha512-PJFwcTq2Pt4AMOKfe3zQOdez6InIDOjUJJD3v3LyEtxHGVVRK3Vo7Dd923t/4M9hSH2q2CLvcTdxlLPjcIk3eg==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -3990,9 +3990,9 @@ packages: '@typescript-eslint/scope-manager': 5.26.0 '@typescript-eslint/types': 5.26.0 '@typescript-eslint/typescript-estree': 5.26.0_typescript@4.6.4 - eslint: 8.15.0 + eslint: 8.19.0 eslint-scope: 5.1.1 - eslint-utils: 3.0.0_eslint@8.15.0 + eslint-utils: 3.0.0_eslint@8.19.0 transitivePeerDependencies: - supports-color - typescript @@ -4341,12 +4341,6 @@ packages: hasBin: true dev: true - /acorn/8.7.0: - resolution: {integrity: sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==} - engines: {node: '>=0.4.0'} - hasBin: true - dev: true - /acorn/8.7.1: resolution: {integrity: sha512-Xx54uLJQZ19lKygFXOWsscKUbsBZW0CPykPhVQdhIeIwrbPmJzqeASDInc8nKBnp/JT6igTs82qPXz069H8I/A==} engines: {node: '>=0.4.0'} @@ -4415,16 +4409,6 @@ packages: json-schema-traverse: 1.0.0 require-from-string: 2.0.2 uri-js: 4.4.1 - dev: false - - /ajv/8.9.0: - resolution: {integrity: sha512-qOKJyNj/h+OWx7s5DePL6Zu1KeM9jPZhwBqs+7DzP6bGOvqzVCSf0xueYmVuaC/oQ/VtS2zLMLHdQFbkka+XDQ==} - dependencies: - fast-deep-equal: 3.1.3 - json-schema-traverse: 1.0.0 - require-from-string: 2.0.2 - uri-js: 4.4.1 - dev: true /alphanum-sort/1.0.2: resolution: {integrity: sha1-l6ERlkmyEa0zaR2fn0hqjsn74KM=} @@ -4819,7 +4803,7 @@ packages: babel-plugin-istanbul: 6.1.1 babel-preset-jest: 26.6.2_@babel+core@7.12.3 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 slash: 3.0.0 transitivePeerDependencies: - supports-color @@ -4838,7 +4822,7 @@ packages: babel-plugin-istanbul: 6.1.1 babel-preset-jest: 26.6.2_@babel+core@7.17.10 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 slash: 3.0.0 transitivePeerDependencies: - supports-color @@ -5532,7 +5516,7 @@ packages: chownr: 1.1.4 figgy-pudding: 3.5.2 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 infer-owner: 1.0.4 lru-cache: 5.1.1 mississippi: 3.0.0 @@ -5625,7 +5609,7 @@ packages: dev: true /callsites/2.0.0: - resolution: {integrity: sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=} + resolution: {integrity: sha512-ksWePWBloaWPxJYQ8TL0JHvtci6G5QTKwQ95RcWAa/lzoAKuAOflGdAK92hpHXjkwb8zLxoLNUoNYZgVsaJzvQ==} engines: {node: '>=4'} dev: true @@ -7243,7 +7227,7 @@ packages: resolution: {integrity: sha512-Nv9m36S/vxpsI+Hc4/ZGRs0n9mXqSWGGq49zxb/cJfPAQMbUtttJAlNPS4AQzaBdw/pKskw5bMbekT/Y7W/Wlg==} engines: {node: '>=6.9.0'} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 memory-fs: 0.5.0 tapable: 1.1.3 dev: true @@ -7252,7 +7236,7 @@ packages: resolution: {integrity: sha512-Bq9VSor+kjvW3f9/MiiR4eE3XYgOl7/rS8lnSxbRbF3kS0B2r+Y9w5krBWxZgDxASVZbdYrn5wT4j/Wb0J9qow==} engines: {node: '>=10.13.0'} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 tapable: 2.2.1 dev: true @@ -7366,7 +7350,7 @@ packages: resolution: {integrity: sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=} /escape-string-regexp/1.0.5: - resolution: {integrity: sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=} + resolution: {integrity: sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==} engines: {node: '>=0.8.0'} /escape-string-regexp/2.0.0: @@ -7392,13 +7376,13 @@ packages: source-map: 0.6.1 dev: true - /eslint-config-prettier/8.5.0_eslint@8.15.0: + /eslint-config-prettier/8.5.0_eslint@8.19.0: resolution: {integrity: sha512-obmWKLUNCnhtQRKc+tmnYuQl0pFU1ibYJQ5BGhTVB08bHe9wC8qUeG7c08dj9XX+AuPj1YSGSQIHl1pnDHZR0Q==} hasBin: true peerDependencies: eslint: '>=7.0.0' dependencies: - eslint: 8.15.0 + eslint: 8.19.0 dev: true /eslint-config-react-app/6.0.0_8b22c17a17a3d81dda5180791a495e4a: @@ -7535,7 +7519,7 @@ packages: - typescript dev: true - /eslint-plugin-jest/26.4.6_4afd50158952fe6648498b887074a9fa: + /eslint-plugin-jest/26.4.6_eb5745a3ff661b59c06237bbf65c0a4d: resolution: {integrity: sha512-R3mq1IepnhtsukHQsWxdyKra3OVwYB+N4k8i45ndqSfr8p9KZV6G+EIUt1Z7hzAh4KlsbXG+nCTlNeGFLFLNvA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -7548,9 +7532,9 @@ packages: jest: optional: true dependencies: - '@typescript-eslint/eslint-plugin': 5.26.0_48ca7bd73a577de4baa676be600aa4d0 - '@typescript-eslint/utils': 5.26.0_eslint@8.15.0+typescript@4.6.4 - eslint: 8.15.0 + '@typescript-eslint/eslint-plugin': 5.26.0_ebf6867741f3398a69467d87ff4602a6 + '@typescript-eslint/utils': 5.26.0_eslint@8.19.0+typescript@4.6.4 + eslint: 8.19.0 jest: 27.5.1 transitivePeerDependencies: - supports-color @@ -7583,7 +7567,7 @@ packages: engines: {node: '>=4.0.0'} dev: true - /eslint-plugin-prettier/4.0.0_440b30a60bbe5bb6e3ad0057150b2782: + /eslint-plugin-prettier/4.0.0_428a0cccbe1cf97f1433c4a63e9b23a0: resolution: {integrity: sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ==} engines: {node: '>=6.0.0'} peerDependencies: @@ -7594,8 +7578,8 @@ packages: eslint-config-prettier: optional: true dependencies: - eslint: 8.15.0 - eslint-config-prettier: 8.5.0_eslint@8.15.0 + eslint: 8.19.0 + eslint-config-prettier: 8.5.0_eslint@8.19.0 prettier: 2.6.2 prettier-linter-helpers: 1.0.0 dev: true @@ -7686,13 +7670,13 @@ packages: eslint-visitor-keys: 2.1.0 dev: true - /eslint-utils/3.0.0_eslint@8.15.0: + /eslint-utils/3.0.0_eslint@8.19.0: resolution: {integrity: sha512-uuQC43IGctw68pJA1RgbQS8/NP7rch6Cwd4j3ZBtgo4/8Flj4eGE7ZYSZRN3iq5pVUv6GPdW5Z1RFleo84uLDA==} engines: {node: ^10.0.0 || ^12.0.0 || >= 14.0.0} peerDependencies: eslint: '>=5' dependencies: - eslint: 8.15.0 + eslint: 8.19.0 eslint-visitor-keys: 2.1.0 dev: true @@ -7753,7 +7737,7 @@ packages: file-entry-cache: 6.0.1 functional-red-black-tree: 1.0.1 glob-parent: 5.1.2 - globals: 13.12.1 + globals: 13.16.0 ignore: 4.0.6 import-fresh: 3.3.0 imurmurhash: 0.1.4 @@ -7777,21 +7761,21 @@ packages: - supports-color dev: true - /eslint/8.15.0: - resolution: {integrity: sha512-GG5USZ1jhCu8HJkzGgeK8/+RGnHaNYZGrGDzUtigK3BsGESW/rs2az23XqE0WVwDxy1VRvvjSSGu5nB0Bu+6SA==} + /eslint/8.19.0: + resolution: {integrity: sha512-SXOPj3x9VKvPe81TjjUJCYlV4oJjQw68Uek+AM0X4p+33dj2HY5bpTZOgnQHcG2eAm1mtCU9uNMnJi7exU/kYw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} hasBin: true dependencies: - '@eslint/eslintrc': 1.2.3 + '@eslint/eslintrc': 1.3.0 '@humanwhocodes/config-array': 0.9.5 ajv: 6.12.6 chalk: 4.1.2 cross-spawn: 7.0.3 - debug: 4.3.3 + debug: 4.3.4 doctrine: 3.0.0 escape-string-regexp: 4.0.0 eslint-scope: 7.1.1 - eslint-utils: 3.0.0_eslint@8.15.0 + eslint-utils: 3.0.0_eslint@8.19.0 eslint-visitor-keys: 3.3.0 espree: 9.3.2 esquery: 1.4.0 @@ -7800,7 +7784,7 @@ packages: file-entry-cache: 6.0.1 functional-red-black-tree: 1.0.1 glob-parent: 6.0.2 - globals: 13.12.1 + globals: 13.16.0 ignore: 5.2.0 import-fresh: 3.3.0 imurmurhash: 0.1.4 @@ -8291,7 +8275,7 @@ packages: dev: true /fast-levenshtein/2.0.6: - resolution: {integrity: sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=} + resolution: {integrity: sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==} dev: true /fast-url-parser/1.1.3: @@ -8621,7 +8605,7 @@ packages: resolution: {integrity: sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==} engines: {node: '>=6 <7 || >=8'} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jsonfile: 4.0.0 universalify: 0.1.2 dev: true @@ -8630,7 +8614,7 @@ packages: resolution: {integrity: sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==} engines: {node: '>=6 <7 || >=8'} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jsonfile: 4.0.0 universalify: 0.1.2 dev: true @@ -8640,7 +8624,7 @@ packages: engines: {node: '>=10'} dependencies: at-least-node: 1.0.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jsonfile: 6.1.0 universalify: 2.0.0 dev: true @@ -8655,7 +8639,7 @@ packages: /fs-write-stream-atomic/1.0.10: resolution: {integrity: sha1-tH31NJPvkR33VzHnCp3tAYnbQMk=} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 iferr: 0.1.5 imurmurhash: 0.1.4 readable-stream: 2.3.7 @@ -8842,8 +8826,8 @@ packages: resolution: {integrity: sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==} engines: {node: '>=4'} - /globals/13.12.1: - resolution: {integrity: sha512-317dFlgY2pdJZ9rspXDks7073GpDmXdfbM3vYYp0HAMKGDh1FfWPleI2ljVNLQX5M5lXcAslTcPTrOrMEFOjyw==} + /globals/13.16.0: + resolution: {integrity: sha512-A1lrQfpNF+McdPOnnFqY3kSN0AFTy485bTi1bkLk4mVPODIUEcSfhHgRqA+QdXPksrSTTztYXx37NFV+GpGk3Q==} engines: {node: '>=8'} dependencies: type-fest: 0.20.2 @@ -8914,7 +8898,6 @@ packages: /graceful-fs/4.2.10: resolution: {integrity: sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==} - dev: false /graceful-fs/4.2.9: resolution: {integrity: sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==} @@ -8971,7 +8954,7 @@ packages: dev: true /has-flag/3.0.0: - resolution: {integrity: sha1-tdRU3CGZriJWmfNGfloH87lVuv0=} + resolution: {integrity: sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==} engines: {node: '>=4'} /has-flag/4.0.0: @@ -9327,7 +9310,7 @@ packages: dev: true /import-fresh/2.0.0: - resolution: {integrity: sha1-2BNVwVYS04bGH53dOSLUMEgipUY=} + resolution: {integrity: sha512-eZ5H8rcgYazHbKC3PG4ClHNykCSxtAhxSSEM+2mb+7evD2CKF5V7c0dNum7AdpDh0ZdICwZY9sRSn8f+KH96sg==} engines: {node: '>=4'} dependencies: caller-path: 2.0.0 @@ -9652,7 +9635,7 @@ packages: dev: true /is-extglob/2.1.1: - resolution: {integrity: sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=} + resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==} engines: {node: '>=0.10.0'} dev: true @@ -10059,7 +10042,7 @@ packages: '@jest/types': 26.6.2 chalk: 4.1.2 exit: 0.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 import-local: 3.1.0 is-ci: 2.0.0 jest-config: 26.6.3 @@ -10090,7 +10073,7 @@ packages: '@jest/types': 27.5.1 chalk: 4.1.2 exit: 0.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 import-local: 3.1.0 jest-config: 27.5.1 jest-util: 27.5.1 @@ -10121,7 +10104,7 @@ packages: chalk: 4.1.2 deepmerge: 4.2.2 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-environment-jsdom: 26.6.2 jest-environment-node: 26.6.2 jest-get-type: 26.3.0 @@ -10155,7 +10138,7 @@ packages: chalk: 4.1.2 deepmerge: 4.2.2 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 is-ci: 3.0.1 jest-circus: 27.5.1 jest-environment-jsdom: 27.5.1 @@ -10193,7 +10176,7 @@ packages: ci-info: 3.3.0 deepmerge: 4.2.2 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-circus: 27.5.1 jest-environment-jsdom: 27.5.1 jest-environment-node: 27.5.1 @@ -10351,7 +10334,7 @@ packages: '@types/node': 17.0.21 anymatch: 3.1.2 fb-watchman: 2.0.1 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-regex-util: 26.0.0 jest-serializer: 26.6.2 jest-util: 26.6.2 @@ -10374,7 +10357,7 @@ packages: '@types/node': 17.0.21 anymatch: 3.1.2 fb-watchman: 2.0.1 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-regex-util: 27.5.1 jest-serializer: 27.5.1 jest-util: 27.5.1 @@ -10484,7 +10467,7 @@ packages: '@jest/types': 26.6.2 '@types/stack-utils': 2.0.1 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 micromatch: 4.0.4 pretty-format: 26.6.2 slash: 3.0.0 @@ -10499,7 +10482,7 @@ packages: '@jest/types': 27.5.1 '@types/stack-utils': 2.0.1 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 micromatch: 4.0.4 pretty-format: 27.5.1 slash: 3.0.0 @@ -10608,7 +10591,7 @@ packages: dependencies: '@jest/types': 26.6.2 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-pnp-resolver: 1.2.2_jest-resolve@26.6.0 jest-util: 26.6.2 read-pkg-up: 7.0.1 @@ -10622,7 +10605,7 @@ packages: dependencies: '@jest/types': 26.6.2 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-pnp-resolver: 1.2.2_jest-resolve@26.6.2 jest-util: 26.6.2 read-pkg-up: 7.0.1 @@ -10637,7 +10620,7 @@ packages: '@jest/types': 27.5.1 chalk: 4.1.2 escalade: 3.1.1 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-haste-map: 27.5.1 jest-pnp-resolver: 1.2.2_jest-resolve@27.2.2 jest-util: 27.5.1 @@ -10652,7 +10635,7 @@ packages: dependencies: '@jest/types': 27.5.1 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-haste-map: 27.5.1 jest-pnp-resolver: 1.2.2_jest-resolve@27.5.1 jest-util: 27.5.1 @@ -10674,7 +10657,7 @@ packages: chalk: 4.1.2 emittery: 0.7.2 exit: 0.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-config: 26.6.3 jest-docblock: 26.0.0 jest-haste-map: 26.6.2 @@ -10706,7 +10689,7 @@ packages: '@types/node': 17.0.21 chalk: 4.1.2 emittery: 0.8.1 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-docblock: 27.5.1 jest-environment-jsdom: 27.5.1 jest-environment-node: 27.5.1 @@ -10745,7 +10728,7 @@ packages: collect-v8-coverage: 1.0.1 exit: 0.1.2 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-config: 26.6.3 jest-haste-map: 26.6.2 jest-message-util: 26.6.2 @@ -10782,7 +10765,7 @@ packages: collect-v8-coverage: 1.0.1 execa: 5.1.1 glob: 7.2.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-haste-map: 27.5.1 jest-message-util: 27.5.1 jest-mock: 27.5.1 @@ -10801,7 +10784,7 @@ packages: engines: {node: '>= 10.14.2'} dependencies: '@types/node': 17.0.21 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 dev: true /jest-serializer/27.5.1: @@ -10809,7 +10792,7 @@ packages: engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0} dependencies: '@types/node': 17.0.21 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 dev: true /jest-snapshot/26.6.2: @@ -10822,7 +10805,7 @@ packages: '@types/prettier': 2.4.4 chalk: 4.1.2 expect: 26.6.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-diff: 26.6.2 jest-get-type: 26.3.0 jest-haste-map: 26.6.2 @@ -10852,7 +10835,7 @@ packages: babel-preset-current-node-syntax: 1.0.1_@babel+core@7.17.10 chalk: 4.1.2 expect: 27.5.1 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 jest-diff: 27.5.1 jest-get-type: 27.5.1 jest-haste-map: 27.5.1 @@ -10873,7 +10856,7 @@ packages: '@jest/types': 26.6.2 '@types/node': 17.0.21 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 is-ci: 2.0.0 micromatch: 4.0.4 dev: true @@ -10885,7 +10868,7 @@ packages: '@jest/types': 27.5.1 '@types/node': 17.0.21 chalk: 4.1.2 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 is-ci: 3.0.1 picomatch: 2.3.1 dev: true @@ -10898,7 +10881,7 @@ packages: '@types/node': 17.0.21 chalk: 4.1.2 ci-info: 3.3.0 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 picomatch: 2.3.1 dev: true @@ -11138,7 +11121,7 @@ packages: dev: true /json-stable-stringify-without-jsonify/1.0.1: - resolution: {integrity: sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=} + resolution: {integrity: sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==} dev: true /json-stringify-safe/5.0.1: @@ -11169,7 +11152,7 @@ packages: /jsonfile/4.0.0: resolution: {integrity: sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=} optionalDependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 dev: true /jsonfile/6.1.0: @@ -11177,7 +11160,7 @@ packages: dependencies: universalify: 2.0.0 optionalDependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 /jsonpointer/5.0.0: resolution: {integrity: sha512-PNYZIdMjVIvVgDSYKTT63Y+KZ6IZvGRNNWcxwD+GNnUz1MKPfv30J8ueCjdwcN0nDx2SlshgyB7Oy0epAzVRRg==} @@ -11297,7 +11280,7 @@ packages: engines: {node: '>=6'} /levn/0.3.0: - resolution: {integrity: sha1-OwmSTt+fCDwEkP3UwLxEIeBHZO4=} + resolution: {integrity: sha512-0OO4y2iOHix2W6ujICbKIaEQXvFQHue65vUG3pb5EUomzPI90z9hsA1VsO/dbIIpC53J8gxM9Q4Oho0jrCM/yA==} engines: {node: '>= 0.8.0'} dependencies: prelude-ls: 1.1.2 @@ -11979,7 +11962,7 @@ packages: dev: true /natural-compare/1.4.0: - resolution: {integrity: sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=} + resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==} dev: true /negotiator/0.6.3: @@ -13418,7 +13401,7 @@ packages: dev: true /prelude-ls/1.1.2: - resolution: {integrity: sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=} + resolution: {integrity: sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w==} engines: {node: '>= 0.8.0'} dev: true @@ -13595,7 +13578,7 @@ packages: dev: true /punycode/1.3.2: - resolution: {integrity: sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=} + resolution: {integrity: sha512-RofWgt/7fL5wP1Y7fxE7/EmTLzQVnB0ycyibJ0OOHIlJqTNzglYFxVwETOcIoJqJmpDXJ9xImDv+Fq34F/d4Dw==} dev: true /punycode/2.1.1: @@ -14000,7 +13983,7 @@ packages: resolution: {integrity: sha512-1JU/8q+VgFZyxwrJ+SVIOsh+KywWGpds3NTqikiKpDMZWScmAYyKIgqkO+ARvNWJfXeXR1zxz7aHF4u4CyH6vQ==} engines: {node: '>=0.10'} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 micromatch: 3.1.10 readable-stream: 2.3.7 transitivePeerDependencies: @@ -14012,7 +13995,7 @@ packages: resolution: {integrity: sha512-1JU/8q+VgFZyxwrJ+SVIOsh+KywWGpds3NTqikiKpDMZWScmAYyKIgqkO+ARvNWJfXeXR1zxz7aHF4u4CyH6vQ==} engines: {node: '>=0.10'} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 micromatch: 3.1.10_supports-color@6.1.0 readable-stream: 2.3.7 transitivePeerDependencies: @@ -14264,7 +14247,7 @@ packages: dev: true /resolve-from/3.0.0: - resolution: {integrity: sha1-six699nWiBvItuZTM17rywoYh0g=} + resolution: {integrity: sha512-GnlH6vxLymXJNMBo7XP1fJIzBFbdYt49CuTwmB/6N53t+kMPRMFKz783LlQ4tv28XoQfMWinAJX6WCGf2IlaIw==} engines: {node: '>=4'} dev: true @@ -15377,7 +15360,7 @@ packages: dev: true /strip-ansi/3.0.1: - resolution: {integrity: sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=} + resolution: {integrity: sha512-VhumSSbBqDTP8p2ZLKj40UjBCV4+v8bUSEpUb4KjRgWk9pbqGF4REFj6KEagidb2f/M6AzC0EmFyDNGaw9OCzg==} engines: {node: '>=0.10.0'} dependencies: ansi-regex: 2.1.1 @@ -15543,7 +15526,7 @@ packages: resolution: {integrity: sha512-s/fitrbVeEyHKFa7mFdkuQMWlH1Wgw/yEXMt5xACT4ZpzWFluehAxRtUUQKPuWhaLAWhFcVx6w3oC8VKaUfPGA==} engines: {node: '>=10.0.0'} dependencies: - ajv: 8.9.0 + ajv: 8.11.0 lodash.truncate: 4.4.2 slice-ansi: 4.0.0 string-width: 4.2.3 @@ -15672,7 +15655,7 @@ packages: engines: {node: '>=10'} hasBin: true dependencies: - acorn: 8.7.0 + acorn: 8.7.1 commander: 2.20.3 source-map: 0.7.3 source-map-support: 0.5.21 @@ -15692,7 +15675,7 @@ packages: dev: false /text-table/0.2.0: - resolution: {integrity: sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=} + resolution: {integrity: sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==} dev: true /throat/5.0.0: @@ -15890,7 +15873,7 @@ packages: dev: true /type-check/0.3.2: - resolution: {integrity: sha1-WITKtRLPHTVeP7eE8wgEsrUg23I=} + resolution: {integrity: sha512-ZCmOJdvOWDBYJlzAoFkC+Q0+bUyEOS1ltgp1MGU03fqHG+dbi9tBFU2Rd9QKiDZFAYrhPh2JUf7rZRIuHRKtOg==} engines: {node: '>= 0.8.0'} dependencies: prelude-ls: 1.1.2 @@ -16318,7 +16301,7 @@ packages: /watchpack/1.7.5: resolution: {integrity: sha512-9P3MWk6SrKjHsGkLT2KHXdQ/9SNkyoJbabxnKOoJepsvJjJG8uYTR3yTPxPQvNDI3w4Nz1xnE0TLHK4RIVe/MQ==} dependencies: - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 neo-async: 2.6.2 optionalDependencies: chokidar: 3.5.3 @@ -16332,7 +16315,7 @@ packages: engines: {node: '>=10.13.0'} dependencies: glob-to-regexp: 0.4.1 - graceful-fs: 4.2.9 + graceful-fs: 4.2.10 dev: true /wbuf/1.7.3: diff --git a/test/core-e2e-legacy/src/config-file-with-custom-route-handler.spec.js b/test/core-e2e-legacy/src/config-file-with-custom-route-handler.spec.js index edaf3501d..8b31283b2 100644 --- a/test/core-e2e-legacy/src/config-file-with-custom-route-handler.spec.js +++ b/test/core-e2e-legacy/src/config-file-with-custom-route-handler.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, removeConfigFile, @@ -37,7 +37,7 @@ describe("when adding route handlers in config file", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e-legacy/src/config-file-with-plugins.spec.js b/test/core-e2e-legacy/src/config-file-with-plugins.spec.js index bf653bd9f..5a299fd26 100644 --- a/test/core-e2e-legacy/src/config-file-with-plugins.spec.js +++ b/test/core-e2e-legacy/src/config-file-with-plugins.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, wait, @@ -53,7 +53,7 @@ describe("when adding plugins in config file", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -61,19 +61,19 @@ describe("when adding plugins in config file", () => { }); it("should serve user 2 in /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 in /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should reload mocks when files are modified", async () => { await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); await wait(4000); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, diff --git a/test/core-e2e-legacy/src/config-file.spec.js b/test/core-e2e-legacy/src/config-file.spec.js index f3ef8c922..018366bbc 100644 --- a/test/core-e2e-legacy/src/config-file.spec.js +++ b/test/core-e2e-legacy/src/config-file.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, wait, @@ -42,7 +42,7 @@ describe("when using config file", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -50,19 +50,19 @@ describe("when using config file", () => { }); it("should serve user 2 in /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 in /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should have watch disabled", async () => { await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); await wait(4000); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e-legacy/src/cors-argument.spec.js b/test/core-e2e-legacy/src/cors-argument.spec.js index ad5137dee..78ade33be 100644 --- a/test/core-e2e-legacy/src/cors-argument.spec.js +++ b/test/core-e2e-legacy/src/cors-argument.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServer, removeConfigFile } = require("./support/helpers"); describe("cors command line argument", () => { let cli; @@ -25,7 +25,7 @@ describe("cors command line argument", () => { }); it("cors middleware should handle OPTIONS requests", async () => { - const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + const users = await doFetch(`/api/users/1`, { method: "OPTIONS" }); expect(users.status).toEqual(204); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); expect(users.headers.get("access-control-allow-methods")).toEqual( @@ -35,7 +35,7 @@ describe("cors command line argument", () => { }); it("cors middleware should handle OPTIONS requests to all paths without route", async () => { - const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + const users = await doFetch(`/api/foo`, { method: "OPTIONS" }); expect(users.status).toEqual(204); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); expect(users.headers.get("access-control-allow-methods")).toEqual( @@ -45,7 +45,7 @@ describe("cors command line argument", () => { }); it("Response headers should include cors headers", async () => { - const users = await fetch(`/api/users/1`); + const users = await doFetch(`/api/users/1`); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); }); }); @@ -61,7 +61,7 @@ describe("cors command line argument", () => { }); it("route middleware should handle OPTIONS request", async () => { - const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + const users = await doFetch(`/api/users/1`, { method: "OPTIONS" }); expect(users.status).toEqual(200); expect(users.headers.get("access-control-allow-origin")).toEqual(null); expect(users.headers.get("access-control-allow-methods")).toEqual(null); @@ -69,7 +69,7 @@ describe("cors command line argument", () => { }); it("cors middleware should not handle OPTIONS requests to paths without route", async () => { - const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + const users = await doFetch(`/api/foo`, { method: "OPTIONS" }); expect(users.status).toEqual(404); expect(users.headers.get("access-control-allow-origin")).toEqual(null); expect(users.headers.get("access-control-allow-methods")).toEqual(null); @@ -77,7 +77,7 @@ describe("cors command line argument", () => { }); it("Response headers should not include cors headers", async () => { - const users = await fetch(`/api/users/1`); + const users = await doFetch(`/api/users/1`); expect(users.headers.get("access-control-allow-origin")).toEqual(null); }); }); @@ -96,7 +96,7 @@ describe("cors command line argument", () => { }); it("route middleware should handle OPTIONS request", async () => { - const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + const users = await doFetch(`/api/users/1`, { method: "OPTIONS" }); expect(users.status).toEqual(200); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); expect(users.headers.get("access-control-allow-methods")).toEqual( @@ -106,7 +106,7 @@ describe("cors command line argument", () => { }); it("cors middleware should not handle OPTIONS requests to paths without route", async () => { - const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + const users = await doFetch(`/api/foo`, { method: "OPTIONS" }); expect(users.status).toEqual(404); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); expect(users.headers.get("access-control-allow-methods")).toEqual( @@ -116,7 +116,7 @@ describe("cors command line argument", () => { }); it("Response headers should include cors headers", async () => { - const users = await fetch(`/api/users/1`); + const users = await doFetch(`/api/users/1`); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); }); }); diff --git a/test/core-e2e-legacy/src/custom-router.spec.js b/test/core-e2e-legacy/src/custom-router.spec.js index 5b6a3314c..abc7275ba 100644 --- a/test/core-e2e-legacy/src/custom-router.spec.js +++ b/test/core-e2e-legacy/src/custom-router.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const express = require("express"); const Core = require("@mocks-server/core"); -const { fetch, fixturesFolder, waitForServer, removeConfigFile } = require("./support/helpers"); +const { doFetch, fixturesFolder, waitForServer, removeConfigFile } = require("./support/helpers"); describe("when using custom router", () => { const customRouter = express.Router(); @@ -49,12 +49,12 @@ describe("when using custom router", () => { }); it("custom router should be listening", async () => { - const response = await fetch("/api/custom"); + const response = await doFetch("/api/custom"); expect(response.body.customRouterListening).toEqual(true); }); it("fixtures routers should be listening", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -63,7 +63,7 @@ describe("when using custom router", () => { it("custom router should stop listening when is removed", async () => { await core.removeRouter("/api/custom", customRouter); - const response = await fetch("/api/custom"); + const response = await doFetch("/api/custom"); expect(response.status).toEqual(404); }); }); @@ -93,12 +93,12 @@ describe("when using custom router", () => { }); it("custom router should be listening", async () => { - const response = await fetch("/api/custom"); + const response = await doFetch("/api/custom"); expect(response.body.customRouterListening).toEqual(true); }); it("mocks routers should be listening", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -107,7 +107,7 @@ describe("when using custom router", () => { it("custom router should stop listening when is removed", async () => { await core.removeRouter("/api/custom", customRouter); - const response = await fetch("/api/custom"); + const response = await doFetch("/api/custom"); expect(response.status).toEqual(404); }); }); diff --git a/test/core-e2e-legacy/src/delay-argument.spec.js b/test/core-e2e-legacy/src/delay-argument.spec.js index 167798f9f..fd1675144 100644 --- a/test/core-e2e-legacy/src/delay-argument.spec.js +++ b/test/core-e2e-legacy/src/delay-argument.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServer, TimeCounter, removeConfigFile, @@ -32,7 +32,7 @@ describe("delay argument", () => { it("should set delay", async () => { expect.assertions(2); const timeCounter = new TimeCounter(); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); timeCounter.stop(); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, diff --git a/test/core-e2e-legacy/src/delay-setting.spec.js b/test/core-e2e-legacy/src/delay-setting.spec.js index f63de8015..9342b3a82 100644 --- a/test/core-e2e-legacy/src/delay-setting.spec.js +++ b/test/core-e2e-legacy/src/delay-setting.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, TimeCounter, waitForServer, removeConfigFile, @@ -32,7 +32,7 @@ describe("delay setting", () => { describe("When started", () => { it("should respond with no delay", async () => { const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeLessThan(400); }); @@ -42,7 +42,7 @@ describe("delay setting", () => { it("should respond after defined delay", async () => { core.config.namespace("mocks").option("delay").value = 1000; const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); }); @@ -52,7 +52,7 @@ describe("delay setting", () => { it("should respond after route variant defined delay", async () => { core.mocks.useRouteVariant("get-users:delayed"); const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); }); @@ -60,7 +60,7 @@ describe("delay setting", () => { it("should respond with same delay after setting delay to zero", async () => { core.config.namespace("mocks").option("delay").value = 0; const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); }); @@ -68,7 +68,7 @@ describe("delay setting", () => { it("should respond with same delay after setting delay to 4000", async () => { core.config.namespace("mocks").option("delay").value = 4000; const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); }); @@ -77,7 +77,7 @@ describe("delay setting", () => { describe("When route has delay", () => { it("should respond after route defined delay", async () => { const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); }); @@ -85,7 +85,7 @@ describe("delay setting", () => { it("should respond with same delay after setting delay to zero", async () => { core.config.namespace("mocks").option("delay").value = 0; const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); }); @@ -95,7 +95,7 @@ describe("delay setting", () => { it("should respond after route variant defined delay", async () => { core.config.namespace("mocks").option("selected").value = "user-2"; const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); }); @@ -109,7 +109,7 @@ describe("delay setting", () => { it("should respond with no delay", async () => { core.mocks.useRouteVariant("get-user:zero-delay"); const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeLessThan(500); }); @@ -140,7 +140,7 @@ describe("delay setting", () => { core.config.namespace("mocks").option("delay").value = 3000; core.mocks.useRouteVariant("get-user:null-delay"); const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(2999); }); diff --git a/test/core-e2e-legacy/src/esm.spec.js b/test/core-e2e-legacy/src/esm.spec.js index 99f3c2bf0..606aa0517 100644 --- a/test/core-e2e-legacy/src/esm.spec.js +++ b/test/core-e2e-legacy/src/esm.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, wait, @@ -43,7 +43,7 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -51,7 +51,7 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("middleware should trace request and add headers", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.headers.get("x-mocks-server-example")).toEqual("custom-header"); expect(mocks.logs.current).toEqual( expect.stringContaining( @@ -61,19 +61,19 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("should serve user 1 in /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 in /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should reload mocks when files are modified", async () => { await fsExtra.copy(fixturesFolder("esm-modified"), fixturesFolder("temp")); await wait(4000); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -81,7 +81,7 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("should serve new users route", async () => { - const users = await fetch("/api/new-users"); + const users = await doFetch("/api/new-users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -90,7 +90,7 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("middleware should be disabled", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); }); }); diff --git a/test/core-e2e-legacy/src/events.spec.js b/test/core-e2e-legacy/src/events.spec.js index aea9f5d06..abc8f157a 100644 --- a/test/core-e2e-legacy/src/events.spec.js +++ b/test/core-e2e-legacy/src/events.spec.js @@ -12,7 +12,7 @@ const sinon = require("sinon"); const { startCore, - fetch, + doFetch, waitForServer, wait, fixturesFolder, @@ -44,7 +44,7 @@ describe("events", () => { describe("When started", () => { it("should load web-tutorial mocks", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e-legacy/src/json-files.spec.js b/test/core-e2e-legacy/src/json-files.spec.js index 267cdee84..f9b5f7f0d 100644 --- a/test/core-e2e-legacy/src/json-files.spec.js +++ b/test/core-e2e-legacy/src/json-files.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, findAlert, removeConfigFile, @@ -40,7 +40,7 @@ describe("json files", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -49,13 +49,13 @@ describe("json files", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -71,7 +71,7 @@ describe("json files", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -80,13 +80,13 @@ describe("json files", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -105,7 +105,7 @@ describe("json files", () => { // if mock not exists, it uses the first one found it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -114,13 +114,13 @@ describe("json files", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); diff --git a/test/core-e2e-legacy/src/load-mocks-and-routes.spec.js b/test/core-e2e-legacy/src/load-mocks-and-routes.spec.js index 98292dd30..aaef20f24 100644 --- a/test/core-e2e-legacy/src/load-mocks-and-routes.spec.js +++ b/test/core-e2e-legacy/src/load-mocks-and-routes.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, waitForServerUrl, removeConfigFile, @@ -42,7 +42,7 @@ describe("loadMocks and loadRoutes methods", () => { describe("When started", () => { it("should return users", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -115,13 +115,13 @@ describe("loadMocks and loadRoutes methods", () => { core.config.namespace("mocks").option("selected").value = "users-and-library"; await waitForServerUrl("/api/books"); - const books = await fetch("/api/books"); + const books = await doFetch("/api/books"); expect(books.body).toEqual([{ id: 1, title: "1984" }]); - const authors = await fetch("/api/authors"); + const authors = await doFetch("/api/authors"); expect(authors.body).toEqual([{ id: 1, name: "George Orwell" }]); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -130,27 +130,27 @@ describe("loadMocks and loadRoutes methods", () => { it("should be able to change to a new mock", async () => { await changeMockAndWait("authors-error"); - const books = await fetch("/api/books"); + const books = await doFetch("/api/books"); expect(books.body).toEqual([{ id: 1, title: "1984" }]); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, ]); - const authors = await fetch("/api/authors"); + const authors = await doFetch("/api/authors"); expect(authors.status).toEqual(403); }); it("should keep mocks loaded from files", async () => { await changeMockAndWait("base"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, ]); - const authors = await fetch("/api/authors"); + const authors = await doFetch("/api/authors"); expect(authors.status).toEqual(404); }); }); diff --git a/test/core-e2e-legacy/src/mock-argument.spec.js b/test/core-e2e-legacy/src/mock-argument.spec.js index 537beb2ad..bb51c3b2f 100644 --- a/test/core-e2e-legacy/src/mock-argument.spec.js +++ b/test/core-e2e-legacy/src/mock-argument.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServer, removeConfigFile } = require("./support/helpers"); describe("mock argument", () => { const PATH_OPTION = "--files.path=web-tutorial"; @@ -23,7 +23,7 @@ describe("mock argument", () => { it("should set as current mock the first one found", async () => { mocks = mocksRunner([PATH_OPTION]); await waitForServer(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); @@ -32,7 +32,7 @@ describe("mock argument", () => { it("should set current behavior", async () => { mocks = mocksRunner([PATH_OPTION, "--mocks.selected=user-real"]); await waitForServer(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); @@ -47,7 +47,7 @@ describe("mock argument", () => { it("should set as current mock the first one found", async () => { mocks = mocksRunner([PATH_OPTION, "--mocks.selected=foo"]); await waitForServer(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); diff --git a/test/core-e2e-legacy/src/mock-setting.spec.js b/test/core-e2e-legacy/src/mock-setting.spec.js index 3c41ee9bf..26a71acb8 100644 --- a/test/core-e2e-legacy/src/mock-setting.spec.js +++ b/test/core-e2e-legacy/src/mock-setting.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, findAlert, removeConfigFile, @@ -40,7 +40,7 @@ describe("mock setting", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -49,13 +49,13 @@ describe("mock setting", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -71,7 +71,7 @@ describe("mock setting", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -80,13 +80,13 @@ describe("mock setting", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -98,7 +98,7 @@ describe("mock setting", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -107,19 +107,19 @@ describe("mock setting", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return not found for /api/users/3 path", async () => { - const users = await fetch("/api/users/3"); + const users = await doFetch("/api/users/3"); expect(users.status).toEqual(404); }); }); @@ -137,7 +137,7 @@ describe("mock setting", () => { // if mock not exists, it uses the first one found it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -146,13 +146,13 @@ describe("mock setting", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); diff --git a/test/core-e2e-legacy/src/multi-methods.spec.js b/test/core-e2e-legacy/src/multi-methods.spec.js index e16238501..6383630eb 100644 --- a/test/core-e2e-legacy/src/multi-methods.spec.js +++ b/test/core-e2e-legacy/src/multi-methods.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startCore, waitForServer, fetch, removeConfigFile } = require("./support/helpers"); +const { startCore, waitForServer, doFetch, removeConfigFile } = require("./support/helpers"); describe("when method is defined as array", () => { let requestNumber = 1; @@ -146,7 +146,7 @@ describe("when method is defined as array", () => { describe("base mock", () => { it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users?req=1"); + const users = await doFetch("/api/users?req=1"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -156,7 +156,7 @@ describe("when method is defined as array", () => { const testMethod = (method, id, expectBody = true) => { it(`should serve user 1 under the /api/users/${id} path with ${method} method`, async () => { - const users = await fetch(`/api/users/${id}?req=${requestNumber}`, { method }); + const users = await doFetch(`/api/users/${id}?req=${requestNumber}`, { method }); requestNumber++; expect(users.status).toEqual(200); if (expectBody) { @@ -186,7 +186,7 @@ describe("when method is defined as array", () => { describe('when using route variant "get-user:2"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("get-user:2"); - const users = await fetch("/api/users?req=10"); + const users = await doFetch("/api/users?req=10"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -196,7 +196,7 @@ describe("when method is defined as array", () => { const testMethod = (method, id, expectBody = true) => { it(`should serve user 2 under the /api/users/${id} path with ${method} method`, async () => { - const users = await fetch(`/api/users/${id}?req=${requestNumber}`, { method }); + const users = await doFetch(`/api/users/${id}?req=${requestNumber}`, { method }); requestNumber++; expect(users.status).toEqual(200); if (expectBody) { diff --git a/test/core-e2e-legacy/src/path-argument.spec.js b/test/core-e2e-legacy/src/path-argument.spec.js index 9855805b9..c2e2720a0 100644 --- a/test/core-e2e-legacy/src/path-argument.spec.js +++ b/test/core-e2e-legacy/src/path-argument.spec.js @@ -12,7 +12,7 @@ const path = require("path"); const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, fixturesFolder, waitForServer, removeConfigFile, @@ -60,7 +60,7 @@ describe("path argument", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, diff --git a/test/core-e2e-legacy/src/path-not-exists.spec.js b/test/core-e2e-legacy/src/path-not-exists.spec.js index c1061ec53..f7571f1dc 100644 --- a/test/core-e2e-legacy/src/path-not-exists.spec.js +++ b/test/core-e2e-legacy/src/path-not-exists.spec.js @@ -12,7 +12,7 @@ const path = require("path"); const fsExtra = require("fs-extra"); const { startCore, - fetch, + doFetch, fixturesFolder, waitForServer, findAlert, @@ -69,7 +69,7 @@ describe("when path not exists", () => { describe("base mock", () => { it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); expect(users.body).toEqual([ @@ -79,14 +79,14 @@ describe("when path not exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); @@ -96,7 +96,7 @@ describe("when path not exists", () => { describe("no-headers mock", () => { it("should not add headers to /api/users", async () => { changeMock("no-headers"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.body).toEqual([ @@ -106,14 +106,14 @@ describe("when path not exists", () => { }); it("should not add headers to /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should not add headers to /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); @@ -123,7 +123,7 @@ describe("when path not exists", () => { describe("user-real mock", () => { it("should serve users under the /api/users path", async () => { changeMock("user-real"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.body).toEqual([ @@ -133,14 +133,14 @@ describe("when path not exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); diff --git a/test/core-e2e-legacy/src/path-setting.spec.js b/test/core-e2e-legacy/src/path-setting.spec.js index b09ed9723..d2e909d93 100644 --- a/test/core-e2e-legacy/src/path-setting.spec.js +++ b/test/core-e2e-legacy/src/path-setting.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, waitForServerUrl, fixturesFolder, @@ -35,7 +35,7 @@ describe("path setting", () => { describe("When started", () => { it("should load web-tutorial mocks", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -48,14 +48,14 @@ describe("path setting", () => { changePath(fixturesFolder("web-tutorial-modified")); await waitForServerUrl("/api/new-users"); - const users = await fetch("/api/new-users"); + const users = await doFetch("/api/new-users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, { id: 3, name: "Brand new user" }, ]); - const oldUsers = await fetch("/api/users"); + const oldUsers = await doFetch("/api/users"); expect(oldUsers.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, diff --git a/test/core-e2e-legacy/src/plugins-legacy-alerts.spec.js b/test/core-e2e-legacy/src/plugins-legacy-alerts.spec.js index 568c862ef..0140e4159 100644 --- a/test/core-e2e-legacy/src/plugins-legacy-alerts.spec.js +++ b/test/core-e2e-legacy/src/plugins-legacy-alerts.spec.js @@ -19,7 +19,7 @@ const filterPluginAlerts = (alerts) => const { startCore, - fetch, + doFetch, fixturesFolder, wait, TimeCounter, @@ -77,7 +77,7 @@ describe("plugins using legacy alerts", () => { describe("when started", () => { it("should start server and send responses", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -117,7 +117,7 @@ describe("plugins using legacy alerts", () => { }); it("should respond to custom routes", async () => { - const response = await fetch("/foo-path"); + const response = await doFetch("/foo-path"); expect(response.body).toEqual(FOO_CUSTOM_RESPONSE); }); @@ -206,7 +206,7 @@ describe("plugins using legacy alerts", () => { core = await startCore("web-tutorial", { plugins: { register: [pluginConstructor] }, }); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e-legacy/src/plugins-legacy-formats.spec.js b/test/core-e2e-legacy/src/plugins-legacy-formats.spec.js index 0c8620a20..f6015baf8 100644 --- a/test/core-e2e-legacy/src/plugins-legacy-formats.spec.js +++ b/test/core-e2e-legacy/src/plugins-legacy-formats.spec.js @@ -21,7 +21,7 @@ const filterLogs = (logs, text) => logs.filter((log) => log.includes(text)); const { startCore, - fetch, + doFetch, fixturesFolder, wait, TimeCounter, @@ -80,7 +80,7 @@ describe("plugins", () => { describe("when started", () => { it("should start server and send responses", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -130,7 +130,7 @@ describe("plugins", () => { }); it("should respond to custom routes", async () => { - const response = await fetch("/foo-path"); + const response = await doFetch("/foo-path"); expect(response.body).toEqual(FOO_CUSTOM_RESPONSE); }); @@ -191,7 +191,7 @@ describe("plugins", () => { core = await startCore("web-tutorial", { plugins: { register: [pluginConstructor] }, }); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e-legacy/src/plugins.spec.js b/test/core-e2e-legacy/src/plugins.spec.js index b691fe344..22f931907 100644 --- a/test/core-e2e-legacy/src/plugins.spec.js +++ b/test/core-e2e-legacy/src/plugins.spec.js @@ -18,7 +18,7 @@ const filterLogs = (logs, text) => logs.filter((log) => log.includes(text)); const { startCore, - fetch, + doFetch, fixturesFolder, wait, TimeCounter, @@ -77,7 +77,7 @@ describe("plugins", () => { describe("when started", () => { it("should start server and send responses", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -127,7 +127,7 @@ describe("plugins", () => { }); it("should respond to custom routes", async () => { - const response = await fetch("/foo-path"); + const response = await doFetch("/foo-path"); expect(response.body).toEqual(FOO_CUSTOM_RESPONSE); }); @@ -188,7 +188,7 @@ describe("plugins", () => { core = await startCore("web-tutorial", { plugins: { register: [pluginConstructor] }, }); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e-legacy/src/port-argument.spec.js b/test/core-e2e-legacy/src/port-argument.spec.js index afbc0b1d5..32091e2d6 100644 --- a/test/core-e2e-legacy/src/port-argument.spec.js +++ b/test/core-e2e-legacy/src/port-argument.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServer, removeConfigFile } = require("./support/helpers"); describe("port command line argument", () => { let cli; @@ -24,7 +24,7 @@ describe("port command line argument", () => { }); it("should set server port", async () => { - const users = await fetch("/api/users", { + const users = await doFetch("/api/users", { port: 3005, }); expect(users.body).toEqual([ diff --git a/test/core-e2e-legacy/src/port-setting.spec.js b/test/core-e2e-legacy/src/port-setting.spec.js index ec4897ff8..7a049c73a 100644 --- a/test/core-e2e-legacy/src/port-setting.spec.js +++ b/test/core-e2e-legacy/src/port-setting.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startCore, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); +const { startCore, doFetch, waitForServer, removeConfigFile } = require("./support/helpers"); describe("port setting", () => { let core; @@ -25,7 +25,7 @@ describe("port setting", () => { describe("When started", () => { it("should be listening on port 3100", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -37,7 +37,7 @@ describe("port setting", () => { it("should be listening on new port", async () => { core.config.namespace("server").option("port").value = 3005; await waitForServer(3005); - const users = await fetch("/api/users", { + const users = await doFetch("/api/users", { port: 3005, }); expect(users.body).toEqual([ diff --git a/test/core-e2e-legacy/src/route-handler.spec.js b/test/core-e2e-legacy/src/route-handler.spec.js index 8952e2b8e..3d3b63c32 100644 --- a/test/core-e2e-legacy/src/route-handler.spec.js +++ b/test/core-e2e-legacy/src/route-handler.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { createCore, startExistingCore, - fetch, + doFetch, fixturesFolder, findTrace, removeConfigFile, @@ -38,7 +38,7 @@ describe("when adding route handlers", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e-legacy/src/route-variants.spec.js b/test/core-e2e-legacy/src/route-variants.spec.js index 2a7b526d1..7f0965f95 100644 --- a/test/core-e2e-legacy/src/route-variants.spec.js +++ b/test/core-e2e-legacy/src/route-variants.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, findTrace, removeConfigFile, @@ -33,7 +33,7 @@ describe("route variants", () => { describe("base mock", () => { it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users?req=1"); + const users = await doFetch("/api/users?req=1"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -48,7 +48,7 @@ describe("route variants", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1?req=2"); + const users = await doFetch("/api/users/1?req=2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -60,7 +60,7 @@ describe("route variants", () => { }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=3"); + const users = await doFetch("/api/users/2?req=3"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -75,7 +75,7 @@ describe("route variants", () => { describe('when using route variant "get-user:2"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("get-user:2"); - const users = await fetch("/api/users?req=4"); + const users = await doFetch("/api/users?req=4"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -90,7 +90,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1?req=5"); + const users = await doFetch("/api/users/1?req=5"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -102,7 +102,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=6"); + const users = await doFetch("/api/users/2?req=6"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -117,7 +117,7 @@ describe("route variants", () => { describe('when using route variant "tracer:disabled"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("tracer:disabled"); - const users = await fetch("/api/users?req=7"); + const users = await doFetch("/api/users?req=7"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -131,7 +131,7 @@ describe("route variants", () => { it("should serve user 2 under the /api/users/1 path", async () => { core.mocks.useRouteVariant("get-user:2"); - const users = await fetch("/api/users/1?req=8"); + const users = await doFetch("/api/users/1?req=8"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -141,7 +141,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=9"); + const users = await doFetch("/api/users/2?req=9"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -154,7 +154,7 @@ describe("route variants", () => { describe('when using route variant "tracer:enabled"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("tracer:enabled"); - const users = await fetch("/api/users?req=10"); + const users = await doFetch("/api/users?req=10"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -169,7 +169,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1?req=11"); + const users = await doFetch("/api/users/1?req=11"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -181,7 +181,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=12"); + const users = await doFetch("/api/users/2?req=12"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -196,7 +196,7 @@ describe("route variants", () => { describe("when restoring route variants", () => { it("should serve users under the /api/users path", async () => { core.mocks.restoreRoutesVariants(); - const users = await fetch("/api/users?req=1"); + const users = await doFetch("/api/users?req=1"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -205,13 +205,13 @@ describe("route variants", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1?req=2"); + const users = await doFetch("/api/users/1?req=2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=3"); + const users = await doFetch("/api/users/2?req=3"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -220,7 +220,7 @@ describe("route variants", () => { describe('when using route variant "get-user:real"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("get-user:real"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -229,13 +229,13 @@ describe("route variants", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -247,7 +247,7 @@ describe("route variants", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -256,13 +256,13 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -271,13 +271,13 @@ describe("route variants", () => { describe('when using route variant "get-user:real" again', () => { it("should serve user 1 under the /api/users/1 path", async () => { core.mocks.useRouteVariant("get-user:real"); - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -286,13 +286,13 @@ describe("route variants", () => { describe("when restoring route variants again", () => { it("should serve user 2 under the /api/users/1 path", async () => { core.mocks.restoreRoutesVariants(); - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); diff --git a/test/core-e2e-legacy/src/scaffold.spec.js b/test/core-e2e-legacy/src/scaffold.spec.js index 826315b8f..ea9251af6 100644 --- a/test/core-e2e-legacy/src/scaffold.spec.js +++ b/test/core-e2e-legacy/src/scaffold.spec.js @@ -12,7 +12,7 @@ const path = require("path"); const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, removeConfigFile, @@ -59,7 +59,7 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -68,13 +68,13 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -96,7 +96,7 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -105,13 +105,13 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -173,7 +173,7 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -182,13 +182,13 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -222,7 +222,7 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); diff --git a/test/core-e2e-legacy/src/support/helpers.js b/test/core-e2e-legacy/src/support/helpers.js index 588513311..eaea72792 100644 --- a/test/core-e2e-legacy/src/support/helpers.js +++ b/test/core-e2e-legacy/src/support/helpers.js @@ -88,7 +88,7 @@ const serverUrl = (port) => { return `http://127.0.0.1:${port || SERVER_PORT}`; }; -const fetch = (uri, options = {}) => { +const doFetch = (uri, options = {}) => { const requestOptions = { ...defaultRequestOptions, ...options, @@ -174,7 +174,7 @@ module.exports = { createCore, startExistingCore, startCore, - fetch, + doFetch, TimeCounter, mocksRunner, wait, diff --git a/test/core-e2e-legacy/src/typescript.spec.js b/test/core-e2e-legacy/src/typescript.spec.js index b8e440516..0dd20ad4c 100644 --- a/test/core-e2e-legacy/src/typescript.spec.js +++ b/test/core-e2e-legacy/src/typescript.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, wait, @@ -40,7 +40,7 @@ describe("when babelRegister is enabled and typescript files are used", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -48,7 +48,7 @@ describe("when babelRegister is enabled and typescript files are used", () => { }); it("should serve books in /api/books path", async () => { - const users = await fetch("/api/books"); + const users = await doFetch("/api/books"); expect(users.body).toEqual([ { id: 1, title: "1984" }, { id: 2, title: "Brave New World" }, @@ -56,7 +56,7 @@ describe("when babelRegister is enabled and typescript files are used", () => { }); it("middleware should trace request and add headers", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.headers.get("x-mocks-server-example")).toEqual("custom-header-typescript"); expect(mocks.logs.current).toEqual( expect.stringContaining( @@ -66,12 +66,12 @@ describe("when babelRegister is enabled and typescript files are used", () => { }); it("should serve user 1 in /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 in /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); diff --git a/test/core-e2e-legacy/src/validations.spec.js b/test/core-e2e-legacy/src/validations.spec.js index 511d29ceb..08ffa2e6d 100644 --- a/test/core-e2e-legacy/src/validations.spec.js +++ b/test/core-e2e-legacy/src/validations.spec.js @@ -13,7 +13,7 @@ const { waitForServer, findAlert, filterAlerts, - fetch, + doFetch, removeConfigFile, } = require("./support/helpers"); @@ -151,25 +151,25 @@ describe("mocks and routes validations", () => { }); it("should return user 2 at /api/users/1", async () => { - const response = await fetch("/api/users/1"); + const response = await doFetch("/api/users/1"); expect(response.status).toEqual(200); expect(response.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return user 1 at /api/invalid-users/1", async () => { - const response = await fetch("/api/invalid-users/1"); + const response = await doFetch("/api/invalid-users/1"); expect(response.status).toEqual(200); expect(response.body).toEqual({ id: 1, name: "John Doe" }); }); it("should return 404 at /api/users", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.status).toEqual(404); }); it("should return user 1 at /api/invalid-users/1 when changing to mock with no valid variant", async () => { core.config.namespace("mocks").option("selected").value = "invalid-variant"; - const response = await fetch("/api/invalid-users/1"); + const response = await doFetch("/api/invalid-users/1"); expect(response.status).toEqual(200); expect(response.body).toEqual({ id: 1, name: "John Doe" }); }); diff --git a/test/core-e2e-legacy/src/watch-disabled.spec.js b/test/core-e2e-legacy/src/watch-disabled.spec.js index e2c516d3c..36e8ed576 100644 --- a/test/core-e2e-legacy/src/watch-disabled.spec.js +++ b/test/core-e2e-legacy/src/watch-disabled.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, fixturesFolder, waitForServer, wait, @@ -36,7 +36,7 @@ describe("when files watch is disabled", () => { describe("When started", () => { it("should serve users", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -51,7 +51,7 @@ describe("when files watch is disabled", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e-legacy/src/watch-enabled.spec.js b/test/core-e2e-legacy/src/watch-enabled.spec.js index 832da94d7..a58ebd616 100644 --- a/test/core-e2e-legacy/src/watch-enabled.spec.js +++ b/test/core-e2e-legacy/src/watch-enabled.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, fixturesFolder, waitForServer, waitForServerUrl, @@ -37,7 +37,7 @@ describe("when files watch is enabled", () => { describe("When started", () => { it("should serve users", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -50,7 +50,7 @@ describe("when files watch is enabled", () => { await wait(2000); await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); await waitForServerUrl("/api/new-users"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.headers.get("x-custom-header")).toEqual("foo-header"); expect(users.headers.get("x-another-header")).toEqual("another-header"); expect(users.body).toEqual([ @@ -60,7 +60,7 @@ describe("when files watch is enabled", () => { }); it("should serve new users in /api/new-users path", async () => { - const users = await fetch("/api/new-users"); + const users = await doFetch("/api/new-users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -74,7 +74,7 @@ describe("when files watch is enabled", () => { await wait(2000); await fsExtra.copy(fixturesFolder("web-tutorial/db"), fixturesFolder("temp/db")); await wait(5000); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e/src/config-file-with-custom-route-handler.spec.js b/test/core-e2e/src/config-file-with-custom-route-handler.spec.js index f7c4a1d04..15fb27ac9 100644 --- a/test/core-e2e/src/config-file-with-custom-route-handler.spec.js +++ b/test/core-e2e/src/config-file-with-custom-route-handler.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, removeConfigFile, @@ -37,7 +37,7 @@ describe("when adding route handlers in config file", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e/src/config-file-with-plugins.spec.js b/test/core-e2e/src/config-file-with-plugins.spec.js index bf653bd9f..5a299fd26 100644 --- a/test/core-e2e/src/config-file-with-plugins.spec.js +++ b/test/core-e2e/src/config-file-with-plugins.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, wait, @@ -53,7 +53,7 @@ describe("when adding plugins in config file", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -61,19 +61,19 @@ describe("when adding plugins in config file", () => { }); it("should serve user 2 in /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 in /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should reload mocks when files are modified", async () => { await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); await wait(4000); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, diff --git a/test/core-e2e/src/config-file.spec.js b/test/core-e2e/src/config-file.spec.js index f3ef8c922..018366bbc 100644 --- a/test/core-e2e/src/config-file.spec.js +++ b/test/core-e2e/src/config-file.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, wait, @@ -42,7 +42,7 @@ describe("when using config file", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -50,19 +50,19 @@ describe("when using config file", () => { }); it("should serve user 2 in /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 in /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should have watch disabled", async () => { await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); await wait(4000); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e/src/cors-argument.spec.js b/test/core-e2e/src/cors-argument.spec.js index ad5137dee..78ade33be 100644 --- a/test/core-e2e/src/cors-argument.spec.js +++ b/test/core-e2e/src/cors-argument.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServer, removeConfigFile } = require("./support/helpers"); describe("cors command line argument", () => { let cli; @@ -25,7 +25,7 @@ describe("cors command line argument", () => { }); it("cors middleware should handle OPTIONS requests", async () => { - const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + const users = await doFetch(`/api/users/1`, { method: "OPTIONS" }); expect(users.status).toEqual(204); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); expect(users.headers.get("access-control-allow-methods")).toEqual( @@ -35,7 +35,7 @@ describe("cors command line argument", () => { }); it("cors middleware should handle OPTIONS requests to all paths without route", async () => { - const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + const users = await doFetch(`/api/foo`, { method: "OPTIONS" }); expect(users.status).toEqual(204); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); expect(users.headers.get("access-control-allow-methods")).toEqual( @@ -45,7 +45,7 @@ describe("cors command line argument", () => { }); it("Response headers should include cors headers", async () => { - const users = await fetch(`/api/users/1`); + const users = await doFetch(`/api/users/1`); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); }); }); @@ -61,7 +61,7 @@ describe("cors command line argument", () => { }); it("route middleware should handle OPTIONS request", async () => { - const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + const users = await doFetch(`/api/users/1`, { method: "OPTIONS" }); expect(users.status).toEqual(200); expect(users.headers.get("access-control-allow-origin")).toEqual(null); expect(users.headers.get("access-control-allow-methods")).toEqual(null); @@ -69,7 +69,7 @@ describe("cors command line argument", () => { }); it("cors middleware should not handle OPTIONS requests to paths without route", async () => { - const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + const users = await doFetch(`/api/foo`, { method: "OPTIONS" }); expect(users.status).toEqual(404); expect(users.headers.get("access-control-allow-origin")).toEqual(null); expect(users.headers.get("access-control-allow-methods")).toEqual(null); @@ -77,7 +77,7 @@ describe("cors command line argument", () => { }); it("Response headers should not include cors headers", async () => { - const users = await fetch(`/api/users/1`); + const users = await doFetch(`/api/users/1`); expect(users.headers.get("access-control-allow-origin")).toEqual(null); }); }); @@ -96,7 +96,7 @@ describe("cors command line argument", () => { }); it("route middleware should handle OPTIONS request", async () => { - const users = await fetch(`/api/users/1`, { method: "OPTIONS" }); + const users = await doFetch(`/api/users/1`, { method: "OPTIONS" }); expect(users.status).toEqual(200); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); expect(users.headers.get("access-control-allow-methods")).toEqual( @@ -106,7 +106,7 @@ describe("cors command line argument", () => { }); it("cors middleware should not handle OPTIONS requests to paths without route", async () => { - const users = await fetch(`/api/foo`, { method: "OPTIONS" }); + const users = await doFetch(`/api/foo`, { method: "OPTIONS" }); expect(users.status).toEqual(404); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); expect(users.headers.get("access-control-allow-methods")).toEqual( @@ -116,7 +116,7 @@ describe("cors command line argument", () => { }); it("Response headers should include cors headers", async () => { - const users = await fetch(`/api/users/1`); + const users = await doFetch(`/api/users/1`); expect(users.headers.get("access-control-allow-origin")).toEqual("*"); }); }); diff --git a/test/core-e2e/src/custom-router.spec.js b/test/core-e2e/src/custom-router.spec.js index 5b6a3314c..abc7275ba 100644 --- a/test/core-e2e/src/custom-router.spec.js +++ b/test/core-e2e/src/custom-router.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const express = require("express"); const Core = require("@mocks-server/core"); -const { fetch, fixturesFolder, waitForServer, removeConfigFile } = require("./support/helpers"); +const { doFetch, fixturesFolder, waitForServer, removeConfigFile } = require("./support/helpers"); describe("when using custom router", () => { const customRouter = express.Router(); @@ -49,12 +49,12 @@ describe("when using custom router", () => { }); it("custom router should be listening", async () => { - const response = await fetch("/api/custom"); + const response = await doFetch("/api/custom"); expect(response.body.customRouterListening).toEqual(true); }); it("fixtures routers should be listening", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -63,7 +63,7 @@ describe("when using custom router", () => { it("custom router should stop listening when is removed", async () => { await core.removeRouter("/api/custom", customRouter); - const response = await fetch("/api/custom"); + const response = await doFetch("/api/custom"); expect(response.status).toEqual(404); }); }); @@ -93,12 +93,12 @@ describe("when using custom router", () => { }); it("custom router should be listening", async () => { - const response = await fetch("/api/custom"); + const response = await doFetch("/api/custom"); expect(response.body.customRouterListening).toEqual(true); }); it("mocks routers should be listening", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -107,7 +107,7 @@ describe("when using custom router", () => { it("custom router should stop listening when is removed", async () => { await core.removeRouter("/api/custom", customRouter); - const response = await fetch("/api/custom"); + const response = await doFetch("/api/custom"); expect(response.status).toEqual(404); }); }); diff --git a/test/core-e2e/src/delay-argument.spec.js b/test/core-e2e/src/delay-argument.spec.js index 167798f9f..fd1675144 100644 --- a/test/core-e2e/src/delay-argument.spec.js +++ b/test/core-e2e/src/delay-argument.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServer, TimeCounter, removeConfigFile, @@ -32,7 +32,7 @@ describe("delay argument", () => { it("should set delay", async () => { expect.assertions(2); const timeCounter = new TimeCounter(); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); timeCounter.stop(); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, diff --git a/test/core-e2e/src/delay-setting.spec.js b/test/core-e2e/src/delay-setting.spec.js index 084c24ce1..7a01fbaa3 100644 --- a/test/core-e2e/src/delay-setting.spec.js +++ b/test/core-e2e/src/delay-setting.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, TimeCounter, waitForServer, removeConfigFile, @@ -32,7 +32,7 @@ describe("delay setting", () => { describe("When started", () => { it("should respond with no delay", async () => { const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeLessThan(400); }); @@ -42,7 +42,7 @@ describe("delay setting", () => { it("should respond after defined delay", async () => { core.config.namespace("mocks").option("delay").value = 1000; const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); }); @@ -52,7 +52,7 @@ describe("delay setting", () => { it("should respond after route variant defined delay", async () => { core.mocks.useRouteVariant("get-users:delayed"); const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); }); @@ -60,7 +60,7 @@ describe("delay setting", () => { it("should respond with same delay after setting delay to zero", async () => { core.config.namespace("mocks").option("delay").value = 0; const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); }); @@ -68,7 +68,7 @@ describe("delay setting", () => { it("should respond with same delay after setting delay to 4000", async () => { core.config.namespace("mocks").option("delay").value = 4000; const timeCounter = new TimeCounter(); - await fetch("/api/users"); + await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); }); @@ -77,7 +77,7 @@ describe("delay setting", () => { describe("When route has delay", () => { it("should respond after route defined delay", async () => { const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); }); @@ -85,7 +85,7 @@ describe("delay setting", () => { it("should respond with same delay after setting delay to zero", async () => { core.config.namespace("mocks").option("delay").value = 0; const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(999); }); @@ -95,7 +95,7 @@ describe("delay setting", () => { it("should respond after route variant defined delay", async () => { core.config.namespace("mocks").option("selected").value = "user-2"; const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); }); @@ -109,7 +109,7 @@ describe("delay setting", () => { it("should respond with no delay", async () => { core.mocks.useRouteVariant("get-user:zero-delay"); const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeLessThan(500); }); @@ -140,7 +140,7 @@ describe("delay setting", () => { core.config.namespace("mocks").option("delay").value = 3000; core.mocks.useRouteVariant("get-user:null-delay"); const timeCounter = new TimeCounter(); - await fetch("/api/users/1"); + await doFetch("/api/users/1"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(2999); }); diff --git a/test/core-e2e/src/esm.spec.js b/test/core-e2e/src/esm.spec.js index 99f3c2bf0..606aa0517 100644 --- a/test/core-e2e/src/esm.spec.js +++ b/test/core-e2e/src/esm.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, wait, @@ -43,7 +43,7 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -51,7 +51,7 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("middleware should trace request and add headers", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.headers.get("x-mocks-server-example")).toEqual("custom-header"); expect(mocks.logs.current).toEqual( expect.stringContaining( @@ -61,19 +61,19 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("should serve user 1 in /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 in /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should reload mocks when files are modified", async () => { await fsExtra.copy(fixturesFolder("esm-modified"), fixturesFolder("temp")); await wait(4000); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -81,7 +81,7 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("should serve new users route", async () => { - const users = await fetch("/api/new-users"); + const users = await doFetch("/api/new-users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -90,7 +90,7 @@ describe("when babelRegister is enabled and esm files are used", () => { }); it("middleware should be disabled", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); }); }); diff --git a/test/core-e2e/src/events.spec.js b/test/core-e2e/src/events.spec.js index aea9f5d06..abc8f157a 100644 --- a/test/core-e2e/src/events.spec.js +++ b/test/core-e2e/src/events.spec.js @@ -12,7 +12,7 @@ const sinon = require("sinon"); const { startCore, - fetch, + doFetch, waitForServer, wait, fixturesFolder, @@ -44,7 +44,7 @@ describe("events", () => { describe("When started", () => { it("should load web-tutorial mocks", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e/src/json-files.spec.js b/test/core-e2e/src/json-files.spec.js index 267cdee84..f9b5f7f0d 100644 --- a/test/core-e2e/src/json-files.spec.js +++ b/test/core-e2e/src/json-files.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, findAlert, removeConfigFile, @@ -40,7 +40,7 @@ describe("json files", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -49,13 +49,13 @@ describe("json files", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -71,7 +71,7 @@ describe("json files", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -80,13 +80,13 @@ describe("json files", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -105,7 +105,7 @@ describe("json files", () => { // if mock not exists, it uses the first one found it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -114,13 +114,13 @@ describe("json files", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); diff --git a/test/core-e2e/src/load-mocks-and-routes.spec.js b/test/core-e2e/src/load-mocks-and-routes.spec.js index 98292dd30..aaef20f24 100644 --- a/test/core-e2e/src/load-mocks-and-routes.spec.js +++ b/test/core-e2e/src/load-mocks-and-routes.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, waitForServerUrl, removeConfigFile, @@ -42,7 +42,7 @@ describe("loadMocks and loadRoutes methods", () => { describe("When started", () => { it("should return users", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -115,13 +115,13 @@ describe("loadMocks and loadRoutes methods", () => { core.config.namespace("mocks").option("selected").value = "users-and-library"; await waitForServerUrl("/api/books"); - const books = await fetch("/api/books"); + const books = await doFetch("/api/books"); expect(books.body).toEqual([{ id: 1, title: "1984" }]); - const authors = await fetch("/api/authors"); + const authors = await doFetch("/api/authors"); expect(authors.body).toEqual([{ id: 1, name: "George Orwell" }]); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -130,27 +130,27 @@ describe("loadMocks and loadRoutes methods", () => { it("should be able to change to a new mock", async () => { await changeMockAndWait("authors-error"); - const books = await fetch("/api/books"); + const books = await doFetch("/api/books"); expect(books.body).toEqual([{ id: 1, title: "1984" }]); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, ]); - const authors = await fetch("/api/authors"); + const authors = await doFetch("/api/authors"); expect(authors.status).toEqual(403); }); it("should keep mocks loaded from files", async () => { await changeMockAndWait("base"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, ]); - const authors = await fetch("/api/authors"); + const authors = await doFetch("/api/authors"); expect(authors.status).toEqual(404); }); }); diff --git a/test/core-e2e/src/mock-argument.spec.js b/test/core-e2e/src/mock-argument.spec.js index 537beb2ad..bb51c3b2f 100644 --- a/test/core-e2e/src/mock-argument.spec.js +++ b/test/core-e2e/src/mock-argument.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServer, removeConfigFile } = require("./support/helpers"); describe("mock argument", () => { const PATH_OPTION = "--files.path=web-tutorial"; @@ -23,7 +23,7 @@ describe("mock argument", () => { it("should set as current mock the first one found", async () => { mocks = mocksRunner([PATH_OPTION]); await waitForServer(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); @@ -32,7 +32,7 @@ describe("mock argument", () => { it("should set current behavior", async () => { mocks = mocksRunner([PATH_OPTION, "--mocks.selected=user-real"]); await waitForServer(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); @@ -47,7 +47,7 @@ describe("mock argument", () => { it("should set as current mock the first one found", async () => { mocks = mocksRunner([PATH_OPTION, "--mocks.selected=foo"]); await waitForServer(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); diff --git a/test/core-e2e/src/mock-setting.spec.js b/test/core-e2e/src/mock-setting.spec.js index 3c41ee9bf..26a71acb8 100644 --- a/test/core-e2e/src/mock-setting.spec.js +++ b/test/core-e2e/src/mock-setting.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, findAlert, removeConfigFile, @@ -40,7 +40,7 @@ describe("mock setting", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -49,13 +49,13 @@ describe("mock setting", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -71,7 +71,7 @@ describe("mock setting", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -80,13 +80,13 @@ describe("mock setting", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -98,7 +98,7 @@ describe("mock setting", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -107,19 +107,19 @@ describe("mock setting", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return not found for /api/users/3 path", async () => { - const users = await fetch("/api/users/3"); + const users = await doFetch("/api/users/3"); expect(users.status).toEqual(404); }); }); @@ -137,7 +137,7 @@ describe("mock setting", () => { // if mock not exists, it uses the first one found it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -146,13 +146,13 @@ describe("mock setting", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); diff --git a/test/core-e2e/src/multi-methods.spec.js b/test/core-e2e/src/multi-methods.spec.js index b7556c050..28755ab96 100644 --- a/test/core-e2e/src/multi-methods.spec.js +++ b/test/core-e2e/src/multi-methods.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startCore, waitForServer, fetch, removeConfigFile } = require("./support/helpers"); +const { startCore, waitForServer, doFetch, removeConfigFile } = require("./support/helpers"); describe("when method is defined as array", () => { let requestNumber = 1; @@ -146,7 +146,7 @@ describe("when method is defined as array", () => { describe("base mock", () => { it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users?req=1"); + const users = await doFetch("/api/users?req=1"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -156,7 +156,7 @@ describe("when method is defined as array", () => { const testMethod = (method, id, expectBody = true) => { it(`should serve user 1 under the /api/users/${id} path with ${method} method`, async () => { - const users = await fetch(`/api/users/${id}?req=${requestNumber}`, { method }); + const users = await doFetch(`/api/users/${id}?req=${requestNumber}`, { method }); requestNumber++; expect(users.status).toEqual(200); if (expectBody) { @@ -186,7 +186,7 @@ describe("when method is defined as array", () => { describe('when using route variant "get-user:2"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("get-user:2"); - const users = await fetch("/api/users?req=10"); + const users = await doFetch("/api/users?req=10"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -196,7 +196,7 @@ describe("when method is defined as array", () => { const testMethod = (method, id, expectBody = true) => { it(`should serve user 2 under the /api/users/${id} path with ${method} method`, async () => { - const users = await fetch(`/api/users/${id}?req=${requestNumber}`, { method }); + const users = await doFetch(`/api/users/${id}?req=${requestNumber}`, { method }); requestNumber++; expect(users.status).toEqual(200); if (expectBody) { diff --git a/test/core-e2e/src/path-argument.spec.js b/test/core-e2e/src/path-argument.spec.js index 9855805b9..c2e2720a0 100644 --- a/test/core-e2e/src/path-argument.spec.js +++ b/test/core-e2e/src/path-argument.spec.js @@ -12,7 +12,7 @@ const path = require("path"); const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, fixturesFolder, waitForServer, removeConfigFile, @@ -60,7 +60,7 @@ describe("path argument", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, diff --git a/test/core-e2e/src/path-not-exists.spec.js b/test/core-e2e/src/path-not-exists.spec.js index c1061ec53..f7571f1dc 100644 --- a/test/core-e2e/src/path-not-exists.spec.js +++ b/test/core-e2e/src/path-not-exists.spec.js @@ -12,7 +12,7 @@ const path = require("path"); const fsExtra = require("fs-extra"); const { startCore, - fetch, + doFetch, fixturesFolder, waitForServer, findAlert, @@ -69,7 +69,7 @@ describe("when path not exists", () => { describe("base mock", () => { it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); expect(users.body).toEqual([ @@ -79,14 +79,14 @@ describe("when path not exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.headers.get("x-mocks-server-example")).toEqual("some-value"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); @@ -96,7 +96,7 @@ describe("when path not exists", () => { describe("no-headers mock", () => { it("should not add headers to /api/users", async () => { changeMock("no-headers"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.body).toEqual([ @@ -106,14 +106,14 @@ describe("when path not exists", () => { }); it("should not add headers to /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should not add headers to /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); @@ -123,7 +123,7 @@ describe("when path not exists", () => { describe("user-real mock", () => { it("should serve users under the /api/users path", async () => { changeMock("user-real"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.body).toEqual([ @@ -133,14 +133,14 @@ describe("when path not exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.headers.get("x-mocks-server-example")).toEqual(null); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); diff --git a/test/core-e2e/src/path-setting.spec.js b/test/core-e2e/src/path-setting.spec.js index b09ed9723..d2e909d93 100644 --- a/test/core-e2e/src/path-setting.spec.js +++ b/test/core-e2e/src/path-setting.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, waitForServerUrl, fixturesFolder, @@ -35,7 +35,7 @@ describe("path setting", () => { describe("When started", () => { it("should load web-tutorial mocks", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -48,14 +48,14 @@ describe("path setting", () => { changePath(fixturesFolder("web-tutorial-modified")); await waitForServerUrl("/api/new-users"); - const users = await fetch("/api/new-users"); + const users = await doFetch("/api/new-users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, { id: 3, name: "Brand new user" }, ]); - const oldUsers = await fetch("/api/users"); + const oldUsers = await doFetch("/api/users"); expect(oldUsers.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, diff --git a/test/core-e2e/src/plugins.spec.js b/test/core-e2e/src/plugins.spec.js index b691fe344..22f931907 100644 --- a/test/core-e2e/src/plugins.spec.js +++ b/test/core-e2e/src/plugins.spec.js @@ -18,7 +18,7 @@ const filterLogs = (logs, text) => logs.filter((log) => log.includes(text)); const { startCore, - fetch, + doFetch, fixturesFolder, wait, TimeCounter, @@ -77,7 +77,7 @@ describe("plugins", () => { describe("when started", () => { it("should start server and send responses", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -127,7 +127,7 @@ describe("plugins", () => { }); it("should respond to custom routes", async () => { - const response = await fetch("/foo-path"); + const response = await doFetch("/foo-path"); expect(response.body).toEqual(FOO_CUSTOM_RESPONSE); }); @@ -188,7 +188,7 @@ describe("plugins", () => { core = await startCore("web-tutorial", { plugins: { register: [pluginConstructor] }, }); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e/src/port-argument.spec.js b/test/core-e2e/src/port-argument.spec.js index afbc0b1d5..32091e2d6 100644 --- a/test/core-e2e/src/port-argument.spec.js +++ b/test/core-e2e/src/port-argument.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServer, removeConfigFile } = require("./support/helpers"); describe("port command line argument", () => { let cli; @@ -24,7 +24,7 @@ describe("port command line argument", () => { }); it("should set server port", async () => { - const users = await fetch("/api/users", { + const users = await doFetch("/api/users", { port: 3005, }); expect(users.body).toEqual([ diff --git a/test/core-e2e/src/port-setting.spec.js b/test/core-e2e/src/port-setting.spec.js index ec4897ff8..7a049c73a 100644 --- a/test/core-e2e/src/port-setting.spec.js +++ b/test/core-e2e/src/port-setting.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { startCore, fetch, waitForServer, removeConfigFile } = require("./support/helpers"); +const { startCore, doFetch, waitForServer, removeConfigFile } = require("./support/helpers"); describe("port setting", () => { let core; @@ -25,7 +25,7 @@ describe("port setting", () => { describe("When started", () => { it("should be listening on port 3100", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -37,7 +37,7 @@ describe("port setting", () => { it("should be listening on new port", async () => { core.config.namespace("server").option("port").value = 3005; await waitForServer(3005); - const users = await fetch("/api/users", { + const users = await doFetch("/api/users", { port: 3005, }); expect(users.body).toEqual([ diff --git a/test/core-e2e/src/route-handler.spec.js b/test/core-e2e/src/route-handler.spec.js index 4c21c7361..9c061fe9a 100644 --- a/test/core-e2e/src/route-handler.spec.js +++ b/test/core-e2e/src/route-handler.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { createCore, startExistingCore, - fetch, + doFetch, fixturesFolder, findTrace, removeConfigFile, @@ -38,7 +38,7 @@ describe("when adding route handlers", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e/src/route-variants.spec.js b/test/core-e2e/src/route-variants.spec.js index 2a7b526d1..7f0965f95 100644 --- a/test/core-e2e/src/route-variants.spec.js +++ b/test/core-e2e/src/route-variants.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { startCore, - fetch, + doFetch, waitForServer, findTrace, removeConfigFile, @@ -33,7 +33,7 @@ describe("route variants", () => { describe("base mock", () => { it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users?req=1"); + const users = await doFetch("/api/users?req=1"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -48,7 +48,7 @@ describe("route variants", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1?req=2"); + const users = await doFetch("/api/users/1?req=2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -60,7 +60,7 @@ describe("route variants", () => { }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=3"); + const users = await doFetch("/api/users/2?req=3"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -75,7 +75,7 @@ describe("route variants", () => { describe('when using route variant "get-user:2"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("get-user:2"); - const users = await fetch("/api/users?req=4"); + const users = await doFetch("/api/users?req=4"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -90,7 +90,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1?req=5"); + const users = await doFetch("/api/users/1?req=5"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -102,7 +102,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=6"); + const users = await doFetch("/api/users/2?req=6"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -117,7 +117,7 @@ describe("route variants", () => { describe('when using route variant "tracer:disabled"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("tracer:disabled"); - const users = await fetch("/api/users?req=7"); + const users = await doFetch("/api/users?req=7"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -131,7 +131,7 @@ describe("route variants", () => { it("should serve user 2 under the /api/users/1 path", async () => { core.mocks.useRouteVariant("get-user:2"); - const users = await fetch("/api/users/1?req=8"); + const users = await doFetch("/api/users/1?req=8"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -141,7 +141,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=9"); + const users = await doFetch("/api/users/2?req=9"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -154,7 +154,7 @@ describe("route variants", () => { describe('when using route variant "tracer:enabled"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("tracer:enabled"); - const users = await fetch("/api/users?req=10"); + const users = await doFetch("/api/users?req=10"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -169,7 +169,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1?req=11"); + const users = await doFetch("/api/users/1?req=11"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -181,7 +181,7 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=12"); + const users = await doFetch("/api/users/2?req=12"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -196,7 +196,7 @@ describe("route variants", () => { describe("when restoring route variants", () => { it("should serve users under the /api/users path", async () => { core.mocks.restoreRoutesVariants(); - const users = await fetch("/api/users?req=1"); + const users = await doFetch("/api/users?req=1"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -205,13 +205,13 @@ describe("route variants", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1?req=2"); + const users = await doFetch("/api/users/1?req=2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2?req=3"); + const users = await doFetch("/api/users/2?req=3"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -220,7 +220,7 @@ describe("route variants", () => { describe('when using route variant "get-user:real"', () => { it("should serve users under the /api/users path", async () => { core.mocks.useRouteVariant("get-user:real"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -229,13 +229,13 @@ describe("route variants", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -247,7 +247,7 @@ describe("route variants", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -256,13 +256,13 @@ describe("route variants", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -271,13 +271,13 @@ describe("route variants", () => { describe('when using route variant "get-user:real" again', () => { it("should serve user 1 under the /api/users/1 path", async () => { core.mocks.useRouteVariant("get-user:real"); - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); @@ -286,13 +286,13 @@ describe("route variants", () => { describe("when restoring route variants again", () => { it("should serve user 2 under the /api/users/1 path", async () => { core.mocks.restoreRoutesVariants(); - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); diff --git a/test/core-e2e/src/scaffold.spec.js b/test/core-e2e/src/scaffold.spec.js index 826315b8f..ea9251af6 100644 --- a/test/core-e2e/src/scaffold.spec.js +++ b/test/core-e2e/src/scaffold.spec.js @@ -12,7 +12,7 @@ const path = require("path"); const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, removeConfigFile, @@ -59,7 +59,7 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -68,13 +68,13 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -96,7 +96,7 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -105,13 +105,13 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -173,7 +173,7 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve users under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(200); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, @@ -182,13 +182,13 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -222,7 +222,7 @@ describe("when nor config file nor mocks folder exists", () => { }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.status).toEqual(200); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); diff --git a/test/core-e2e/src/support/helpers.js b/test/core-e2e/src/support/helpers.js index 588513311..eaea72792 100644 --- a/test/core-e2e/src/support/helpers.js +++ b/test/core-e2e/src/support/helpers.js @@ -88,7 +88,7 @@ const serverUrl = (port) => { return `http://127.0.0.1:${port || SERVER_PORT}`; }; -const fetch = (uri, options = {}) => { +const doFetch = (uri, options = {}) => { const requestOptions = { ...defaultRequestOptions, ...options, @@ -174,7 +174,7 @@ module.exports = { createCore, startExistingCore, startCore, - fetch, + doFetch, TimeCounter, mocksRunner, wait, diff --git a/test/core-e2e/src/typescript.spec.js b/test/core-e2e/src/typescript.spec.js index b8e440516..0dd20ad4c 100644 --- a/test/core-e2e/src/typescript.spec.js +++ b/test/core-e2e/src/typescript.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServer, fixturesFolder, wait, @@ -40,7 +40,7 @@ describe("when babelRegister is enabled and typescript files are used", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -48,7 +48,7 @@ describe("when babelRegister is enabled and typescript files are used", () => { }); it("should serve books in /api/books path", async () => { - const users = await fetch("/api/books"); + const users = await doFetch("/api/books"); expect(users.body).toEqual([ { id: 1, title: "1984" }, { id: 2, title: "Brave New World" }, @@ -56,7 +56,7 @@ describe("when babelRegister is enabled and typescript files are used", () => { }); it("middleware should trace request and add headers", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.headers.get("x-mocks-server-example")).toEqual("custom-header-typescript"); expect(mocks.logs.current).toEqual( expect.stringContaining( @@ -66,12 +66,12 @@ describe("when babelRegister is enabled and typescript files are used", () => { }); it("should serve user 1 in /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 in /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); diff --git a/test/core-e2e/src/validations.spec.js b/test/core-e2e/src/validations.spec.js index eb6ec42df..643f1f8de 100644 --- a/test/core-e2e/src/validations.spec.js +++ b/test/core-e2e/src/validations.spec.js @@ -13,7 +13,7 @@ const { waitForServer, findAlert, filterAlerts, - fetch, + doFetch, removeConfigFile, } = require("./support/helpers"); @@ -151,25 +151,25 @@ describe("mocks and routes validations", () => { }); it("should return user 2 at /api/users/1", async () => { - const response = await fetch("/api/users/1"); + const response = await doFetch("/api/users/1"); expect(response.status).toEqual(200); expect(response.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return user 1 at /api/invalid-users/1", async () => { - const response = await fetch("/api/invalid-users/1"); + const response = await doFetch("/api/invalid-users/1"); expect(response.status).toEqual(200); expect(response.body).toEqual({ id: 1, name: "John Doe" }); }); it("should return 404 at /api/users", async () => { - const response = await fetch("/api/users"); + const response = await doFetch("/api/users"); expect(response.status).toEqual(404); }); it("should return user 1 at /api/invalid-users/1 when changing to mock with no valid variant", async () => { core.config.namespace("mocks").option("selected").value = "invalid-variant"; - const response = await fetch("/api/invalid-users/1"); + const response = await doFetch("/api/invalid-users/1"); expect(response.status).toEqual(200); expect(response.body).toEqual({ id: 1, name: "John Doe" }); }); diff --git a/test/core-e2e/src/watch-disabled.spec.js b/test/core-e2e/src/watch-disabled.spec.js index e2c516d3c..36e8ed576 100644 --- a/test/core-e2e/src/watch-disabled.spec.js +++ b/test/core-e2e/src/watch-disabled.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, fixturesFolder, waitForServer, wait, @@ -36,7 +36,7 @@ describe("when files watch is disabled", () => { describe("When started", () => { it("should serve users", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -51,7 +51,7 @@ describe("when files watch is disabled", () => { }); it("should serve users in /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/core-e2e/src/watch-enabled.spec.js b/test/core-e2e/src/watch-enabled.spec.js index 832da94d7..a58ebd616 100644 --- a/test/core-e2e/src/watch-enabled.spec.js +++ b/test/core-e2e/src/watch-enabled.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, fixturesFolder, waitForServer, waitForServerUrl, @@ -37,7 +37,7 @@ describe("when files watch is enabled", () => { describe("When started", () => { it("should serve users", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -50,7 +50,7 @@ describe("when files watch is enabled", () => { await wait(2000); await fsExtra.copy(fixturesFolder("web-tutorial-modified"), fixturesFolder("temp")); await waitForServerUrl("/api/new-users"); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.headers.get("x-custom-header")).toEqual("foo-header"); expect(users.headers.get("x-another-header")).toEqual("another-header"); expect(users.body).toEqual([ @@ -60,7 +60,7 @@ describe("when files watch is enabled", () => { }); it("should serve new users in /api/new-users path", async () => { - const users = await fetch("/api/new-users"); + const users = await doFetch("/api/new-users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -74,7 +74,7 @@ describe("when files watch is enabled", () => { await wait(2000); await fsExtra.copy(fixturesFolder("web-tutorial/db"), fixturesFolder("temp/db")); await wait(5000); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, diff --git a/test/main-e2e/src/proxy.spec.js b/test/main-e2e/src/proxy.spec.js index 3bc03b2c1..0154122e3 100644 --- a/test/main-e2e/src/proxy.spec.js +++ b/test/main-e2e/src/proxy.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServerAndCli, wait, fixturesFolder, @@ -43,7 +43,7 @@ describe("scaffold", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -51,19 +51,19 @@ describe("scaffold", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); describe("mocks api", () => { it("should return mocks", async () => { - const response = await fetch("/admin/mocks"); + const response = await doFetch("/admin/mocks"); expect(response.body).toEqual([ { id: "base", @@ -83,7 +83,7 @@ describe("scaffold", () => { describe("routes api", () => { it("should return routes", async () => { - const response = await fetch("/admin/routes"); + const response = await doFetch("/admin/routes"); expect(response.body).toEqual([ { id: "proxy-all", @@ -98,7 +98,7 @@ describe("scaffold", () => { describe("routes variants api", () => { it("should return routes variants", async () => { - const response = await fetch("/admin/routes-variants"); + const response = await doFetch("/admin/routes-variants"); expect(response.body).toEqual([ { id: "proxy-all:enabled", @@ -129,12 +129,12 @@ describe("scaffold", () => { }); it("should return not found for /api/users path", async () => { - const usersResponse = await fetch("/api/users"); + const usersResponse = await doFetch("/api/users"); expect(usersResponse.status).toEqual(404); }); it("should return not found for /api/users/2 path", async () => { - const usersResponse = await fetch("/api/users/2"); + const usersResponse = await doFetch("/api/users/2"); expect(usersResponse.status).toEqual(404); }); }); @@ -153,7 +153,7 @@ describe("scaffold", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -161,14 +161,14 @@ describe("scaffold", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); describe("when using api to restore routes variants", () => { beforeAll(async () => { - await fetch("/admin/mock-custom-routes-variants", { + await doFetch("/admin/mock-custom-routes-variants", { method: "DELETE", }); }); @@ -180,19 +180,19 @@ describe("scaffold", () => { }); it("should return custom route variants in API", async () => { - const response = await fetch("/admin/mock-custom-routes-variants"); + const response = await doFetch("/admin/mock-custom-routes-variants"); expect(response.body).toEqual([]); }); it("should return not found for /api/users path", async () => { - const usersResponse = await fetch("/api/users"); + const usersResponse = await doFetch("/api/users"); expect(usersResponse.status).toEqual(404); }); }); describe("when using api to change current mock", () => { beforeAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { mocks: { @@ -203,7 +203,7 @@ describe("scaffold", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); diff --git a/test/main-e2e/src/scaffold.spec.js b/test/main-e2e/src/scaffold.spec.js index b11769da4..e81013d85 100644 --- a/test/main-e2e/src/scaffold.spec.js +++ b/test/main-e2e/src/scaffold.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServerAndCli, wait, cleanScaffold, @@ -40,7 +40,7 @@ describe("scaffold", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -48,19 +48,19 @@ describe("scaffold", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); describe("mocks api", () => { it("should return mocks", async () => { - const response = await fetch("/admin/mocks"); + const response = await doFetch("/admin/mocks"); expect(response.body).toEqual([ { id: "base", @@ -86,7 +86,7 @@ describe("scaffold", () => { describe("routes api", () => { it("should return routes", async () => { - const response = await fetch("/admin/routes"); + const response = await doFetch("/admin/routes"); expect(response.body).toEqual([ { id: "add-headers", @@ -115,7 +115,7 @@ describe("scaffold", () => { describe("routes variants api", () => { it("should return routes variants", async () => { - const response = await fetch("/admin/routes-variants"); + const response = await doFetch("/admin/routes-variants"); expect(response.body).toEqual([ { id: "add-headers:enabled", @@ -178,7 +178,7 @@ describe("scaffold", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -186,17 +186,17 @@ describe("scaffold", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return not found for /api/users/3 path", async () => { - const usersResponse = await fetch("/api/users/3"); + const usersResponse = await doFetch("/api/users/3"); expect(usersResponse.status).toEqual(404); }); }); @@ -213,7 +213,7 @@ describe("scaffold", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -221,17 +221,17 @@ describe("scaffold", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should return user 2 for /api/users/3 path", async () => { - const users = await fetch("/api/users/3"); + const users = await doFetch("/api/users/3"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); @@ -244,7 +244,7 @@ describe("scaffold", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -252,24 +252,24 @@ describe("scaffold", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return not found for /api/users/3 path", async () => { - const usersResponse = await fetch("/api/users/3"); + const usersResponse = await doFetch("/api/users/3"); expect(usersResponse.status).toEqual(404); }); }); describe("when using api to change current mock", () => { beforeAll(async () => { - await fetch("/admin/settings", { + await doFetch("/admin/settings", { method: "PATCH", body: { mocks: { @@ -280,12 +280,12 @@ describe("scaffold", () => { }); it("should return new mock when getting settings", async () => { - const settingsResponse = await fetch("/admin/settings"); + const settingsResponse = await doFetch("/admin/settings"); expect(settingsResponse.body.mocks.selected).toEqual("base"); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); @@ -297,7 +297,7 @@ describe("scaffold", () => { describe("when using api to set route variant", () => { beforeAll(async () => { - await fetch("/admin/mock-custom-routes-variants", { + await doFetch("/admin/mock-custom-routes-variants", { method: "POST", body: { id: "get-user:real", @@ -313,29 +313,29 @@ describe("scaffold", () => { }); it("should return custom route variant in API", async () => { - const response = await fetch("/admin/mock-custom-routes-variants"); + const response = await doFetch("/admin/mock-custom-routes-variants"); expect(response.body).toEqual(["get-user:real"]); }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return not found for /api/users/3 path", async () => { - const usersResponse = await fetch("/api/users/3"); + const usersResponse = await doFetch("/api/users/3"); expect(usersResponse.status).toEqual(404); }); }); describe("when using api to restore routes variants", () => { beforeAll(async () => { - await fetch("/admin/mock-custom-routes-variants", { + await doFetch("/admin/mock-custom-routes-variants", { method: "DELETE", }); }); @@ -347,12 +347,12 @@ describe("scaffold", () => { }); it("should return custom route variants in API", async () => { - const response = await fetch("/admin/mock-custom-routes-variants"); + const response = await doFetch("/admin/mock-custom-routes-variants"); expect(response.body).toEqual([]); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); diff --git a/test/main-e2e/src/support/helpers.js b/test/main-e2e/src/support/helpers.js index a24e01d04..e396aefb8 100644 --- a/test/main-e2e/src/support/helpers.js +++ b/test/main-e2e/src/support/helpers.js @@ -48,7 +48,7 @@ const serverUrl = (port) => { return `http://127.0.0.1:${port || SERVER_PORT}`; }; -const fetch = (uri, options = {}) => { +const doFetch = (uri, options = {}) => { const requestOptions = { ...defaultRequestOptions, ...options, @@ -123,7 +123,7 @@ const mocksRunner = (args = [], options = {}) => { }; module.exports = { - fetch, + doFetch, TimeCounter, mocksRunner, wait, diff --git a/test/plugin-inquirer-cli-e2e/src/main/arguments.spec.js b/test/plugin-inquirer-cli-e2e/src/main/arguments.spec.js index 9b6fdde0a..34fa2ef2c 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/arguments.spec.js +++ b/test/plugin-inquirer-cli-e2e/src/main/arguments.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServerAndCli, TimeCounter } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServerAndCli, TimeCounter } = require("./support/helpers"); describe("command line arguments", () => { let mocks; @@ -22,7 +22,7 @@ describe("command line arguments", () => { expect.assertions(2); mocks = mocksRunner(["--files.path=web-tutorial"]); await waitForServerAndCli(); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -37,7 +37,7 @@ describe("command line arguments", () => { expect.assertions(2); mocks = mocksRunner(["--files.path=web-tutorial"]); await waitForServerAndCli(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); expect(mocks.currentScreen).toEqual(expect.stringContaining("Current mock: base")); }); @@ -48,7 +48,7 @@ describe("command line arguments", () => { expect.assertions(2); mocks = mocksRunner(["--files.path=web-tutorial", "--mocks.selected=user-2"]); await waitForServerAndCli(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); expect(mocks.currentScreen).toEqual(expect.stringContaining("Current mock: user-2")); }); @@ -66,7 +66,7 @@ describe("command line arguments", () => { expect.assertions(3); mocks = mocksRunner(["--files.path=web-tutorial", "--mocks.selected=foo"]); await waitForServerAndCli(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); expect(mocks.currentScreen).toEqual(expect.stringContaining("Using the first one found")); expect(mocks.currentScreen).toEqual(expect.stringContaining("Current mock: base")); @@ -80,7 +80,7 @@ describe("command line arguments", () => { mocks = mocksRunner(["--files.path=web-tutorial", "--mocks.delay=2000"]); await waitForServerAndCli(); const timeCounter = new TimeCounter(); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); timeCounter.stop(); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, diff --git a/test/plugin-inquirer-cli-e2e/src/main/disabled.spec.js b/test/plugin-inquirer-cli-e2e/src/main/disabled.spec.js index c154bc479..3b25f3eb3 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/disabled.spec.js +++ b/test/plugin-inquirer-cli-e2e/src/main/disabled.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServer, TimeCounter } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServer, TimeCounter } = require("./support/helpers"); describe("command line arguments with cli disabled", () => { jest.setTimeout(15000); @@ -30,7 +30,7 @@ describe("command line arguments with cli disabled", () => { it("should set mocks folder", async () => { mocks = mocksRunner(["--files.path=web-tutorial", "--no-plugins.inquirerCli.enabled"]); await waitForServer(); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -43,7 +43,7 @@ describe("command line arguments with cli disabled", () => { it("should set as current behavior the first one found", async () => { mocks = mocksRunner(["--files.path=web-tutorial", "--no-plugins.inquirerCli.enabled"]); await waitForServer(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); @@ -56,7 +56,7 @@ describe("command line arguments with cli disabled", () => { "--mocks.selected=user-2", ]); await waitForServer(); - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); @@ -72,7 +72,7 @@ describe("command line arguments with cli disabled", () => { ]); await waitForServer(); const timeCounter = new TimeCounter(); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); timeCounter.stop(); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, diff --git a/test/plugin-inquirer-cli-e2e/src/main/files-watch.spec.js b/test/plugin-inquirer-cli-e2e/src/main/files-watch.spec.js index 16904e02b..8c437753a 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/files-watch.spec.js +++ b/test/plugin-inquirer-cli-e2e/src/main/files-watch.spec.js @@ -11,7 +11,7 @@ Unless required by applicable law or agreed to in writing, software distributed const fsExtra = require("fs-extra"); const { mocksRunner, - fetch, + doFetch, waitForServerAndCli, wait, fixturesFolder, @@ -43,7 +43,7 @@ describe("files watcher", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -51,12 +51,12 @@ describe("files watcher", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); @@ -73,7 +73,7 @@ describe("files watcher", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -81,12 +81,12 @@ describe("files watcher", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe modified" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe modified" }); }); }); @@ -104,7 +104,7 @@ describe("files watcher", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -112,12 +112,12 @@ describe("files watcher", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 2, name: "Jane Doe modified" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe modified" }); }); }); @@ -135,7 +135,7 @@ describe("files watcher", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe modified" }, { id: 2, name: "Jane Doe modified" }, @@ -143,12 +143,12 @@ describe("files watcher", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe modified" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe modified" }); }); }); @@ -179,7 +179,7 @@ describe("files watcher", () => { }); it("should not serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.status).toEqual(404); }); diff --git a/test/plugin-inquirer-cli-e2e/src/main/interactive-cli.spec.js b/test/plugin-inquirer-cli-e2e/src/main/interactive-cli.spec.js index d650190bc..4237df463 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/interactive-cli.spec.js +++ b/test/plugin-inquirer-cli-e2e/src/main/interactive-cli.spec.js @@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed const { mocksRunner, - fetch, + doFetch, waitForServerAndCli, wait, TimeCounter, @@ -44,7 +44,7 @@ describe("interactive CLI", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -52,12 +52,12 @@ describe("interactive CLI", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); @@ -75,7 +75,7 @@ describe("interactive CLI", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -83,17 +83,17 @@ describe("interactive CLI", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return not found for /api/users/3 path", async () => { - const usersResponse = await fetch("/api/users/3"); + const usersResponse = await doFetch("/api/users/3"); expect(usersResponse.status).toEqual(404); }); }); @@ -114,7 +114,7 @@ describe("interactive CLI", () => { await mocks.cursorDown(7); await mocks.pressEnter(); await wait(500); - await fetch("/api/users"); + await doFetch("/api/users"); await wait(1000); expect(mocks.currentScreen).toEqual(expect.stringContaining("Displaying logs")); expect(mocks.currentScreen).toEqual( @@ -136,7 +136,7 @@ describe("interactive CLI", () => { it("should respond after defined delay", async () => { expect.assertions(2); const timeCounter = new TimeCounter(); - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); timeCounter.stop(); expect(timeCounter.total).toBeGreaterThan(1999); expect(users.body).toEqual([ diff --git a/test/plugin-inquirer-cli-e2e/src/main/support/helpers.js b/test/plugin-inquirer-cli-e2e/src/main/support/helpers.js index 4fdae7498..3d933129a 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/support/helpers.js +++ b/test/plugin-inquirer-cli-e2e/src/main/support/helpers.js @@ -38,7 +38,7 @@ const serverUrl = (port) => { return `http://127.0.0.1:${port || SERVER_PORT}`; }; -const fetch = (uri, options = {}) => { +const doFetch = (uri, options = {}) => { const requestOptions = { ...defaultRequestOptions, ...options, @@ -111,7 +111,7 @@ function pathJoin(...args) { } module.exports = { - fetch, + doFetch, TimeCounter, mocksRunner, wait, diff --git a/test/plugin-inquirer-cli-e2e/src/main/web-tutorial.spec.js b/test/plugin-inquirer-cli-e2e/src/main/web-tutorial.spec.js index 93ab0feb5..f85e850d5 100644 --- a/test/plugin-inquirer-cli-e2e/src/main/web-tutorial.spec.js +++ b/test/plugin-inquirer-cli-e2e/src/main/web-tutorial.spec.js @@ -8,7 +8,7 @@ http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -const { mocksRunner, fetch, waitForServerAndCli } = require("./support/helpers"); +const { mocksRunner, doFetch, waitForServerAndCli } = require("./support/helpers"); describe("web tutorial", () => { jest.setTimeout(15000); @@ -33,7 +33,7 @@ describe("web tutorial", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -41,12 +41,12 @@ describe("web tutorial", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 1 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); }); @@ -60,7 +60,7 @@ describe("web tutorial", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -68,12 +68,12 @@ describe("web tutorial", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); @@ -87,7 +87,7 @@ describe("web tutorial", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -95,17 +95,17 @@ describe("web tutorial", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return not found for /api/users/3 path", async () => { - const usersResponse = await fetch("/api/users/3"); + const usersResponse = await doFetch("/api/users/3"); expect(usersResponse.status).toEqual(404); }); }); @@ -122,7 +122,7 @@ describe("web tutorial", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -130,17 +130,17 @@ describe("web tutorial", () => { }); it("should serve user 2 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return user 2 for /api/users/3 path", async () => { - const users = await fetch("/api/users/3"); + const users = await doFetch("/api/users/3"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); }); @@ -154,7 +154,7 @@ describe("web tutorial", () => { }); it("should serve users collection mock under the /api/users path", async () => { - const users = await fetch("/api/users"); + const users = await doFetch("/api/users"); expect(users.body).toEqual([ { id: 1, name: "John Doe" }, { id: 2, name: "Jane Doe" }, @@ -162,17 +162,17 @@ describe("web tutorial", () => { }); it("should serve user 1 under the /api/users/1 path", async () => { - const users = await fetch("/api/users/1"); + const users = await doFetch("/api/users/1"); expect(users.body).toEqual({ id: 1, name: "John Doe" }); }); it("should serve user 2 under the /api/users/2 path", async () => { - const users = await fetch("/api/users/2"); + const users = await doFetch("/api/users/2"); expect(users.body).toEqual({ id: 2, name: "Jane Doe" }); }); it("should return not found for /api/users/3 path", async () => { - const usersResponse = await fetch("/api/users/3"); + const usersResponse = await doFetch("/api/users/3"); expect(usersResponse.status).toEqual(404); }); }); From 4312e5abf5def8fb2499f12add85073a5760800a Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 10:38:11 +0200 Subject: [PATCH 16/18] chore: Do not build branch --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 45ed2f4a8..f2583e32c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,7 +6,6 @@ on: branches: - master - release - - feat-variants-format pull_request: jobs: get-affected: From 150dd4081599ea88fc95f0e969379b37e3d46cce Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 11:04:57 +0200 Subject: [PATCH 17/18] style: Fix Sonar smell --- packages/core/test/mocks/validations.spec.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core/test/mocks/validations.spec.js b/packages/core/test/mocks/validations.spec.js index 975544c14..a7a8bc0b2 100644 --- a/packages/core/test/mocks/validations.spec.js +++ b/packages/core/test/mocks/validations.spec.js @@ -371,7 +371,9 @@ describe("mocks validations", () => { }); describe("variantValidationErrors using Middleware handler schema", () => { - const EMPTY_MIDDLEWARE = () => {}; + const EMPTY_MIDDLEWARE = () => { + // do nothing + }; it("should return null if variant is valid", () => { expect( From 364d8e73c697b2e0502ce46c73acf228f57cf3bb Mon Sep 17 00:00:00 2001 From: javierbrea Date: Tue, 5 Jul 2022 12:16:40 +0200 Subject: [PATCH 18/18] chore(release): Upgrade versions --- packages/admin-api-client/CHANGELOG.md | 2 +- packages/admin-api-client/package.json | 2 +- packages/admin-api-client/sonar-project.properties | 2 +- packages/core/CHANGELOG.md | 2 +- packages/core/package.json | 2 +- packages/core/sonar-project.properties | 2 +- packages/core/src/mocks/helpers.js | 2 +- packages/cypress-commands/CHANGELOG.md | 5 +++++ packages/cypress-commands/package.json | 2 +- packages/cypress-commands/sonar-project.properties | 2 +- packages/logger/CHANGELOG.md | 2 +- packages/logger/package.json | 2 +- packages/logger/sonar-project.properties | 2 +- packages/main/CHANGELOG.md | 6 ++++++ packages/main/package.json | 2 +- packages/main/sonar-project.properties | 2 +- packages/plugin-proxy/CHANGELOG.md | 3 ++- packages/plugin-proxy/package.json | 2 +- packages/plugin-proxy/sonar-project.properties | 2 +- 19 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/admin-api-client/CHANGELOG.md b/packages/admin-api-client/CHANGELOG.md index 072afb235..70d175285 100644 --- a/packages/admin-api-client/CHANGELOG.md +++ b/packages/admin-api-client/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Removed ### Breaking change -## [unreleased] +## [5.0.2] - 2022-07-05 ### Changed - refactor: Rename fetch variable into crossFetch diff --git a/packages/admin-api-client/package.json b/packages/admin-api-client/package.json index 0efdf3805..b24223e32 100644 --- a/packages/admin-api-client/package.json +++ b/packages/admin-api-client/package.json @@ -1,6 +1,6 @@ { "name": "@mocks-server/admin-api-client", - "version": "5.0.1", + "version": "5.0.2", "description": "Client of @mocks-server/plugin-admin-api", "keywords": [ "mocks-server", diff --git a/packages/admin-api-client/sonar-project.properties b/packages/admin-api-client/sonar-project.properties index a171a3de0..9a1efe20a 100644 --- a/packages/admin-api-client/sonar-project.properties +++ b/packages/admin-api-client/sonar-project.properties @@ -1,7 +1,7 @@ sonar.organization=mocks-server sonar.projectKey=mocks-server_main_admin-api-client sonar.projectName=admin-api-client -sonar.projectVersion=5.0.1 +sonar.projectVersion=5.0.2 sonar.javascript.file.suffixes=.js sonar.sourceEncoding=UTF-8 diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 12390efbd..b958c58f7 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed ### Removed -## [unreleased] +## [3.5.0] - 2022-07-05 ### Added - feat(#335): Pass only response property from variants to route variant handlers having the "version" property defined as "4". If it has another value, pass the whole variant object (for backward compatibility) diff --git a/packages/core/package.json b/packages/core/package.json index 72c2f0c1d..3b9522429 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@mocks-server/core", - "version": "3.4.0", + "version": "3.5.0", "description": "Pluggable mock server supporting multiple route variants and mocks", "keywords": [ "mocks", diff --git a/packages/core/sonar-project.properties b/packages/core/sonar-project.properties index cf681fe58..d2fffa787 100644 --- a/packages/core/sonar-project.properties +++ b/packages/core/sonar-project.properties @@ -1,7 +1,7 @@ sonar.organization=mocks-server sonar.projectKey=mocks-server_main_core sonar.projectName=core -sonar.projectVersion=3.4.0 +sonar.projectVersion=3.5.0 sonar.javascript.file.suffixes=.js sonar.sourceEncoding=UTF-8 diff --git a/packages/core/src/mocks/helpers.js b/packages/core/src/mocks/helpers.js index bc734c940..0fdf120f9 100644 --- a/packages/core/src/mocks/helpers.js +++ b/packages/core/src/mocks/helpers.js @@ -222,7 +222,7 @@ function getVariantHandler({ if (Handler.deprecated) { handlersAlerts.set( Handler.id, - `Handler '${Handler.id}' is deprecated and will be removed in next major version. Consider using another handler.` + `Handler '${Handler.id}' is deprecated and will be removed in next major version. Consider using another handler. https://www.mocks-server.org/docs/guides-migrating-from-v3#route-variants-handlers` ); } routeHandler = new Handler( diff --git a/packages/cypress-commands/CHANGELOG.md b/packages/cypress-commands/CHANGELOG.md index cd1eb67a5..85b43b8d5 100644 --- a/packages/cypress-commands/CHANGELOG.md +++ b/packages/cypress-commands/CHANGELOG.md @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed ### Removed +## [4.0.2] - 2022-07-05 + +### Changed +- chore(deps): Update @mocks-server/admin-api-client + ## [4.0.1] - 2022-06-03 ### Changed diff --git a/packages/cypress-commands/package.json b/packages/cypress-commands/package.json index abe3c5376..05df4c37d 100644 --- a/packages/cypress-commands/package.json +++ b/packages/cypress-commands/package.json @@ -1,6 +1,6 @@ { "name": "@mocks-server/cypress-commands", - "version": "4.0.1", + "version": "4.0.2", "description": "Extends Cypress' cy commands with methods for administrating Mocks Server", "keywords": [ "cypress", diff --git a/packages/cypress-commands/sonar-project.properties b/packages/cypress-commands/sonar-project.properties index 07709f05e..82faeeeda 100644 --- a/packages/cypress-commands/sonar-project.properties +++ b/packages/cypress-commands/sonar-project.properties @@ -1,7 +1,7 @@ sonar.organization=mocks-server sonar.projectKey=mocks-server_main_cypress-commands sonar.projectName=cypress-commands -sonar.projectVersion=4.0.1 +sonar.projectVersion=4.0.2 sonar.javascript.file.suffixes=.js sonar.sourceEncoding=UTF-8 diff --git a/packages/logger/CHANGELOG.md b/packages/logger/CHANGELOG.md index 95f4cb25b..fcbb9b528 100644 --- a/packages/logger/CHANGELOG.md +++ b/packages/logger/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed ### Removed -## [unreleased] +## [1.1.0] - 2022-07-05 ### Changed - feat: Increase winston-array-transport maxEventListeners diff --git a/packages/logger/package.json b/packages/logger/package.json index b7c226c72..7ab5433ae 100644 --- a/packages/logger/package.json +++ b/packages/logger/package.json @@ -1,6 +1,6 @@ { "name": "@mocks-server/logger", - "version": "1.0.0", + "version": "1.1.0", "description": "Namespaced logger", "keywords": [ "log", diff --git a/packages/logger/sonar-project.properties b/packages/logger/sonar-project.properties index c99cffb09..627560050 100644 --- a/packages/logger/sonar-project.properties +++ b/packages/logger/sonar-project.properties @@ -1,7 +1,7 @@ sonar.organization=mocks-server sonar.projectKey=mocks-server_main_logger sonar.projectName=logger -sonar.projectVersion=1.0.0 +sonar.projectVersion=1.1.0 sonar.javascript.file.suffixes=.js sonar.sourceEncoding=UTF-8 diff --git a/packages/main/CHANGELOG.md b/packages/main/CHANGELOG.md index 57f69c9f7..17ff82381 100644 --- a/packages/main/CHANGELOG.md +++ b/packages/main/CHANGELOG.md @@ -14,6 +14,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Removed ### Breaking change +## [3.5.0] - 2022-07-05 + +### Changed +- chore(deps): Update core dependency to v3.5.0 +- chore(deps): Update plugin-proxy dependency to v3.0.0 + ## [3.4.0] - 2022-07-01 ### Changed diff --git a/packages/main/package.json b/packages/main/package.json index 0559ff3c8..c7128a42b 100644 --- a/packages/main/package.json +++ b/packages/main/package.json @@ -1,6 +1,6 @@ { "name": "@mocks-server/main", - "version": "3.4.0", + "version": "3.5.0", "description": "Mock Server supporting multiple route variants and mocks", "keywords": [ "mock", diff --git a/packages/main/sonar-project.properties b/packages/main/sonar-project.properties index ef87414cb..6870dcafd 100644 --- a/packages/main/sonar-project.properties +++ b/packages/main/sonar-project.properties @@ -1,7 +1,7 @@ sonar.organization=mocks-server sonar.projectKey=mocks-server_main sonar.projectName=main -sonar.projectVersion=3.4.0 +sonar.projectVersion=3.5.0 sonar.javascript.file.suffixes=.js sonar.sourceEncoding=UTF-8 diff --git a/packages/plugin-proxy/CHANGELOG.md b/packages/plugin-proxy/CHANGELOG.md index c6a2b806f..ee5e7bb83 100644 --- a/packages/plugin-proxy/CHANGELOG.md +++ b/packages/plugin-proxy/CHANGELOG.md @@ -12,10 +12,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed ### Removed -## [unreleased] +## [3.0.0] - 2022-07-05 ### Changed - feat: Add routes handler "proxy-v4" using new handlers API released on @mocks-server/core@3.5.0. Keep old "legacy" handler for backward compatibility. +- chore(deps): Require @mocks-server/core >=3.5.0 in peerDependencies ## [2.1.0] ### Changed diff --git a/packages/plugin-proxy/package.json b/packages/plugin-proxy/package.json index ab913bae6..f406dd670 100644 --- a/packages/plugin-proxy/package.json +++ b/packages/plugin-proxy/package.json @@ -1,6 +1,6 @@ { "name": "@mocks-server/plugin-proxy", - "version": "2.1.0", + "version": "3.0.0", "description": "Mocks Server plugin providing http-proxy route handler", "keywords": [ "mocks-server-plugin", diff --git a/packages/plugin-proxy/sonar-project.properties b/packages/plugin-proxy/sonar-project.properties index 6e2d2482a..336150b4c 100644 --- a/packages/plugin-proxy/sonar-project.properties +++ b/packages/plugin-proxy/sonar-project.properties @@ -1,7 +1,7 @@ sonar.organization=mocks-server sonar.projectKey=mocks-server_main_plugin-proxy sonar.projectName=plugin-proxy -sonar.projectVersion=2.1.0 +sonar.projectVersion=3.0.0 sonar.javascript.file.suffixes=.js sonar.sourceEncoding=UTF-8