Ejemplo n.º 1
0
  toMatchSnapshot: (util, customEquality) => {
    return {
      negativeCompare() {
        throw new Error(
          'Jest: `.not` can not be used with `.toMatchSnapshot()`.'
        );
      },
      compare(actual, expected) {
        if (expected !== undefined) {
          throw new Error(
            'Jest: toMatchSnapshot() does not accept parameters.'
          );
        }

        const currentSpecName = snapshotState.currentSpecName;
        const snapshot = snapshotState.snapshot;

        const callCount = snapshotState.getCounter();
        snapshotState.incrementCounter();

        let pass = false;
        let message;
        const key = currentSpecName + ' ' + callCount;
        if (
          !snapshot.fileExists() ||
          (snapshot.has(key) && options.updateSnapshot) ||
          !snapshot.has(key)
        ) {
          if (options.updateSnapshot && snapshot.has(key)) {
            snapshotState.removed++;
          }
          snapshot.add(key, actual);
          snapshotState.added++;
          pass = true;
        } else {
          actual = snapshot.serialize(actual);
          const matches = snapshot.matches(key, actual);
          pass = matches.pass;
          if (!pass) {
            const matcherName = 'toMatchSnapshot';
            const formatter = new JasmineFormatter(jasmine, {global: {}}, {});
            formatter.addDiffableMatcher(matcherName);
            message = formatter
              .formatMatchFailure(Object.assign({matcherName}, matches))
              .replace(
                'toMatchSnapshot:',
                'toMatchSnapshot #' + (callCount + 1) + ':'
              );
          } else {
            snapshotState.matched++;
          }
        }

        return {
          pass,
          message,
        };
      },
    };
  },
Ejemplo n.º 2
0
 specResult.failedExpectations.forEach(failed => {
   let message;
   if (!failed.matcherName && failed.stack) {
     message = failed.stack;
   } else {
     message = this._formatter.formatMatchFailure(failed);
   }
   results.failureMessages.push(message);
 });
Ejemplo n.º 3
0
 spec.results().getItems().forEach(result => {
   switch (result.type) {
     case 'expect':
       if (result.passed()) {
         results.numPassingAsserts++;
       } else if (!result.matcherName && result.trace.stack) {
         results.failureMessages.push(result.trace.stack);
       } else {
         results.failureMessages.push(
           this._formatter.formatMatchFailure(result),
         );
       }
       break;
     default:
       throw new Error('Unexpected jasmine spec result type: ', result.type);
   }
 });
Ejemplo n.º 4
0
/**
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @emails oncall+jsinfra
 */
'use strict';

jest.disableAutomock();

const JasmineFormatter = require('jest-util').JasmineFormatter;

const formatter = new JasmineFormatter(jasmine, {global: {}});
const CALLED_AT_LEAST_ONCE = 'Expected to be called at least once';
const SHOULD_NOT_BE_CALLED = 'Expected not to be called';
const NOT_EXPECTED_VALUES =
  'Was not called with the expected values.\n' +
  `Expected call:\n${formatter.prettyPrint([1, {}, 'Error'])}\n` +
  `Actual call:\n${formatter.prettyPrint([1, {}, ''])}`;

const NOT_EXPECTED_VALUES_EXACTLY_FOUR =
  'Was not called with the expected values.\n' +
  `Expected call:\n${formatter.prettyPrint([5])}\n` +
  `Actual calls:\n${formatter.prettyPrint([4])},` +
  `\n${formatter.prettyPrint([3])},` +
  `\n${formatter.prettyPrint([2])}` +
  `\nand 1 other call.`;
Ejemplo n.º 5
0
  toMatchSnapshot: (util, customEquality) => {
    return {
      negativeCompare() {
        throw new Error(
          'Jest: `.not` can not be used with `.toMatchSnapshot()`.'
        );
      },
      compare(rendered, expected) {

        const currentSpecName = snapshotState.currentSpecName;

        if (expected !== undefined) {
          throw new Error('toMatchSnapshot() does not accepts parameters.');
        }
        if (typeof rendered !== 'string') {
          throw new Error('toMatchSnapshot() only works with Strings.');
        }

        const snapshot = snapshotState.snapshot;

        const callCount = snapshotState.getCounter();
        snapshotState.incrementCounter();

        let pass = false;
        let message;
        const key = currentSpecName + ' ' + callCount;
        if (
          !snapshot.fileExists() ||
          (snapshot.has(key) && options.updateSnapshot) ||
          !snapshot.has(key)
        ) {
          snapshot.add(key, rendered);
          pass = true;
        } else {
          pass = snapshot.matches(key, rendered);
          if (!pass) {
            const matcherName = 'toMatchSnapshot';
            const res = {
              matcherName,
              expected: snapshot.get(key),
              actual: rendered,
            };

            const JasmineFormatter = require('jest-util').JasmineFormatter;

            const formatter = new JasmineFormatter(jasmine, {global: {}}, {});
            formatter.addDiffableMatcher('toMatchSnapshot');
            message = formatter.formatMatchFailure(res).replace(
              'toMatchSnapshot:',
              'toMatchSnapshot #' + (callCount + 1) + ':'
            );
          }
        }

        return {
          pass,
          message,
        };
      },
    };
  },