@opra/testing
Version:
Opra testing package
76 lines (75 loc) • 3.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const ansi_colors_1 = tslib_1.__importDefault(require("ansi-colors"));
const expect_1 = require("expect");
const jest_matcher_utils_1 = require("jest-matcher-utils");
expect_1.expect.extend({
toHaveFields(received, expected) {
const expectedKeys = (Array.isArray(expected) ? expected : Object.keys(expected)).map(x => x.toLowerCase());
const objectKeys = Object.keys(received).map(x => x.toLowerCase());
const filteredKeys = expectedKeys.filter(x => !objectKeys.includes(x));
const pass = !filteredKeys.length === !this.isNot;
if (!pass) {
const message = () => `Expects keys ${this.isNot ? 'not ' : ''}to contain: ${ansi_colors_1.default.yellow('' + expectedKeys)}\n` +
`${this.isNot ? 'Unsolicited' : 'Missing'} fields: ${ansi_colors_1.default.yellow('' + filteredKeys)}\n`;
return { message, pass: !!this.isNot };
}
return { actual: received, pass: !this.isNot, message: () => '' };
},
toHaveFieldsOnly(received, expected) {
const expectedKeys = (Array.isArray(expected) ? expected : Object.keys(expected)).map(x => x.toLowerCase());
const objectKeys = Object.keys(received).map(x => x.toLowerCase());
const filteredKeys = objectKeys.filter(x => !expectedKeys.includes(x));
const pass = !filteredKeys.length === !this.isNot;
if (!pass) {
const message = () => `${!this.isNot ? 'Do not expects' : 'Expects'} additional keys other than: ${ansi_colors_1.default.yellow('' + expectedKeys)}\n` +
(filteredKeys
? `Additional keys received: ${ansi_colors_1.default.yellow('' + filteredKeys)}\n`
: 'No additional keys received\n');
return { message, pass };
}
return { actual: received, pass: !this.isNot, message: () => '' };
},
toBeArray(received) {
if (Array.isArray(received)) {
return { actual: received, pass: true, message: () => '' };
}
return {
pass: false,
message: () => 'Value is not an array',
};
},
toBeGreaterThanAny(received, expected) {
return compare('toBeGreaterThan', {
isNot: this.isNot,
promise: this.promise,
}, received, expected, '>', () => received > expected);
},
toBeGreaterThanOrEqualAny(received, expected) {
return compare('toBeGreaterThanOrEqual', {
isNot: this.isNot,
promise: this.promise,
}, received, expected, '>=', () => received >= expected);
},
toBeLessThanAny(received, expected) {
return compare('toBeLessThan', {
isNot: this.isNot,
promise: this.promise,
}, received, expected, '<', () => received < expected);
},
toBeLessThanOrEqualAny(received, expected) {
return compare('toBeLessThanOrEqual', {
isNot: this.isNot,
promise: this.promise,
}, received, expected, '<=', () => received <= expected);
},
});
function compare(matcherName, options, received, expected, operator, fn) {
const pass = fn(received, expected);
const message = () => (0, jest_matcher_utils_1.matcherHint)(matcherName, undefined, undefined, options) +
'\n\n' +
`Expected:${options.isNot ? ' not' : ''} ${operator} ${(0, jest_matcher_utils_1.printExpected)(expected)}\n` +
`Received:${options.isNot ? ' ' : ''} ${(0, jest_matcher_utils_1.printReceived)(received)}`;
return { message, pass };
}