aboutsummaryrefslogtreecommitdiff
path: root/node_modules/jest-snapshot/build/index.js
blob: 866f8b69864cdc416ca577534e3f577d7e047b11 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 * 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.
 *
 * 
 */
'use strict';




const diff = require('jest-diff');
const fileExists = require('jest-file-exists');
const fs = require('fs');
const path = require('path');
const SnapshotState = require('./State');var _require =
require('./plugins');const addSerializer = _require.addSerializer,getSerializers = _require.getSerializers;var _require2 =






require('jest-matcher-utils');const EXPECTED_COLOR = _require2.EXPECTED_COLOR,ensureNoExpected = _require2.ensureNoExpected,matcherHint = _require2.matcherHint,RECEIVED_COLOR = _require2.RECEIVED_COLOR;var _require3 =
require('./utils');const SNAPSHOT_EXTENSION = _require3.SNAPSHOT_EXTENSION;

const cleanup = (hasteFS, update) => {
  const pattern = '\\.' + SNAPSHOT_EXTENSION + '$';
  const files = hasteFS.matchFiles(pattern);
  const filesRemoved = files.
  filter(snapshotFile => !fileExists(
  path.resolve(
  path.dirname(snapshotFile),
  '..',
  path.basename(snapshotFile, '.' + SNAPSHOT_EXTENSION)),

  hasteFS)).

  map(snapshotFile => {
    if (update) {
      fs.unlinkSync(snapshotFile);
    }
  }).
  length;

  return {
    filesRemoved };

};

const initializeSnapshotState = (
testFile,
update,
testPath,
expand) =>
new SnapshotState(testFile, update, testPath, expand);

const toMatchSnapshot = function (received, testName) {
  this.dontThrow && this.dontThrow();const

  currentTestName = this.currentTestName,isNot = this.isNot,snapshotState = this.snapshotState;

  if (isNot) {
    throw new Error(
    'Jest: `.not` cannot be used with `.toMatchSnapshot()`.');

  }

  if (!snapshotState) {
    throw new Error('Jest: snapshot state must be initialized.');
  }

  const result = snapshotState.match(testName || currentTestName, received);const
  pass = result.pass;

  if (pass) {
    return { message: '', pass: true };
  } else {const
    count = result.count,expected = result.expected,actual = result.actual;

    const expectedString = expected.trim();
    const actualString = actual.trim();
    const diffMessage = diff(
    expectedString,
    actualString,
    {
      aAnnotation: 'Snapshot',
      bAnnotation: 'Received',
      expand: snapshotState.expand });



    const report =
    () => `${RECEIVED_COLOR('Received value')} does not match ` +
    `${EXPECTED_COLOR('stored snapshot ' + count)}.\n\n` + (
    diffMessage ||
    RECEIVED_COLOR('- ' + expectedString) + '\n' +
    EXPECTED_COLOR('+ ' + actualString));


    const message =
    () => matcherHint('.toMatchSnapshot', 'value', '') + '\n\n' +
    report();

    // Passing the the actual and expected objects so that a custom reporter
    // could access them, for example in order to display a custom visual diff,
    // or create a different error message
    return {
      actual: actualString,
      expected: expectedString,
      message,
      name: 'toMatchSnapshot',
      pass: false,
      report };

  }
};

const toThrowErrorMatchingSnapshot = function (received, expected) {
  this.dontThrow && this.dontThrow();const
  isNot = this.isNot;

  if (isNot) {
    throw new Error(
    'Jest: `.not` cannot be used with `.toThrowErrorMatchingSnapshot()`.');

  }

  ensureNoExpected(expected, '.toThrowErrorMatchingSnapshot');

  let error;

  try {
    received();
  } catch (e) {
    error = e;
  }

  if (error === undefined) {
    throw new Error(
    matcherHint('.toThrowErrorMatchingSnapshot', '() => {}', '') + '\n\n' +
    `Expected the function to throw an error.\n` +
    `But it didn't throw anything.`);

  }

  return toMatchSnapshot.call(this, error.message);
};

module.exports = {
  EXTENSION: SNAPSHOT_EXTENSION,
  SnapshotState,
  addSerializer,
  cleanup,
  getSerializers,
  initializeSnapshotState,
  toMatchSnapshot,
  toThrowErrorMatchingSnapshot };