JamieMason/expect-more

View on GitHub
packages/expect-more-jasmine/src/to-be-array-including-any-of.ts

Summary

Maintainability
B
4 hrs
Test Coverage
/// <reference types="jasmine" />

import { isArrayIncludingAnyOf } from 'expect-more';
import { printExpected, printReceived } from 'jest-matcher-utils';

declare global {
  namespace jasmine {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    interface Matchers<T> {
      /**
       * Asserts that `value` is an `Array` including at least one of the members of `allowedValues`.
       * @example
       * expect([12, 0, 14, 'Ginola']).toBeArrayIncludingAnyOf(['Ginola', 3]);
       */
      toBeArrayIncludingAnyOf(allowedValues: unknown[]): boolean;
    }
  }
}

export const toBeArrayIncludingAnyOfMatcher: jasmine.CustomMatcherFactory = () => {
  return {
    compare(value: unknown, allowedValues: unknown[]) {
      const pass = isArrayIncludingAnyOf(allowedValues, value);
      const message = pass
        ? `expected ${printReceived(
            value,
          )} not to include at least one of the values in ${printExpected(allowedValues)}`
        : `expected ${printReceived(
            value,
          )} to include at least one of the values in ${printExpected(allowedValues)}`;
      return { message, pass };
    },
  };
};

beforeAll(() => {
  jasmine.addMatchers({
    toBeArrayIncludingAnyOf: toBeArrayIncludingAnyOfMatcher,
  });
});