@case-contract-testing/case
Version:
Next-generation contract testing suite
36 lines (35 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonStringifiedString = void 0;
const entities_1 = require("../../entities");
const context_1 = require("../../entities/context");
const results_1 = require("../../entities/results");
const check = (matcher, matchContext, actual) => {
if (typeof actual !== 'string') {
return (0, results_1.makeResults)((0, results_1.matchingError)(matcher, `${(0, results_1.actualToString)(actual)} is not a string; so it can't match a json stringified string`, actual, matchContext, '<A string that would parse as json>'));
}
let decodedActual;
try {
decodedActual = JSON.parse(actual);
}
catch (parseError) {
return (0, results_1.makeResults)((0, results_1.matchingError)(matcher, `${(0, results_1.actualToString)(actual)} failed to parse as json (${parseError.message})`, actual, matchContext, '<A string that would parse as json>'));
}
return matchContext.descendAndCheck(matcher['case:matcher:child'], (0, context_1.addLocation)(':jsonStringify', matchContext), decodedActual);
};
const strip = (matcher, matchContext) => {
const result = matchContext.descendAndStrip(matcher['case:matcher:child'], (0, context_1.addLocation)(':jsonStringify', matchContext));
let encoded;
try {
encoded = JSON.stringify(result);
}
catch (parseError) {
throw new entities_1.CaseConfigurationError(`Unable to json stringify '${result}' during stripMatchers (${parseError.message})`);
}
return encoded;
};
exports.JsonStringifiedString = {
describe: (matcher, matchContext) => `json stringified '${matchContext.descendAndDescribe(matcher['case:matcher:child'], (0, context_1.addLocation)(':jsonStringify', matchContext))}'`,
check,
strip,
};