How to use jest-matchers - 10 common examples

To help you get started, we’ve selected a few jest-matchers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github w3tecch / aurelia-typescript-boilerplate / test / karma-bundle.js View on Github external
function installJestExpect() {
  const expect = require('jest-matchers');
  global.expect = expect;

  const jasmine = global.jasmine;
  jasmine.anything = expect.anything;
  jasmine.any = expect.any;
  jasmine.objectContaining = expect.objectContaining;
  jasmine.arrayContaining = expect.arrayContaining;
  jasmine.stringMatching = expect.stringMatching;
  jasmine.addMatchers = (jasmineMatchersObject) => {
    const jestMatchersObject = Object.create(null);
    Object.keys(jasmineMatchersObject).forEach(name => {
      jestMatchersObject[name] = function() {
        const result = jasmineMatchersObject[name](jasmine.matchersUtil, null);
        // if there is no 'negativeCompare', both should be handled by `compare`
        const negativeCompare = result.negativeCompare || result.compare;

        return this.isNot
          ? negativeCompare.apply(null, arguments)
          : result.compare.apply(null, arguments);
      };
    });
github w3tecch / aurelia-typescript-boilerplate / test / karma-bundle.js View on Github external
function installJestExpect() {
  const expect = require('jest-matchers');
  global.expect = expect;

  const jasmine = global.jasmine;
  jasmine.anything = expect.anything;
  jasmine.any = expect.any;
  jasmine.objectContaining = expect.objectContaining;
  jasmine.arrayContaining = expect.arrayContaining;
  jasmine.stringMatching = expect.stringMatching;
  jasmine.addMatchers = (jasmineMatchersObject) => {
    const jestMatchersObject = Object.create(null);
    Object.keys(jasmineMatchersObject).forEach(name => {
      jestMatchersObject[name] = function() {
        const result = jasmineMatchersObject[name](jasmine.matchersUtil, null);
        // if there is no 'negativeCompare', both should be handled by `compare`
        const negativeCompare = result.negativeCompare || result.compare;

        return this.isNot
          ? negativeCompare.apply(null, arguments)
          : result.compare.apply(null, arguments);
      };
github w3tecch / aurelia-typescript-boilerplate / test / karma-bundle.js View on Github external
function installJestExpect() {
  const expect = require('jest-matchers');
  global.expect = expect;

  const jasmine = global.jasmine;
  jasmine.anything = expect.anything;
  jasmine.any = expect.any;
  jasmine.objectContaining = expect.objectContaining;
  jasmine.arrayContaining = expect.arrayContaining;
  jasmine.stringMatching = expect.stringMatching;
  jasmine.addMatchers = (jasmineMatchersObject) => {
    const jestMatchersObject = Object.create(null);
    Object.keys(jasmineMatchersObject).forEach(name => {
      jestMatchersObject[name] = function() {
        const result = jasmineMatchersObject[name](jasmine.matchersUtil, null);
        // if there is no 'negativeCompare', both should be handled by `compare`
        const negativeCompare = result.negativeCompare || result.compare;

        return this.isNot
          ? negativeCompare.apply(null, arguments)
          : result.compare.apply(null, arguments);
      };
    });

    const expect = global.expect;
github w3tecch / aurelia-typescript-boilerplate / test / karma-bundle.js View on Github external
function installJestExpect() {
  const expect = require('jest-matchers');
  global.expect = expect;

  const jasmine = global.jasmine;
  jasmine.anything = expect.anything;
  jasmine.any = expect.any;
  jasmine.objectContaining = expect.objectContaining;
  jasmine.arrayContaining = expect.arrayContaining;
  jasmine.stringMatching = expect.stringMatching;
  jasmine.addMatchers = (jasmineMatchersObject) => {
    const jestMatchersObject = Object.create(null);
    Object.keys(jasmineMatchersObject).forEach(name => {
      jestMatchersObject[name] = function() {
        const result = jasmineMatchersObject[name](jasmine.matchersUtil, null);
        // if there is no 'negativeCompare', both should be handled by `compare`
        const negativeCompare = result.negativeCompare || result.compare;

        return this.isNot
          ? negativeCompare.apply(null, arguments)
          : result.compare.apply(null, arguments);
      };
    });
github w3tecch / aurelia-typescript-boilerplate / test / karma-bundle.js View on Github external
function installJestExpect() {
  const expect = require('jest-matchers');
  global.expect = expect;

  const jasmine = global.jasmine;
  jasmine.anything = expect.anything;
  jasmine.any = expect.any;
  jasmine.objectContaining = expect.objectContaining;
  jasmine.arrayContaining = expect.arrayContaining;
  jasmine.stringMatching = expect.stringMatching;
  jasmine.addMatchers = (jasmineMatchersObject) => {
    const jestMatchersObject = Object.create(null);
    Object.keys(jasmineMatchersObject).forEach(name => {
      jestMatchersObject[name] = function() {
        const result = jasmineMatchersObject[name](jasmine.matchersUtil, null);
        // if there is no 'negativeCompare', both should be handled by `compare`
        const negativeCompare = result.negativeCompare || result.compare;

        return this.isNot
          ? negativeCompare.apply(null, arguments)
          : result.compare.apply(null, arguments);
      };
    });

    const expect = global.expect;
    expect.extend(jestMatchersObject);
github bruderstein / unexpected-preact / src / helpers / snapshots.js View on Github external
function compareSnapshot(expect, flags, subjectAdapter, subjectRenderer, subjectOutput) {

  const state = matchers.getState();

  if (!state.unexpectedSnapshot) {
    state.unexpectedSnapshot = new UnexpectedSnapshotState(state.snapshotState);
  }

  const snapshot = state.unexpectedSnapshot.getSnapshot(state.testPath, state.currentTestName, expect);
  if (snapshot === null) {
    // Write and save
    state.unexpectedSnapshot.saveSnapshot(state.testPath, state.currentTestName, rawAdapter.serialize(subjectAdapter, subjectOutput), expect);
    state.snapshotState.added++;
  } else {
    expect.withError(() => {
      if (flags.satisfy) {
        expect(subjectRenderer, 'to have rendered', rawAdapter.deserialize(snapshot));
      } else {
        expect(subjectRenderer, 'to have rendered with all children with all wrappers with all classes with all attributes', rawAdapter.deserialize(snapshot));
github jiayihu / rx-polling / test / index.spec.ts View on Github external
function assertDeepEqual(actual, expected) {
  const result = matchers.toEqual(actual, expected);

  if (!result.pass) {
    const diff = diffTestMessages(result.actual, result.expected);
    throw diff + '\n' + result.message();
  }
}
github bruderstein / unexpected-preact / src / helpers / snapshots.js View on Github external
snapshotState.removeUncheckedKeys = function () {
      const state = matchers.getState();
      let isDirty = false;
      const snapshot = state.unexpectedSnapshot && state.unexpectedSnapshot._files[state.testPath];
      if (snapshot && snapshot.uncheckedKeys.size) {
        snapshot.uncheckedKeys.forEach(key => {
          const testName = /(.*)\s[0-9]+$/.exec(key)[1];

          if (!snapshot.failedTests.has(testName)) {
            isDirty = true;
            delete snapshot.allTests[key]
          }
        });
      }

      if (!snapshot || Object.keys(snapshot.allTests).length === 0) {
        const snapshotPath = getSnapshotPath(state.testPath);
        try {
github bruderstein / unexpected-preact / src / helpers / snapshots.js View on Github external
function injectStateHooks() {
  const state = matchers.getState();
  const snapshotState = state && state.snapshotState;
  if (snapshotState) {
    const originalRemoveUncheckedKeys = snapshotState.removeUncheckedKeys;

    snapshotState.removeUncheckedKeys = function () {
      const state = matchers.getState();
      let isDirty = false;
      const snapshot = state.unexpectedSnapshot && state.unexpectedSnapshot._files[state.testPath];
      if (snapshot && snapshot.uncheckedKeys.size) {
        snapshot.uncheckedKeys.forEach(key => {
          const testName = /(.*)\s[0-9]+$/.exec(key)[1];

          if (!snapshot.failedTests.has(testName)) {
            isDirty = true;
            delete snapshot.allTests[key]
          }
github kwonoj / rx-sandbox / src / assert / marbleAssert.ts View on Github external
import { toEqual } from 'jest-matchers/build/matchers';
import { SubscriptionLog } from 'rxjs/internal/testing/SubscriptionLog';
import { TestMessage } from '../message/TestMessage';
import { constructObservableMarble } from './constructObservableMarble';
import { constructSubscriptionMarble } from './constructSubscriptionMarble';
const { matcherHint, printExpected, printReceived } = require('jest-matcher-utils'); //tslint:disable-line:no-require-imports no-var-requires

const toEqualAssert = toEqual.bind({ expand: false });

const subscriptionMarbleAssert = (source: Array) => (expected: Array) => {
  const asserted = toEqualAssert(source, expected);

  if (!asserted.pass) {
    const length = source.length > expected.length ? source.length : expected.length;
    let description = `
${matcherHint(' to equal ')}
    `;

    for (let idx = 0; idx < length; idx++) {
      const sourceMarble = !!source[idx]
        ? constructSubscriptionMarble(source[idx])
        : { marbleString: '', frameString: '' };
      const expectedMarble = !!expected[idx]
        ? constructSubscriptionMarble(expected[idx])