All files / config matchers.js

65.52% Statements 19/29
24% Branches 6/25
50% Functions 4/8
66.67% Lines 18/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 7324x   24x   26x 26x 26x 26x   26x                               26x 97x 97x     26x                                         26x     6x   6x 6x     6x             6x       24x  
const diff = require('jest-diff');
 
const getMatchers = () => ({
  toEqualOn(received, expected, ...props) {
    let failedAt = '';
    let pass = false;
    let checkProps = props;
    Eif (!checkProps.length) {
      // use the expected object itself for props to check
      checkProps = Object.keys(expected);
    } else if (props.length === 1) {
      // first arg is an array
      if (props[0] instanceof Array) {
        checkProps = props[0];
      } else if (props[0] instanceof Object) {
        checkProps = Object.keys(props[0]).filter(p => p);
      }
    }
    // if (this.isNot) {
    //   pass = checkProps.some(p => {
    //     failedAt = p;
    //     return this.equal(received[p], expected[p]);
    //   });
    // }
    // else {
    pass = checkProps.every(p => {
      failedAt = p;
      return this.equals(received[p], expected[p]);
    });
    // }
    const message = pass
      ? () =>
          this.utils.matcherHint('.not.toEqualOn') +
          '\n\n' +
          `Expected prop '${failedAt}' to not equal (using expect.toEqual):\n` +
          `  ${this.utils.printExpected(expected[failedAt])}\n` +
          `Received:\n` +
          `  ${this.utils.printReceived(received[failedAt])}`
      : () => {
          const diffString = diff(expected, received, { expand: this.expand });
          return (
            this.utils.matcherHint('.toEqualOn') +
            '\n\n' +
            `Expected prop '${failedAt}' to equal (using expect.toEqual):\n` +
            `  ${this.utils.printExpected(expected[failedAt])}\n` +
            `Received:\n` +
            `  ${this.utils.printReceived(received[failedAt])}` +
            (diffString ? `\n\nDifference:\n\n${diffString}` : '')
          );
        };
 
    return { actual: received, pass, message };
  },
  toBeIterable(received, ignoreStrings = false) {
    const exists = received != null;
    const isIterable =
      exists && typeof received[Symbol.iterator] === 'function';
    const isMatch = !ignoreStrings
      ? isIterable
      : typeof received === 'string' ? false : isIterable;
    const message = () =>
      this.utils.matcherHint(`${isMatch ? '.not' : ''}.toBeIterable`) +
      '\n\n' +
      `Expected value ${isMatch ? 'not ' : ''}to be Iterable${
        ignoreStrings ? ' (ignoring strings)' : ''
      }, instead received:\n` +
      `  ${this.utils.printReceived(received)}`;
    return { actual: received, pass: isMatch, message };
  }
});
 
module.exports = getMatchers;