@iamkenos/iris
Version:
Test API endpoints with Axios & Jest using a collection of custom matchers and built-in utility functions.
72 lines • 3.91 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const json_schema_ref_parser_1 = __importDefault(require("@apidevtools/json-schema-ref-parser"));
const ajv_1 = __importDefault(require("ajv"));
const ajv_errors_1 = __importDefault(require("ajv-errors"));
const ajv_formats_1 = __importDefault(require("ajv-formats"));
const _common_1 = require("../common/index");
expect.extend({
// @ts-ignore
async toMatchJsonSchema(received, filename) {
const ajv = new ajv_1.default({ allErrors: true });
(0, ajv_errors_1.default)(ajv);
(0, ajv_formats_1.default)(ajv);
const matcherName = "toMatchJsonSchema";
const options = {
comment: "json schema matching",
isNot: this.isNot,
promise: this.promise
};
const snapshot = iris.config.snapshots.schema;
const output = JSON.stringify((0, _common_1.isJSON)(received) ? JSON.parse(received) : received);
const actFile = path_1.default.join(snapshot.actualDir, filename) + ".json";
const expFile = path_1.default.join(snapshot.expectedDir, filename) + ".json";
fs_extra_1.default.outputFileSync(actFile, output);
const actContent = fs_extra_1.default.readFileSync(actFile, _common_1.BufferEncoding.UTF8);
const expContent = fs_extra_1.default.readFileSync(expFile, _common_1.BufferEncoding.UTF8);
const actual = JSON.parse(actContent);
const expected = JSON.parse(expContent);
const canRead = /^schema:\/\//i;
const customResolver = {
canRead,
/** see: [custom resolvers](https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html) */
read(file) {
const uri = path_1.default.join(snapshot.expectedDir, file.url.replace(canRead, ""));
const content = fs_extra_1.default.readFileSync(uri);
return content;
}
};
// we need the dereferencer because ajv doesn't support local $refs out of the box
const schema = await json_schema_ref_parser_1.default.dereference(expected, { resolve: { schema: customResolver } });
const validator = ajv.compile(schema);
const pass = validator(actual);
const errors = validator.errors?.map((e) => {
return {
field: e.instancePath,
value: e.data,
message: e.message
};
});
const message = pass
? () => this.utils.matcherHint(matcherName, undefined, undefined, options) +
"\n\n" +
`Expected: not ${this.utils.RECEIVED_COLOR(JSON.stringify(expected, null, 2))}\n` +
`Received: ${this.utils.EXPECTED_COLOR(JSON.stringify(actual, null, 2))}`
: () => this.utils.matcherHint(matcherName, undefined, undefined, options) +
"\n\n" +
`Schema mismatch: ${this.utils.RECEIVED_COLOR(JSON.stringify(errors, null, 2))}\n` +
`Expected: ${this.utils.EXPECTED_COLOR(JSON.stringify(expected, null, 2))}\n` +
`Received: ${this.utils.RECEIVED_COLOR(JSON.stringify(actual, null, 2))}`;
if (!pass && !this.isNot) {
_common_1.Reporter.addAttachment(`Actual: ${actFile}`, JSON.stringify(actual, null, 2), _common_1.MimeType.APP_JSON);
_common_1.Reporter.addAttachment(`Expected: ${actFile}`, JSON.stringify(expected, null, 2), _common_1.MimeType.APP_JSON);
}
return { actual: received, expected, message, name: matcherName, pass };
}
});
//# sourceMappingURL=match-json-schema.js.map