This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+19
View File
@@ -0,0 +1,19 @@
const schemaMatcher = require('./schema-matcher');
const statusCodeMatcher = require('./status-matcher');
const coverage = require('../../helpers/coverage');
const { messages } = require('../../helpers/common');
module.exports = function getChaiPlugin(options) {
if (!(options instanceof Object) || !options.apiDefinitionsPath) {
throw new Error(messages.REQUIRED_API_DEFINITIONS_PATH);
}
coverage.init(options);
return function apiSchemaPlugin(chai) {
const { Assertion } = chai;
schemaMatcher(Assertion, options);
statusCodeMatcher(Assertion);
};
};
@@ -0,0 +1,20 @@
const validators = require('../../validators');
module.exports = (Assertion, options) => {
Assertion.addMethod('matchApiSchema', function addApiSchemaMethod(apiDefinitionsPath) {
const {
predicate, actual, expected, matchMsg, noMatchMsg,
} = validators.schemaValidator(
this._obj,
{ ...options, apiDefinitionsPath: apiDefinitionsPath || options.apiDefinitionsPath },
);
this.assert(
predicate,
matchMsg,
noMatchMsg,
expected,
actual,
);
});
};
@@ -0,0 +1,31 @@
const validators = require('../../validators');
module.exports = (Assertion) => {
buildMatcher('successful', 200);
buildMatcher('created', 201);
buildMatcher('badRequest', 400);
buildMatcher('unauthorized', 401);
buildMatcher('forbidden', 403);
buildMatcher('notFound', 404);
buildMatcher('serverError', 500);
buildMatcher('serviceUnavailable', 503);
buildMatcher('gatewayTimeout', 504);
buildMatcher('status');
function buildMatcher(str, statusToTest) {
Assertion.addMethod(str, function statusCodeMatcher(customStatus) {
const expectedStatus = statusToTest || customStatus;
const {
predicate, actual, expected, matchMsg, noMatchMsg,
} = validators.statusValidator(expectedStatus, this._obj);
this.assert(
predicate,
matchMsg,
noMatchMsg,
expected,
actual,
);
});
}
};
+14
View File
@@ -0,0 +1,14 @@
const schemaMatcher = require('./schema-matcher');
const statusCodeMatcher = require('./status-matcher');
const coverage = require('../../helpers/coverage');
const { messages } = require('../../helpers/common');
module.exports = function apiSchemaPlugin(options) {
if (!(options instanceof Object) || !options.apiDefinitionsPath) {
throw new Error(messages.REQUIRED_API_DEFINITIONS_PATH);
}
coverage.init(options);
schemaMatcher(options);
statusCodeMatcher();
};
@@ -0,0 +1,41 @@
const diff = require('jest-diff').default;
const matcherUtils = require('jest-matcher-utils');
const validators = require('../../validators');
module.exports = (options) => {
expect.extend(
{
toMatchApiSchema: (validatedRequest, apiDefinitionsPath) => {
const {
predicate, expected, actual,
} = validators.schemaValidator(
validatedRequest,
{ ...options, apiDefinitionsPath: apiDefinitionsPath || options.apiDefinitionsPath },
);
const pass = predicate;
const message = pass
? () => `${matcherUtils.matcherHint('toMatchApiSchema')
}\n\n`
+ `Expected: ${matcherUtils.printExpected(expected)}\n`
+ `Received: ${matcherUtils.printReceived(actual)}`
: () => {
const difference = diff(expected, actual, {
expand: this.expand,
});
return (
`${matcherUtils.matcherHint('toMatchApiSchema')
}\n\n${
difference && difference.includes('- Expect')
? `Difference:\n\n${difference}`
: `Expected: ${matcherUtils.printExpected(expected)}\n`
+ `Received: ${matcherUtils.printReceived(actual)}`}`
);
};
return { actual, message, pass };
},
},
);
};
@@ -0,0 +1,51 @@
const diff = require('jest-diff');
const matcherUtils = require('jest-matcher-utils');
const validators = require('../../validators');
module.exports = () => {
buildMatcher('toBeSuccessful', 200);
buildMatcher('toBeCreated', 201);
buildMatcher('toBeBadRequest', 400);
buildMatcher('toBeUnauthorized', 401);
buildMatcher('toBeForbidden', 403);
buildMatcher('toBeNotFound', 404);
buildMatcher('toBeServerError', 500);
buildMatcher('toBeServiceUnavailable', 503);
buildMatcher('toBeGatewayTimeout', 504);
buildMatcher('toHaveStatus');
function buildMatcher(str, statusToTest) {
expect.extend({
[str]: (validatedResponse, customStatus) => {
const expectedStatus = statusToTest || customStatus;
const {
predicate, expected, actual,
} = validators.statusValidator(expectedStatus, validatedResponse);
const pass = predicate;
const message = pass
? () => `${matcherUtils.matcherHint(str)
}\n\n`
+ `Expected: ${matcherUtils.printExpected(expected)}\n`
+ `Received: ${matcherUtils.printReceived(actual)}`
: () => {
const difference = diff(expected, actual, {
expand: this.expand,
});
return (
`${matcherUtils.matcherHint(str)
}\n\n${
difference && difference.includes('- Expect')
? `Difference:\n\n${difference}`
: `Expected: ${matcherUtils.printExpected(expected)}\n`
+ `Received: ${matcherUtils.printReceived(actual)}`}`
);
};
return { actual, message, pass };
},
});
}
};
+16
View File
@@ -0,0 +1,16 @@
const schemaMatcher = require('./schema-matcher');
const statusCodeMatcher = require('./status-matcher');
const coverage = require('../../helpers/coverage');
const { messages } = require('../../helpers/common');
module.exports = function apiSchemaPlugin(should, options) {
const Assertion = should.Assertion || should;
if (!(options instanceof Object) || !options.apiDefinitionsPath) {
throw new Error(messages.REQUIRED_API_DEFINITIONS_PATH);
}
coverage.init(options);
schemaMatcher(Assertion, options);
statusCodeMatcher(Assertion);
};
@@ -0,0 +1,20 @@
const validators = require('../../validators');
module.exports = (Assertion, options) => {
Assertion.add('matchApiSchema', function addApiSchemaMethod(apiDefinitionsPath) {
const {
predicate, actual, expected, matchMsg,
} = validators.schemaValidator(
this.obj,
{ ...options, apiDefinitionsPath: apiDefinitionsPath || options.apiDefinitionsPath },
);
this.params = {
message: matchMsg,
expected,
actual,
};
predicate.should.be.true();
});
};
@@ -0,0 +1,31 @@
const validators = require('../../validators');
module.exports = (Assertion) => {
buildMatcher('successful', 200);
buildMatcher('created', 201);
buildMatcher('badRequest', 400);
buildMatcher('unauthorized', 401);
buildMatcher('forbidden', 403);
buildMatcher('notFound', 404);
buildMatcher('serverError', 500);
buildMatcher('serviceUnavailable', 503);
buildMatcher('gatewayTimeout', 504);
buildMatcher('status');
function buildMatcher(str, statusToTest) {
Assertion.add(str, function statusCodeMatcher(customStatus) {
const expectedStatus = statusToTest || customStatus;
const {
predicate, actual, expected, matchMsg,
} = validators.statusValidator(expectedStatus, this.obj);
this.params = {
message: matchMsg,
expected,
actual,
};
predicate.should.be.true();
});
}
};