@case-contract-testing/case
Version:
Next-generation contract testing suite
46 lines (45 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShapedObjectExecutor = void 0;
const context_1 = require("../../entities/context");
const results_1 = require("../../entities/results");
const strip = (matcher, matchContext) => Object.entries(matcher['case:matcher:children'])
.map(([expectedKey, expectedValueMatcher]) => ({
[expectedKey]: matchContext.descendAndStrip(expectedValueMatcher, (0, context_1.addLocation)(expectedKey, matchContext)),
}))
.reduce((acc, entry) => ({
...acc,
...entry,
}), {});
const whyNotAnObject = (actual) => {
if (typeof actual !== 'object') {
return `Expected an object, but the type was '${typeof actual}' instead`;
}
if (Array.isArray(actual)) {
return 'Expected an object, but it was an array';
}
if (actual != null) {
return 'Expected an object, but it was null or undefined';
}
if (actual !== Object(actual)) {
return `Expected an object, but '${(0, results_1.actualToString)(actual)}' is not an object because the Object constructor doesn't return a reference to it`;
}
return `If you are seeing this message, there is a bug in whyNotAnObject, where it can't see a reason that '${(0, results_1.actualToString)(actual)} is not an Object.`;
};
const check = async (matcher, matchContext, actual) => [
...(typeof actual === 'object' &&
actual === Object(actual) &&
!Array.isArray(actual) &&
actual != null
? (0, results_1.combineResults)((await Promise.all(Object.entries(matcher['case:matcher:children']).map(([expectedKey, expectedValueMatcher]) => expectedKey in actual
? Promise.resolve(matchContext.descendAndCheck(expectedValueMatcher, (0, context_1.addLocation)(expectedKey, matchContext), actual[expectedKey]))
: Promise.resolve((0, results_1.makeResults)((0, results_1.matchingError)(matcher, `missing key '${expectedKey}' in object: ${(0, results_1.actualToString)(actual)}`, actual, matchContext)))))).flat())
: (0, results_1.makeResults)((0, results_1.matchingError)(matcher, whyNotAnObject(actual), actual, matchContext))),
];
exports.ShapedObjectExecutor = {
describe: (matcher, context) => `an object shaped like {${Object.entries(matcher['case:matcher:children'])
.map(([key, child]) => `${key}: ${context.descendAndDescribe(child, (0, context_1.addLocation)(key, context))}`)
.join(',')}}`,
check,
strip,
};