UNPKG

@opra/testing

Version:

Opra testing package

73 lines (72 loc) 3.39 kB
import colors from 'ansi-colors'; import { expect } from 'expect'; import { matcherHint, printExpected, printReceived, } from 'jest-matcher-utils'; 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: ${colors.yellow('' + expectedKeys)}\n` + `${this.isNot ? 'Unsolicited' : 'Missing'} fields: ${colors.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: ${colors.yellow('' + expectedKeys)}\n` + (filteredKeys ? `Additional keys received: ${colors.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 = () => matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected:${options.isNot ? ' not' : ''} ${operator} ${printExpected(expected)}\n` + `Received:${options.isNot ? ' ' : ''} ${printReceived(received)}`; return { message, pass }; }