Skip to content

Commit

Permalink
Merge pull request #359 from mocks-server/release
Browse files Browse the repository at this point in the history
Release v3.5.0
  • Loading branch information
javierbrea authored Jul 5, 2022
2 parents 9903f60 + 364d8e7 commit 2ffe8b1
Show file tree
Hide file tree
Showing 291 changed files with 8,487 additions and 909 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
/packages/*/dist
/packages/*/coverage
/test/core-e2e/src/fixtures
/test/core-e2e-legacy/src/fixtures
/test/main-e2e/src/fixtures
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = {
describe: true,
expect: true,
it: true,
fetch: false,
},
plugins: ["jest", "no-only-tests"],
extends: ["plugin:jest/recommended"],
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ jobs:
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install
- name: Generate graph
run: pnpm run graph -- --file=graph.json
- name: Lint
run: pnpm run lint
test-unit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ module.exports = [
variants: [
{
id: "1",
handler: "json",
response: {
status: 200,
body: [{ email: "[email protected]" }],
},
},
{
id: "2",
handler: "json",
response: {
status: 200,
body: [{ email: "[email protected]" }],
Expand Down
2 changes: 2 additions & 0 deletions mocks/admin-api-client-unit-mocks/mocks/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ module.exports = [
variants: [
{
id: "1",
handler: "json",
response: {
status: 200,
body: [{ email: "[email protected]" }],
},
},
{
id: "2",
handler: "json",
response: {
status: 200,
body: [{ email: "[email protected]" }],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions packages/admin-api-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed
### Breaking change

## [5.0.2] - 2022-07-05

### Changed
- refactor: Rename fetch variable into crossFetch

## [5.0.1] - 2022-06-03

### Changed
Expand Down
2 changes: 1 addition & 1 deletion packages/admin-api-client/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/admin-api-client/sonar-project.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions packages/admin-api-client/src/entities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from "cross-fetch";
import crossFetch from "cross-fetch";

import {
DEFAULT_BASE_PATH,
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
6 changes: 3 additions & 3 deletions packages/admin-api-client/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,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: "[email protected]" }], status: 200 },
delay: null,
},
{
id: "get-user:2",
routeId: "get-user",
handler: "default",
handler: "json",
response: { body: [{ email: "[email protected]" }], status: 200 },
delay: null,
},
Expand All @@ -191,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: "[email protected]" }], status: 200 },
delay: null,
});
Expand Down
11 changes: 11 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
### Removed

## [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)
- feat(#336): Add "Json" and "Middleware" variant handlers.
- 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
- 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
Expand Down
2 changes: 1 addition & 1 deletion packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {

// The glob patterns Jest uses to detect test files
testMatch: ["<rootDir>/test/**/*.spec.js"],
// testMatch: ["<rootDir>/test/**/Core.spec.js"],
// testMatch: ["<rootDir>/test/**/mocks/helpers.spec.js"],

// The test environment that will be used for testing
testEnvironment: "node",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/sonar-project.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/mocks/Mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`);
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/mocks/Mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Mocks {
mocksDefinitions: this._mocksDefinitions,
alerts: this._alertsMocks,
logger: this._loggerLoadMocks,
loggerRoutes: this._loggerRoutes,
routeVariants: this._routesVariants,
getGlobalDelay: this.getDelay,
});
Expand Down Expand Up @@ -212,6 +213,7 @@ class Mocks {
getGlobalDelay: this.getDelay,
alerts,
logger: this._loggerLoadMocks,
loggerRoutes: this._loggerRoutes,
});
}

Expand Down
31 changes: 26 additions & 5 deletions packages/core/src/mocks/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ 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 { getDataFromVariant, getPreview } = require("../routes-handlers/helpers");
const CustomCore = require("../CustomCore");
const Mock = require("./Mock");
const {
Expand Down Expand Up @@ -135,11 +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: routeVariant.plainResponsePreview,
response: isUndefined(preview) ? null : preview,
delay: routeVariant.delay,
};
});
Expand Down Expand Up @@ -200,6 +202,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,
Expand All @@ -215,15 +218,23 @@ function getVariantHandler({
}

try {
const variantArgument = getDataFromVariant(variant, Handler);
if (Handler.deprecated) {
handlersAlerts.set(
Handler.id,
`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(
{
...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;
Expand All @@ -248,6 +259,7 @@ function getRouteVariants({
}) {
let routeIds = [];
alerts.clean();
alertsRoutes.clean();
return compact(
flatten(
routesDefinitions.map((route, index) => {
Expand Down Expand Up @@ -304,6 +316,7 @@ function getMock({
mocksDefinitions,
routeVariants,
logger,
loggerRoutes,
getGlobalDelay,
alerts,
}) {
Expand Down Expand Up @@ -335,7 +348,7 @@ function getMock({
routeVariants,
alerts
),
logger,
logger: loggerRoutes,
getDelay: getGlobalDelay,
});
} catch (error) {
Expand All @@ -344,7 +357,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 = [];
Expand All @@ -360,6 +380,7 @@ function getMocks({ mocksDefinitions, alerts, logger, routeVariants, getGlobalDe
routeVariants,
getGlobalDelay,
logger,
loggerRoutes,
alerts: alertsMock,
});
if (!mock) {
Expand Down
23 changes: 17 additions & 6 deletions packages/core/src/mocks/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -117,6 +119,7 @@ const routesSchema = {
],
},
},
// TODO, require "response" in all variants to be an object, do not allow additionalProperties
required: ["id"],
},
},
Expand Down Expand Up @@ -282,17 +285,25 @@ function variantValidationErrors(route, variant, Handler) {
return null;
}
const variantValidator = ajv.compile(Handler.validationSchema);
const isValid = variantValidator(variant);
const dataToCheck = getDataFromVariant(variant, Handler);
const dataMessage = isVersion4(Handler) ? "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,
};
}
Expand Down
Loading

0 comments on commit 2ffe8b1

Please sign in to comment.