Add node modules and compiled JavaScript from main (#57)
Co-authored-by: Oliver King <oking3@uncc.edu>
This commit is contained in:
parent
d893f27da9
commit
7f7e5ba5ea
6750 changed files with 1745644 additions and 10860 deletions
61
node_modules/jest-snapshot/build/State.d.ts
generated
vendored
Normal file
61
node_modules/jest-snapshot/build/State.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,61 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import type { Config } from '@jest/types';
|
||||
export declare type SnapshotStateOptions = {
|
||||
updateSnapshot: Config.SnapshotUpdateState;
|
||||
getPrettier: () => null | typeof import('prettier');
|
||||
getBabelTraverse: () => Function;
|
||||
expand?: boolean;
|
||||
};
|
||||
export declare type SnapshotMatchOptions = {
|
||||
testName: string;
|
||||
received: unknown;
|
||||
key?: string;
|
||||
inlineSnapshot?: string;
|
||||
isInline: boolean;
|
||||
error?: Error;
|
||||
};
|
||||
declare type SnapshotReturnOptions = {
|
||||
actual: string;
|
||||
count: number;
|
||||
expected?: string;
|
||||
key: string;
|
||||
pass: boolean;
|
||||
};
|
||||
declare type SaveStatus = {
|
||||
deleted: boolean;
|
||||
saved: boolean;
|
||||
};
|
||||
export default class SnapshotState {
|
||||
private _counters;
|
||||
private _dirty;
|
||||
private _index;
|
||||
private _updateSnapshot;
|
||||
private _snapshotData;
|
||||
private _initialData;
|
||||
private _snapshotPath;
|
||||
private _inlineSnapshots;
|
||||
private _uncheckedKeys;
|
||||
private _getBabelTraverse;
|
||||
private _getPrettier;
|
||||
added: number;
|
||||
expand: boolean;
|
||||
matched: number;
|
||||
unmatched: number;
|
||||
updated: number;
|
||||
constructor(snapshotPath: Config.Path, options: SnapshotStateOptions);
|
||||
markSnapshotsAsCheckedForTest(testName: string): void;
|
||||
private _addSnapshot;
|
||||
clear(): void;
|
||||
save(): SaveStatus;
|
||||
getUncheckedCount(): number;
|
||||
getUncheckedKeys(): Array<string>;
|
||||
removeUncheckedKeys(): void;
|
||||
match({ testName, received, key, inlineSnapshot, isInline, error, }: SnapshotMatchOptions): SnapshotReturnOptions;
|
||||
fail(testName: string, _received: unknown, key?: string): string;
|
||||
}
|
||||
export {};
|
351
node_modules/jest-snapshot/build/State.js
generated
vendored
Normal file
351
node_modules/jest-snapshot/build/State.js
generated
vendored
Normal file
|
@ -0,0 +1,351 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var fs = _interopRequireWildcard(require('graceful-fs'));
|
||||
|
||||
var _jestMessageUtil = require('jest-message-util');
|
||||
|
||||
var _utils = require('./utils');
|
||||
|
||||
var _inline_snapshots = require('./inline_snapshots');
|
||||
|
||||
function _getRequireWildcardCache() {
|
||||
if (typeof WeakMap !== 'function') return null;
|
||||
var cache = new WeakMap();
|
||||
_getRequireWildcardCache = function () {
|
||||
return cache;
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
function _interopRequireWildcard(obj) {
|
||||
if (obj && obj.__esModule) {
|
||||
return obj;
|
||||
}
|
||||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
||||
return {default: obj};
|
||||
}
|
||||
var cache = _getRequireWildcardCache();
|
||||
if (cache && cache.has(obj)) {
|
||||
return cache.get(obj);
|
||||
}
|
||||
var newObj = {};
|
||||
var hasPropertyDescriptor =
|
||||
Object.defineProperty && Object.getOwnPropertyDescriptor;
|
||||
for (var key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
var desc = hasPropertyDescriptor
|
||||
? Object.getOwnPropertyDescriptor(obj, key)
|
||||
: null;
|
||||
if (desc && (desc.get || desc.set)) {
|
||||
Object.defineProperty(newObj, key, desc);
|
||||
} else {
|
||||
newObj[key] = obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
newObj.default = obj;
|
||||
if (cache) {
|
||||
cache.set(obj, newObj);
|
||||
}
|
||||
return newObj;
|
||||
}
|
||||
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var jestExistsFile =
|
||||
global[Symbol.for('jest-native-exists-file')] || fs.existsSync;
|
||||
|
||||
function _defineProperty(obj, key, value) {
|
||||
if (key in obj) {
|
||||
Object.defineProperty(obj, key, {
|
||||
value: value,
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true
|
||||
});
|
||||
} else {
|
||||
obj[key] = value;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
class SnapshotState {
|
||||
// @ts-expect-error
|
||||
constructor(snapshotPath, options) {
|
||||
_defineProperty(this, '_counters', void 0);
|
||||
|
||||
_defineProperty(this, '_dirty', void 0);
|
||||
|
||||
_defineProperty(this, '_index', void 0);
|
||||
|
||||
_defineProperty(this, '_updateSnapshot', void 0);
|
||||
|
||||
_defineProperty(this, '_snapshotData', void 0);
|
||||
|
||||
_defineProperty(this, '_initialData', void 0);
|
||||
|
||||
_defineProperty(this, '_snapshotPath', void 0);
|
||||
|
||||
_defineProperty(this, '_inlineSnapshots', void 0);
|
||||
|
||||
_defineProperty(this, '_uncheckedKeys', void 0);
|
||||
|
||||
_defineProperty(this, '_getBabelTraverse', void 0);
|
||||
|
||||
_defineProperty(this, '_getPrettier', void 0);
|
||||
|
||||
_defineProperty(this, 'added', void 0);
|
||||
|
||||
_defineProperty(this, 'expand', void 0);
|
||||
|
||||
_defineProperty(this, 'matched', void 0);
|
||||
|
||||
_defineProperty(this, 'unmatched', void 0);
|
||||
|
||||
_defineProperty(this, 'updated', void 0);
|
||||
|
||||
this._snapshotPath = snapshotPath;
|
||||
const {data, dirty} = (0, _utils.getSnapshotData)(
|
||||
this._snapshotPath,
|
||||
options.updateSnapshot
|
||||
);
|
||||
this._initialData = data;
|
||||
this._snapshotData = data;
|
||||
this._dirty = dirty;
|
||||
this._getBabelTraverse = options.getBabelTraverse;
|
||||
this._getPrettier = options.getPrettier;
|
||||
this._inlineSnapshots = [];
|
||||
this._uncheckedKeys = new Set(Object.keys(this._snapshotData));
|
||||
this._counters = new Map();
|
||||
this._index = 0;
|
||||
this.expand = options.expand || false;
|
||||
this.added = 0;
|
||||
this.matched = 0;
|
||||
this.unmatched = 0;
|
||||
this._updateSnapshot = options.updateSnapshot;
|
||||
this.updated = 0;
|
||||
}
|
||||
|
||||
markSnapshotsAsCheckedForTest(testName) {
|
||||
this._uncheckedKeys.forEach(uncheckedKey => {
|
||||
if ((0, _utils.keyToTestName)(uncheckedKey) === testName) {
|
||||
this._uncheckedKeys.delete(uncheckedKey);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_addSnapshot(key, receivedSerialized, options) {
|
||||
this._dirty = true;
|
||||
|
||||
if (options.isInline) {
|
||||
const error = options.error || new Error();
|
||||
const lines = (0, _jestMessageUtil.getStackTraceLines)(
|
||||
(0, _utils.removeLinesBeforeExternalMatcherTrap)(error.stack || '')
|
||||
);
|
||||
const frame = (0, _jestMessageUtil.getTopFrame)(lines);
|
||||
|
||||
if (!frame) {
|
||||
throw new Error(
|
||||
"Jest: Couldn't infer stack frame for inline snapshot."
|
||||
);
|
||||
}
|
||||
|
||||
this._inlineSnapshots.push({
|
||||
frame,
|
||||
snapshot: receivedSerialized
|
||||
});
|
||||
} else {
|
||||
this._snapshotData[key] = receivedSerialized;
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
this._snapshotData = this._initialData;
|
||||
this._inlineSnapshots = [];
|
||||
this._counters = new Map();
|
||||
this._index = 0;
|
||||
this.added = 0;
|
||||
this.matched = 0;
|
||||
this.unmatched = 0;
|
||||
this.updated = 0;
|
||||
}
|
||||
|
||||
save() {
|
||||
const hasExternalSnapshots = Object.keys(this._snapshotData).length;
|
||||
const hasInlineSnapshots = this._inlineSnapshots.length;
|
||||
const isEmpty = !hasExternalSnapshots && !hasInlineSnapshots;
|
||||
const status = {
|
||||
deleted: false,
|
||||
saved: false
|
||||
};
|
||||
|
||||
if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) {
|
||||
if (hasExternalSnapshots) {
|
||||
(0, _utils.saveSnapshotFile)(this._snapshotData, this._snapshotPath);
|
||||
}
|
||||
|
||||
if (hasInlineSnapshots) {
|
||||
const prettier = this._getPrettier(); // Load lazily
|
||||
|
||||
const babelTraverse = this._getBabelTraverse(); // Load lazily
|
||||
|
||||
(0, _inline_snapshots.saveInlineSnapshots)(
|
||||
this._inlineSnapshots,
|
||||
prettier,
|
||||
babelTraverse
|
||||
);
|
||||
}
|
||||
|
||||
status.saved = true;
|
||||
} else if (!hasExternalSnapshots && jestExistsFile(this._snapshotPath)) {
|
||||
if (this._updateSnapshot === 'all') {
|
||||
fs.unlinkSync(this._snapshotPath);
|
||||
}
|
||||
|
||||
status.deleted = true;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
getUncheckedCount() {
|
||||
return this._uncheckedKeys.size || 0;
|
||||
}
|
||||
|
||||
getUncheckedKeys() {
|
||||
return Array.from(this._uncheckedKeys);
|
||||
}
|
||||
|
||||
removeUncheckedKeys() {
|
||||
if (this._updateSnapshot === 'all' && this._uncheckedKeys.size) {
|
||||
this._dirty = true;
|
||||
|
||||
this._uncheckedKeys.forEach(key => delete this._snapshotData[key]);
|
||||
|
||||
this._uncheckedKeys.clear();
|
||||
}
|
||||
}
|
||||
|
||||
match({testName, received, key, inlineSnapshot, isInline, error}) {
|
||||
this._counters.set(testName, (this._counters.get(testName) || 0) + 1);
|
||||
|
||||
const count = Number(this._counters.get(testName));
|
||||
|
||||
if (!key) {
|
||||
key = (0, _utils.testNameToKey)(testName, count);
|
||||
} // Do not mark the snapshot as "checked" if the snapshot is inline and
|
||||
// there's an external snapshot. This way the external snapshot can be
|
||||
// removed with `--updateSnapshot`.
|
||||
|
||||
if (!(isInline && this._snapshotData[key] !== undefined)) {
|
||||
this._uncheckedKeys.delete(key);
|
||||
}
|
||||
|
||||
const receivedSerialized = (0, _utils.addExtraLineBreaks)(
|
||||
(0, _utils.serialize)(received)
|
||||
);
|
||||
const expected = isInline ? inlineSnapshot : this._snapshotData[key];
|
||||
const pass = expected === receivedSerialized;
|
||||
const hasSnapshot = expected !== undefined;
|
||||
const snapshotIsPersisted = isInline || fs.existsSync(this._snapshotPath);
|
||||
|
||||
if (pass && !isInline) {
|
||||
// Executing a snapshot file as JavaScript and writing the strings back
|
||||
// when other snapshots have changed loses the proper escaping for some
|
||||
// characters. Since we check every snapshot in every test, use the newly
|
||||
// generated formatted string.
|
||||
// Note that this is only relevant when a snapshot is added and the dirty
|
||||
// flag is set.
|
||||
this._snapshotData[key] = receivedSerialized;
|
||||
} // These are the conditions on when to write snapshots:
|
||||
// * There's no snapshot file in a non-CI environment.
|
||||
// * There is a snapshot file and we decided to update the snapshot.
|
||||
// * There is a snapshot file, but it doesn't have this snaphsot.
|
||||
// These are the conditions on when not to write snapshots:
|
||||
// * The update flag is set to 'none'.
|
||||
// * There's no snapshot file or a file without this snapshot on a CI environment.
|
||||
|
||||
if (
|
||||
(hasSnapshot && this._updateSnapshot === 'all') ||
|
||||
((!hasSnapshot || !snapshotIsPersisted) &&
|
||||
(this._updateSnapshot === 'new' || this._updateSnapshot === 'all'))
|
||||
) {
|
||||
if (this._updateSnapshot === 'all') {
|
||||
if (!pass) {
|
||||
if (hasSnapshot) {
|
||||
this.updated++;
|
||||
} else {
|
||||
this.added++;
|
||||
}
|
||||
|
||||
this._addSnapshot(key, receivedSerialized, {
|
||||
error,
|
||||
isInline
|
||||
});
|
||||
} else {
|
||||
this.matched++;
|
||||
}
|
||||
} else {
|
||||
this._addSnapshot(key, receivedSerialized, {
|
||||
error,
|
||||
isInline
|
||||
});
|
||||
|
||||
this.added++;
|
||||
}
|
||||
|
||||
return {
|
||||
actual: '',
|
||||
count,
|
||||
expected: '',
|
||||
key,
|
||||
pass: true
|
||||
};
|
||||
} else {
|
||||
if (!pass) {
|
||||
this.unmatched++;
|
||||
return {
|
||||
actual: (0, _utils.removeExtraLineBreaks)(receivedSerialized),
|
||||
count,
|
||||
expected:
|
||||
expected !== undefined
|
||||
? (0, _utils.removeExtraLineBreaks)(expected)
|
||||
: undefined,
|
||||
key,
|
||||
pass: false
|
||||
};
|
||||
} else {
|
||||
this.matched++;
|
||||
return {
|
||||
actual: '',
|
||||
count,
|
||||
expected: '',
|
||||
key,
|
||||
pass: true
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fail(testName, _received, key) {
|
||||
this._counters.set(testName, (this._counters.get(testName) || 0) + 1);
|
||||
|
||||
const count = Number(this._counters.get(testName));
|
||||
|
||||
if (!key) {
|
||||
key = (0, _utils.testNameToKey)(testName, count);
|
||||
}
|
||||
|
||||
this._uncheckedKeys.delete(key);
|
||||
|
||||
this.unmatched++;
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
exports.default = SnapshotState;
|
15
node_modules/jest-snapshot/build/colors.d.ts
generated
vendored
Normal file
15
node_modules/jest-snapshot/build/colors.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
export declare const aForeground2 = 90;
|
||||
export declare const aBackground2 = 225;
|
||||
export declare const bForeground2 = 23;
|
||||
export declare const bBackground2 = 195;
|
||||
export declare type RGB = [number, number, number];
|
||||
export declare const aForeground3: RGB;
|
||||
export declare const aBackground3: RGB;
|
||||
export declare const bForeground3: RGB;
|
||||
export declare const bBackground3: RGB;
|
30
node_modules/jest-snapshot/build/colors.js
generated
vendored
Normal file
30
node_modules/jest-snapshot/build/colors.js
generated
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.bBackground3 = exports.bForeground3 = exports.aBackground3 = exports.aForeground3 = exports.bBackground2 = exports.bForeground2 = exports.aBackground2 = exports.aForeground2 = void 0;
|
||||
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
// https://jonasjacek.github.io/colors/
|
||||
const aForeground2 = 90;
|
||||
exports.aForeground2 = aForeground2;
|
||||
const aBackground2 = 225;
|
||||
exports.aBackground2 = aBackground2;
|
||||
const bForeground2 = 23;
|
||||
exports.bForeground2 = bForeground2;
|
||||
const bBackground2 = 195;
|
||||
exports.bBackground2 = bBackground2;
|
||||
const aForeground3 = [0x80, 0, 0x80];
|
||||
exports.aForeground3 = aForeground3;
|
||||
const aBackground3 = [0xff, 0xd7, 0xff];
|
||||
exports.aBackground3 = aBackground3;
|
||||
const bForeground3 = [0, 0x5f, 0x5f];
|
||||
exports.bForeground3 = bForeground3;
|
||||
const bBackground3 = [0xd7, 0xff, 0xff];
|
||||
exports.bBackground3 = bBackground3;
|
7
node_modules/jest-snapshot/build/dedentLines.d.ts
generated
vendored
Normal file
7
node_modules/jest-snapshot/build/dedentLines.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
export declare const dedentLines: (input: Array<string>) => Array<string> | null;
|
149
node_modules/jest-snapshot/build/dedentLines.js
generated
vendored
Normal file
149
node_modules/jest-snapshot/build/dedentLines.js
generated
vendored
Normal file
|
@ -0,0 +1,149 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.dedentLines = void 0;
|
||||
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
const getIndentationLength = line => {
|
||||
const result = /^( {2})+/.exec(line);
|
||||
return result === null ? 0 : result[0].length;
|
||||
};
|
||||
|
||||
const dedentLine = line => line.slice(getIndentationLength(line)); // Return true if:
|
||||
// "key": "value has multiple lines\n…
|
||||
// "key has multiple lines\n…
|
||||
|
||||
const hasUnmatchedDoubleQuoteMarks = string => {
|
||||
let n = 0;
|
||||
let i = string.indexOf('"', 0);
|
||||
|
||||
while (i !== -1) {
|
||||
if (i === 0 || string[i - 1] !== '\\') {
|
||||
n += 1;
|
||||
}
|
||||
|
||||
i = string.indexOf('"', i + 1);
|
||||
}
|
||||
|
||||
return n % 2 !== 0;
|
||||
};
|
||||
|
||||
const isFirstLineOfTag = line => /^( {2})*\</.test(line); // The length of the output array is the index of the next input line.
|
||||
// Push dedented lines of start tag onto output and return true;
|
||||
// otherwise return false because:
|
||||
// * props include a multiline string (or text node, if props have markup)
|
||||
// * start tag does not close
|
||||
|
||||
const dedentStartTag = (input, output) => {
|
||||
let line = input[output.length];
|
||||
output.push(dedentLine(line));
|
||||
|
||||
if (line.includes('>')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
while (output.length < input.length) {
|
||||
line = input[output.length];
|
||||
|
||||
if (hasUnmatchedDoubleQuoteMarks(line)) {
|
||||
return false; // because props include a multiline string
|
||||
} else if (isFirstLineOfTag(line)) {
|
||||
// Recursion only if props have markup.
|
||||
if (!dedentMarkup(input, output)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
output.push(dedentLine(line));
|
||||
|
||||
if (line.includes('>')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}; // Push dedented lines of markup onto output and return true;
|
||||
// otherwise return false because:
|
||||
// * props include a multiline string
|
||||
// * text has more than one adjacent line
|
||||
// * markup does not close
|
||||
|
||||
const dedentMarkup = (input, output) => {
|
||||
let line = input[output.length];
|
||||
|
||||
if (!dedentStartTag(input, output)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (input[output.length - 1].includes('/>')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
let isText = false;
|
||||
const stack = [];
|
||||
stack.push(getIndentationLength(line));
|
||||
|
||||
while (stack.length > 0 && output.length < input.length) {
|
||||
line = input[output.length];
|
||||
|
||||
if (isFirstLineOfTag(line)) {
|
||||
if (line.includes('</')) {
|
||||
output.push(dedentLine(line));
|
||||
stack.pop();
|
||||
} else {
|
||||
if (!dedentStartTag(input, output)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!input[output.length - 1].includes('/>')) {
|
||||
stack.push(getIndentationLength(line));
|
||||
}
|
||||
}
|
||||
|
||||
isText = false;
|
||||
} else {
|
||||
if (isText) {
|
||||
return false; // because text has more than one adjacent line
|
||||
}
|
||||
|
||||
const indentationLengthOfTag = stack[stack.length - 1];
|
||||
output.push(line.slice(indentationLengthOfTag + 2));
|
||||
isText = true;
|
||||
}
|
||||
}
|
||||
|
||||
return stack.length === 0;
|
||||
}; // Return lines unindented by heuristic;
|
||||
// otherwise return null because:
|
||||
// * props include a multiline string
|
||||
// * text has more than one adjacent line
|
||||
// * markup does not close
|
||||
|
||||
const dedentLines = input => {
|
||||
const output = [];
|
||||
|
||||
while (output.length < input.length) {
|
||||
const line = input[output.length];
|
||||
|
||||
if (hasUnmatchedDoubleQuoteMarks(line)) {
|
||||
return null;
|
||||
} else if (isFirstLineOfTag(line)) {
|
||||
if (!dedentMarkup(input, output)) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
output.push(dedentLine(line));
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
exports.dedentLines = dedentLines;
|
106
node_modules/jest-snapshot/build/index.d.ts
generated
vendored
Normal file
106
node_modules/jest-snapshot/build/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,106 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import type { Config } from '@jest/types';
|
||||
import type { FS as HasteFS } from 'jest-haste-map';
|
||||
import { SnapshotResolver as JestSnapshotResolver } from './snapshot_resolver';
|
||||
import SnapshotState from './State';
|
||||
import type { Context } from './types';
|
||||
import * as utils from './utils';
|
||||
declare const JestSnapshot: {
|
||||
EXTENSION: string;
|
||||
SnapshotState: typeof SnapshotState;
|
||||
addSerializer: (plugin: import("pretty-format/build/types").Plugin) => void;
|
||||
buildSnapshotResolver: (config: Config.ProjectConfig) => JestSnapshotResolver;
|
||||
cleanup: (hasteFS: HasteFS, update: Config.SnapshotUpdateState, snapshotResolver: JestSnapshotResolver, testPathIgnorePatterns?: string[] | undefined) => {
|
||||
filesRemoved: number;
|
||||
filesRemovedList: Array<string>;
|
||||
};
|
||||
getSerializers: () => import("pretty-format/build/types").Plugins;
|
||||
isSnapshotPath: (path: string) => boolean;
|
||||
toMatchInlineSnapshot: (this: Context, received: any, propertiesOrSnapshot?: string | object | undefined, inlineSnapshot?: string | undefined) => {
|
||||
message: () => string;
|
||||
name: string;
|
||||
pass: boolean;
|
||||
actual?: undefined;
|
||||
expected?: undefined;
|
||||
} | {
|
||||
message: () => string;
|
||||
pass: boolean;
|
||||
name?: undefined;
|
||||
actual?: undefined;
|
||||
expected?: undefined;
|
||||
} | {
|
||||
actual: string;
|
||||
expected: string | undefined;
|
||||
message: () => string;
|
||||
name: string;
|
||||
pass: boolean;
|
||||
};
|
||||
toMatchSnapshot: (this: Context, received: any, propertiesOrHint?: string | object | undefined, hint?: string | undefined) => {
|
||||
message: () => string;
|
||||
name: string;
|
||||
pass: boolean;
|
||||
actual?: undefined;
|
||||
expected?: undefined;
|
||||
} | {
|
||||
message: () => string;
|
||||
pass: boolean;
|
||||
name?: undefined;
|
||||
actual?: undefined;
|
||||
expected?: undefined;
|
||||
} | {
|
||||
actual: string;
|
||||
expected: string | undefined;
|
||||
message: () => string;
|
||||
name: string;
|
||||
pass: boolean;
|
||||
};
|
||||
toThrowErrorMatchingInlineSnapshot: (this: Context, received: any, inlineSnapshot?: string | undefined, fromPromise?: boolean | undefined) => {
|
||||
message: () => string;
|
||||
name: string;
|
||||
pass: boolean;
|
||||
actual?: undefined;
|
||||
expected?: undefined;
|
||||
} | {
|
||||
message: () => string;
|
||||
pass: boolean;
|
||||
name?: undefined;
|
||||
actual?: undefined;
|
||||
expected?: undefined;
|
||||
} | {
|
||||
actual: string;
|
||||
expected: string | undefined;
|
||||
message: () => string;
|
||||
name: string;
|
||||
pass: boolean;
|
||||
};
|
||||
toThrowErrorMatchingSnapshot: (this: Context, received: any, hint: string | undefined, fromPromise: boolean) => {
|
||||
message: () => string;
|
||||
name: string;
|
||||
pass: boolean;
|
||||
actual?: undefined;
|
||||
expected?: undefined;
|
||||
} | {
|
||||
message: () => string;
|
||||
pass: boolean;
|
||||
name?: undefined;
|
||||
actual?: undefined;
|
||||
expected?: undefined;
|
||||
} | {
|
||||
actual: string;
|
||||
expected: string | undefined;
|
||||
message: () => string;
|
||||
name: string;
|
||||
pass: boolean;
|
||||
};
|
||||
utils: typeof utils;
|
||||
};
|
||||
declare namespace JestSnapshot {
|
||||
type SnapshotResolver = JestSnapshotResolver;
|
||||
type SnapshotStateType = SnapshotState;
|
||||
}
|
||||
export = JestSnapshot;
|
614
node_modules/jest-snapshot/build/index.js
generated
vendored
Normal file
614
node_modules/jest-snapshot/build/index.js
generated
vendored
Normal file
|
@ -0,0 +1,614 @@
|
|||
'use strict';
|
||||
|
||||
var fs = _interopRequireWildcard(require('graceful-fs'));
|
||||
|
||||
var _jestMatcherUtils = require('jest-matcher-utils');
|
||||
|
||||
var _snapshot_resolver = require('./snapshot_resolver');
|
||||
|
||||
var _State = _interopRequireDefault(require('./State'));
|
||||
|
||||
var _plugins = require('./plugins');
|
||||
|
||||
var _printSnapshot = require('./printSnapshot');
|
||||
|
||||
var utils = _interopRequireWildcard(require('./utils'));
|
||||
|
||||
function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : {default: obj};
|
||||
}
|
||||
|
||||
function _getRequireWildcardCache() {
|
||||
if (typeof WeakMap !== 'function') return null;
|
||||
var cache = new WeakMap();
|
||||
_getRequireWildcardCache = function () {
|
||||
return cache;
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
function _interopRequireWildcard(obj) {
|
||||
if (obj && obj.__esModule) {
|
||||
return obj;
|
||||
}
|
||||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
||||
return {default: obj};
|
||||
}
|
||||
var cache = _getRequireWildcardCache();
|
||||
if (cache && cache.has(obj)) {
|
||||
return cache.get(obj);
|
||||
}
|
||||
var newObj = {};
|
||||
var hasPropertyDescriptor =
|
||||
Object.defineProperty && Object.getOwnPropertyDescriptor;
|
||||
for (var key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
var desc = hasPropertyDescriptor
|
||||
? Object.getOwnPropertyDescriptor(obj, key)
|
||||
: null;
|
||||
if (desc && (desc.get || desc.set)) {
|
||||
Object.defineProperty(newObj, key, desc);
|
||||
} else {
|
||||
newObj[key] = obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
newObj.default = obj;
|
||||
if (cache) {
|
||||
cache.set(obj, newObj);
|
||||
}
|
||||
return newObj;
|
||||
}
|
||||
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var jestExistsFile =
|
||||
global[Symbol.for('jest-native-exists-file')] || fs.existsSync;
|
||||
const DID_NOT_THROW = 'Received function did not throw'; // same as toThrow
|
||||
|
||||
const NOT_SNAPSHOT_MATCHERS = `Snapshot matchers cannot be used with ${(0,
|
||||
_jestMatcherUtils.BOLD_WEIGHT)('not')}`;
|
||||
const INDENTATION_REGEX = /^([^\S\n]*)\S/m; // Display name in report when matcher fails same as in snapshot file,
|
||||
// but with optional hint argument in bold weight.
|
||||
|
||||
const printSnapshotName = (concatenatedBlockNames = '', hint = '', count) => {
|
||||
const hasNames = concatenatedBlockNames.length !== 0;
|
||||
const hasHint = hint.length !== 0;
|
||||
return (
|
||||
'Snapshot name: `' +
|
||||
(hasNames ? utils.escapeBacktickString(concatenatedBlockNames) : '') +
|
||||
(hasNames && hasHint ? ': ' : '') +
|
||||
(hasHint
|
||||
? (0, _jestMatcherUtils.BOLD_WEIGHT)(utils.escapeBacktickString(hint))
|
||||
: '') +
|
||||
' ' +
|
||||
count +
|
||||
'`'
|
||||
);
|
||||
};
|
||||
|
||||
function stripAddedIndentation(inlineSnapshot) {
|
||||
// Find indentation if exists.
|
||||
const match = inlineSnapshot.match(INDENTATION_REGEX);
|
||||
|
||||
if (!match || !match[1]) {
|
||||
// No indentation.
|
||||
return inlineSnapshot;
|
||||
}
|
||||
|
||||
const indentation = match[1];
|
||||
const lines = inlineSnapshot.split('\n');
|
||||
|
||||
if (lines.length <= 2) {
|
||||
// Must be at least 3 lines.
|
||||
return inlineSnapshot;
|
||||
}
|
||||
|
||||
if (lines[0].trim() !== '' || lines[lines.length - 1].trim() !== '') {
|
||||
// If not blank first and last lines, abort.
|
||||
return inlineSnapshot;
|
||||
}
|
||||
|
||||
for (let i = 1; i < lines.length - 1; i++) {
|
||||
if (lines[i] !== '') {
|
||||
if (lines[i].indexOf(indentation) !== 0) {
|
||||
// All lines except first and last should either be blank or have the same
|
||||
// indent as the first line (or more). If this isn't the case we don't
|
||||
// want to touch the snapshot at all.
|
||||
return inlineSnapshot;
|
||||
}
|
||||
|
||||
lines[i] = lines[i].substr(indentation.length);
|
||||
}
|
||||
} // Last line is a special case because it won't have the same indent as others
|
||||
// but may still have been given some indent to line up.
|
||||
|
||||
lines[lines.length - 1] = ''; // Return inline snapshot, now at indent 0.
|
||||
|
||||
inlineSnapshot = lines.join('\n');
|
||||
return inlineSnapshot;
|
||||
}
|
||||
|
||||
const fileExists = (filePath, hasteFS) =>
|
||||
hasteFS.exists(filePath) || jestExistsFile(filePath);
|
||||
|
||||
const cleanup = (hasteFS, update, snapshotResolver, testPathIgnorePatterns) => {
|
||||
const pattern = '\\.' + _snapshot_resolver.EXTENSION + '$';
|
||||
const files = hasteFS.matchFiles(pattern);
|
||||
let testIgnorePatternsRegex = null;
|
||||
|
||||
if (testPathIgnorePatterns && testPathIgnorePatterns.length > 0) {
|
||||
testIgnorePatternsRegex = new RegExp(testPathIgnorePatterns.join('|'));
|
||||
}
|
||||
|
||||
const list = files.filter(snapshotFile => {
|
||||
const testPath = snapshotResolver.resolveTestPath(snapshotFile); // ignore snapshots of ignored tests
|
||||
|
||||
if (testIgnorePatternsRegex && testIgnorePatternsRegex.test(testPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!fileExists(testPath, hasteFS)) {
|
||||
if (update === 'all') {
|
||||
fs.unlinkSync(snapshotFile);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
return {
|
||||
filesRemoved: list.length,
|
||||
filesRemovedList: list
|
||||
};
|
||||
};
|
||||
|
||||
const toMatchSnapshot = function (received, propertiesOrHint, hint) {
|
||||
const matcherName = 'toMatchSnapshot';
|
||||
let properties;
|
||||
const length = arguments.length;
|
||||
|
||||
if (length === 2 && typeof propertiesOrHint === 'string') {
|
||||
hint = propertiesOrHint;
|
||||
} else if (length >= 2) {
|
||||
if (typeof propertiesOrHint !== 'object' || propertiesOrHint === null) {
|
||||
const options = {
|
||||
isNot: this.isNot,
|
||||
promise: this.promise
|
||||
};
|
||||
let printedWithType = (0, _jestMatcherUtils.printWithType)(
|
||||
'Expected properties',
|
||||
propertiesOrHint,
|
||||
_printSnapshot.printExpected
|
||||
);
|
||||
|
||||
if (length === 3) {
|
||||
options.secondArgument = 'hint';
|
||||
options.secondArgumentColor = _jestMatcherUtils.BOLD_WEIGHT;
|
||||
|
||||
if (propertiesOrHint == null) {
|
||||
printedWithType += `\n\nTo provide a hint without properties: toMatchSnapshot('hint')`;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
(0, _jestMatcherUtils.matcherErrorMessage)(
|
||||
(0, _jestMatcherUtils.matcherHint)(
|
||||
matcherName,
|
||||
undefined,
|
||||
_printSnapshot.PROPERTIES_ARG,
|
||||
options
|
||||
),
|
||||
`Expected ${(0, _jestMatcherUtils.EXPECTED_COLOR)(
|
||||
'properties'
|
||||
)} must be an object`,
|
||||
printedWithType
|
||||
)
|
||||
);
|
||||
} // Future breaking change: Snapshot hint must be a string
|
||||
// if (arguments.length === 3 && typeof hint !== 'string') {}
|
||||
|
||||
properties = propertiesOrHint;
|
||||
}
|
||||
|
||||
return _toMatchSnapshot({
|
||||
context: this,
|
||||
hint,
|
||||
isInline: false,
|
||||
matcherName,
|
||||
properties,
|
||||
received
|
||||
});
|
||||
};
|
||||
|
||||
const toMatchInlineSnapshot = function (
|
||||
received,
|
||||
propertiesOrSnapshot,
|
||||
inlineSnapshot
|
||||
) {
|
||||
const matcherName = 'toMatchInlineSnapshot';
|
||||
let properties;
|
||||
const length = arguments.length;
|
||||
|
||||
if (length === 2 && typeof propertiesOrSnapshot === 'string') {
|
||||
inlineSnapshot = propertiesOrSnapshot;
|
||||
} else if (length >= 2) {
|
||||
const options = {
|
||||
isNot: this.isNot,
|
||||
promise: this.promise
|
||||
};
|
||||
|
||||
if (length === 3) {
|
||||
options.secondArgument = _printSnapshot.SNAPSHOT_ARG;
|
||||
options.secondArgumentColor = _printSnapshot.noColor;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof propertiesOrSnapshot !== 'object' ||
|
||||
propertiesOrSnapshot === null
|
||||
) {
|
||||
throw new Error(
|
||||
(0, _jestMatcherUtils.matcherErrorMessage)(
|
||||
(0, _jestMatcherUtils.matcherHint)(
|
||||
matcherName,
|
||||
undefined,
|
||||
_printSnapshot.PROPERTIES_ARG,
|
||||
options
|
||||
),
|
||||
`Expected ${(0, _jestMatcherUtils.EXPECTED_COLOR)(
|
||||
'properties'
|
||||
)} must be an object`,
|
||||
(0, _jestMatcherUtils.printWithType)(
|
||||
'Expected properties',
|
||||
propertiesOrSnapshot,
|
||||
_printSnapshot.printExpected
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (length === 3 && typeof inlineSnapshot !== 'string') {
|
||||
throw new Error(
|
||||
(0, _jestMatcherUtils.matcherErrorMessage)(
|
||||
(0, _jestMatcherUtils.matcherHint)(
|
||||
matcherName,
|
||||
undefined,
|
||||
_printSnapshot.PROPERTIES_ARG,
|
||||
options
|
||||
),
|
||||
`Inline snapshot must be a string`,
|
||||
(0, _jestMatcherUtils.printWithType)(
|
||||
'Inline snapshot',
|
||||
inlineSnapshot,
|
||||
utils.serialize
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
properties = propertiesOrSnapshot;
|
||||
}
|
||||
|
||||
return _toMatchSnapshot({
|
||||
context: this,
|
||||
inlineSnapshot:
|
||||
inlineSnapshot !== undefined
|
||||
? stripAddedIndentation(inlineSnapshot)
|
||||
: undefined,
|
||||
isInline: true,
|
||||
matcherName,
|
||||
properties,
|
||||
received
|
||||
});
|
||||
};
|
||||
|
||||
const _toMatchSnapshot = config => {
|
||||
const {
|
||||
context,
|
||||
hint,
|
||||
inlineSnapshot,
|
||||
isInline,
|
||||
matcherName,
|
||||
properties
|
||||
} = config;
|
||||
let {received} = config;
|
||||
context.dontThrow && context.dontThrow();
|
||||
const {currentTestName, isNot, snapshotState} = context;
|
||||
|
||||
if (isNot) {
|
||||
throw new Error(
|
||||
(0, _jestMatcherUtils.matcherErrorMessage)(
|
||||
(0, _printSnapshot.matcherHintFromConfig)(config, false),
|
||||
NOT_SNAPSHOT_MATCHERS
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (snapshotState == null) {
|
||||
// Because the state is the problem, this is not a matcher error.
|
||||
// Call generic stringify from jest-matcher-utils package
|
||||
// because uninitialized snapshot state does not need snapshot serializers.
|
||||
throw new Error(
|
||||
(0, _printSnapshot.matcherHintFromConfig)(config, false) +
|
||||
'\n\n' +
|
||||
`Snapshot state must be initialized` +
|
||||
'\n\n' +
|
||||
(0, _jestMatcherUtils.printWithType)(
|
||||
'Snapshot state',
|
||||
snapshotState,
|
||||
_jestMatcherUtils.stringify
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const fullTestName =
|
||||
currentTestName && hint
|
||||
? `${currentTestName}: ${hint}`
|
||||
: currentTestName || ''; // future BREAKING change: || hint
|
||||
|
||||
if (typeof properties === 'object') {
|
||||
if (typeof received !== 'object' || received === null) {
|
||||
throw new Error(
|
||||
(0, _jestMatcherUtils.matcherErrorMessage)(
|
||||
(0, _printSnapshot.matcherHintFromConfig)(config, false),
|
||||
`${(0, _jestMatcherUtils.RECEIVED_COLOR)(
|
||||
'received'
|
||||
)} value must be an object when the matcher has ${(0,
|
||||
_jestMatcherUtils.EXPECTED_COLOR)('properties')}`,
|
||||
(0, _jestMatcherUtils.printWithType)(
|
||||
'Received',
|
||||
received,
|
||||
_printSnapshot.printReceived
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const propertyPass = context.equals(received, properties, [
|
||||
context.utils.iterableEquality,
|
||||
context.utils.subsetEquality
|
||||
]);
|
||||
|
||||
if (!propertyPass) {
|
||||
const key = snapshotState.fail(fullTestName, received);
|
||||
const matched = /(\d+)$/.exec(key);
|
||||
const count = matched === null ? 1 : Number(matched[1]);
|
||||
|
||||
const message = () =>
|
||||
(0, _printSnapshot.matcherHintFromConfig)(config, false) +
|
||||
'\n\n' +
|
||||
printSnapshotName(currentTestName, hint, count) +
|
||||
'\n\n' +
|
||||
(0, _printSnapshot.printPropertiesAndReceived)(
|
||||
properties,
|
||||
received,
|
||||
snapshotState.expand
|
||||
);
|
||||
|
||||
return {
|
||||
message,
|
||||
name: matcherName,
|
||||
pass: false
|
||||
};
|
||||
} else {
|
||||
received = utils.deepMerge(received, properties);
|
||||
}
|
||||
}
|
||||
|
||||
const result = snapshotState.match({
|
||||
error: context.error,
|
||||
inlineSnapshot,
|
||||
isInline,
|
||||
received,
|
||||
testName: fullTestName
|
||||
});
|
||||
const {actual, count, expected, pass} = result;
|
||||
|
||||
if (pass) {
|
||||
return {
|
||||
message: () => '',
|
||||
pass: true
|
||||
};
|
||||
}
|
||||
|
||||
const message =
|
||||
expected === undefined
|
||||
? () =>
|
||||
(0, _printSnapshot.matcherHintFromConfig)(config, true) +
|
||||
'\n\n' +
|
||||
printSnapshotName(currentTestName, hint, count) +
|
||||
'\n\n' +
|
||||
`New snapshot was ${(0, _jestMatcherUtils.BOLD_WEIGHT)(
|
||||
'not written'
|
||||
)}. The update flag ` +
|
||||
`must be explicitly passed to write a new snapshot.\n\n` +
|
||||
`This is likely because this test is run in a continuous integration ` +
|
||||
`(CI) environment in which snapshots are not written by default.\n\n` +
|
||||
`Received:${actual.includes('\n') ? '\n' : ' '}${(0,
|
||||
_printSnapshot.bReceivedColor)(actual)}`
|
||||
: () =>
|
||||
(0, _printSnapshot.matcherHintFromConfig)(config, true) +
|
||||
'\n\n' +
|
||||
printSnapshotName(currentTestName, hint, count) +
|
||||
'\n\n' +
|
||||
(0, _printSnapshot.printSnapshotAndReceived)(
|
||||
expected,
|
||||
actual,
|
||||
received,
|
||||
snapshotState.expand
|
||||
); // Passing 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,
|
||||
expected,
|
||||
message,
|
||||
name: matcherName,
|
||||
pass: false
|
||||
};
|
||||
};
|
||||
|
||||
const toThrowErrorMatchingSnapshot = function (
|
||||
received,
|
||||
hint, // because error TS1016 for hint?: string
|
||||
fromPromise
|
||||
) {
|
||||
const matcherName = 'toThrowErrorMatchingSnapshot'; // Future breaking change: Snapshot hint must be a string
|
||||
// if (hint !== undefined && typeof hint !== string) {}
|
||||
|
||||
return _toThrowErrorMatchingSnapshot(
|
||||
{
|
||||
context: this,
|
||||
hint,
|
||||
isInline: false,
|
||||
matcherName,
|
||||
received
|
||||
},
|
||||
fromPromise
|
||||
);
|
||||
};
|
||||
|
||||
const toThrowErrorMatchingInlineSnapshot = function (
|
||||
received,
|
||||
inlineSnapshot,
|
||||
fromPromise
|
||||
) {
|
||||
const matcherName = 'toThrowErrorMatchingInlineSnapshot';
|
||||
|
||||
if (inlineSnapshot !== undefined && typeof inlineSnapshot !== 'string') {
|
||||
const options = {
|
||||
expectedColor: _printSnapshot.noColor,
|
||||
isNot: this.isNot,
|
||||
promise: this.promise
|
||||
};
|
||||
throw new Error(
|
||||
(0, _jestMatcherUtils.matcherErrorMessage)(
|
||||
(0, _jestMatcherUtils.matcherHint)(
|
||||
matcherName,
|
||||
undefined,
|
||||
_printSnapshot.SNAPSHOT_ARG,
|
||||
options
|
||||
),
|
||||
`Inline snapshot must be a string`,
|
||||
(0, _jestMatcherUtils.printWithType)(
|
||||
'Inline snapshot',
|
||||
inlineSnapshot,
|
||||
utils.serialize
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return _toThrowErrorMatchingSnapshot(
|
||||
{
|
||||
context: this,
|
||||
inlineSnapshot:
|
||||
inlineSnapshot !== undefined
|
||||
? stripAddedIndentation(inlineSnapshot)
|
||||
: undefined,
|
||||
isInline: true,
|
||||
matcherName,
|
||||
received
|
||||
},
|
||||
fromPromise
|
||||
);
|
||||
};
|
||||
|
||||
const _toThrowErrorMatchingSnapshot = (config, fromPromise) => {
|
||||
const {
|
||||
context,
|
||||
hint,
|
||||
inlineSnapshot,
|
||||
isInline,
|
||||
matcherName,
|
||||
received
|
||||
} = config;
|
||||
context.dontThrow && context.dontThrow();
|
||||
const {isNot, promise} = context;
|
||||
|
||||
if (!fromPromise) {
|
||||
if (typeof received !== 'function') {
|
||||
const options = {
|
||||
isNot,
|
||||
promise
|
||||
};
|
||||
throw new Error(
|
||||
(0, _jestMatcherUtils.matcherErrorMessage)(
|
||||
(0, _jestMatcherUtils.matcherHint)(
|
||||
matcherName,
|
||||
undefined,
|
||||
'',
|
||||
options
|
||||
),
|
||||
`${(0, _jestMatcherUtils.RECEIVED_COLOR)(
|
||||
'received'
|
||||
)} value must be a function`,
|
||||
(0, _jestMatcherUtils.printWithType)(
|
||||
'Received',
|
||||
received,
|
||||
_printSnapshot.printReceived
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (isNot) {
|
||||
throw new Error(
|
||||
(0, _jestMatcherUtils.matcherErrorMessage)(
|
||||
(0, _printSnapshot.matcherHintFromConfig)(config, false),
|
||||
NOT_SNAPSHOT_MATCHERS
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
let error;
|
||||
|
||||
if (fromPromise) {
|
||||
error = received;
|
||||
} else {
|
||||
try {
|
||||
received();
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
}
|
||||
|
||||
if (error === undefined) {
|
||||
// Because the received value is a function, this is not a matcher error.
|
||||
throw new Error(
|
||||
(0, _printSnapshot.matcherHintFromConfig)(config, false) +
|
||||
'\n\n' +
|
||||
DID_NOT_THROW
|
||||
);
|
||||
}
|
||||
|
||||
return _toMatchSnapshot({
|
||||
context,
|
||||
hint,
|
||||
inlineSnapshot,
|
||||
isInline,
|
||||
matcherName,
|
||||
received: error.message
|
||||
});
|
||||
};
|
||||
|
||||
const JestSnapshot = {
|
||||
EXTENSION: _snapshot_resolver.EXTENSION,
|
||||
SnapshotState: _State.default,
|
||||
addSerializer: _plugins.addSerializer,
|
||||
buildSnapshotResolver: _snapshot_resolver.buildSnapshotResolver,
|
||||
cleanup,
|
||||
getSerializers: _plugins.getSerializers,
|
||||
isSnapshotPath: _snapshot_resolver.isSnapshotPath,
|
||||
toMatchInlineSnapshot,
|
||||
toMatchSnapshot,
|
||||
toThrowErrorMatchingInlineSnapshot,
|
||||
toThrowErrorMatchingSnapshot,
|
||||
utils
|
||||
};
|
||||
/* eslint-disable-next-line no-redeclare */
|
||||
|
||||
module.exports = JestSnapshot;
|
12
node_modules/jest-snapshot/build/inline_snapshots.d.ts
generated
vendored
Normal file
12
node_modules/jest-snapshot/build/inline_snapshots.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import type { Frame } from 'jest-message-util';
|
||||
export declare type InlineSnapshot = {
|
||||
snapshot: string;
|
||||
frame: Frame;
|
||||
};
|
||||
export declare function saveInlineSnapshots(snapshots: Array<InlineSnapshot>, prettier: typeof import('prettier') | null, babelTraverse: Function): void;
|
340
node_modules/jest-snapshot/build/inline_snapshots.js
generated
vendored
Normal file
340
node_modules/jest-snapshot/build/inline_snapshots.js
generated
vendored
Normal file
|
@ -0,0 +1,340 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.saveInlineSnapshots = saveInlineSnapshots;
|
||||
|
||||
var path = _interopRequireWildcard(require('path'));
|
||||
|
||||
var fs = _interopRequireWildcard(require('graceful-fs'));
|
||||
|
||||
var _semver = _interopRequireDefault(require('semver'));
|
||||
|
||||
var _types = require('@babel/types');
|
||||
|
||||
var _utils = require('./utils');
|
||||
|
||||
function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : {default: obj};
|
||||
}
|
||||
|
||||
function _getRequireWildcardCache() {
|
||||
if (typeof WeakMap !== 'function') return null;
|
||||
var cache = new WeakMap();
|
||||
_getRequireWildcardCache = function () {
|
||||
return cache;
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
function _interopRequireWildcard(obj) {
|
||||
if (obj && obj.__esModule) {
|
||||
return obj;
|
||||
}
|
||||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
||||
return {default: obj};
|
||||
}
|
||||
var cache = _getRequireWildcardCache();
|
||||
if (cache && cache.has(obj)) {
|
||||
return cache.get(obj);
|
||||
}
|
||||
var newObj = {};
|
||||
var hasPropertyDescriptor =
|
||||
Object.defineProperty && Object.getOwnPropertyDescriptor;
|
||||
for (var key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
var desc = hasPropertyDescriptor
|
||||
? Object.getOwnPropertyDescriptor(obj, key)
|
||||
: null;
|
||||
if (desc && (desc.get || desc.set)) {
|
||||
Object.defineProperty(newObj, key, desc);
|
||||
} else {
|
||||
newObj[key] = obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
newObj.default = obj;
|
||||
if (cache) {
|
||||
cache.set(obj, newObj);
|
||||
}
|
||||
return newObj;
|
||||
}
|
||||
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var jestWriteFile =
|
||||
global[Symbol.for('jest-native-write-file')] || fs.writeFileSync;
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var jestReadFile =
|
||||
global[Symbol.for('jest-native-read-file')] || fs.readFileSync;
|
||||
|
||||
function saveInlineSnapshots(snapshots, prettier, babelTraverse) {
|
||||
if (!prettier) {
|
||||
throw new Error(
|
||||
`Jest: Inline Snapshots requires Prettier.\n` +
|
||||
`Please ensure "prettier" is installed in your project.`
|
||||
);
|
||||
} // Custom parser API was added in 1.5.0
|
||||
|
||||
if (_semver.default.lt(prettier.version, '1.5.0')) {
|
||||
throw new Error(
|
||||
`Jest: Inline Snapshots require prettier>=1.5.0.\n` +
|
||||
`Please upgrade "prettier".`
|
||||
);
|
||||
}
|
||||
|
||||
const snapshotsByFile = groupSnapshotsByFile(snapshots);
|
||||
|
||||
for (const sourceFilePath of Object.keys(snapshotsByFile)) {
|
||||
saveSnapshotsForFile(
|
||||
snapshotsByFile[sourceFilePath],
|
||||
sourceFilePath,
|
||||
prettier,
|
||||
babelTraverse
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const saveSnapshotsForFile = (
|
||||
snapshots,
|
||||
sourceFilePath,
|
||||
prettier,
|
||||
babelTraverse
|
||||
) => {
|
||||
const sourceFile = jestReadFile(sourceFilePath, 'utf8'); // Resolve project configuration.
|
||||
// For older versions of Prettier, do not load configuration.
|
||||
|
||||
const config = prettier.resolveConfig
|
||||
? prettier.resolveConfig.sync(sourceFilePath, {
|
||||
editorconfig: true
|
||||
})
|
||||
: null; // Detect the parser for the test file.
|
||||
// For older versions of Prettier, fallback to a simple parser detection.
|
||||
|
||||
const inferredParser = prettier.getFileInfo
|
||||
? prettier.getFileInfo.sync(sourceFilePath).inferredParser
|
||||
: (config && config.parser) || simpleDetectParser(sourceFilePath); // Record the matcher names seen in insertion parser and pass them down one
|
||||
// by one to formatting parser.
|
||||
|
||||
const snapshotMatcherNames = []; // Insert snapshots using the custom parser API. After insertion, the code is
|
||||
// formatted, except snapshot indentation. Snapshots cannot be formatted until
|
||||
// after the initial format because we don't know where the call expression
|
||||
// will be placed (specifically its indentation).
|
||||
|
||||
const newSourceFile = prettier.format(sourceFile, {
|
||||
...config,
|
||||
filepath: sourceFilePath,
|
||||
parser: createInsertionParser(
|
||||
snapshots,
|
||||
snapshotMatcherNames,
|
||||
inferredParser,
|
||||
babelTraverse
|
||||
)
|
||||
}); // Format the snapshots using the custom parser API.
|
||||
|
||||
const formattedNewSourceFile = prettier.format(newSourceFile, {
|
||||
...config,
|
||||
filepath: sourceFilePath,
|
||||
parser: createFormattingParser(
|
||||
snapshotMatcherNames,
|
||||
inferredParser,
|
||||
babelTraverse
|
||||
)
|
||||
});
|
||||
|
||||
if (formattedNewSourceFile !== sourceFile) {
|
||||
jestWriteFile(sourceFilePath, formattedNewSourceFile);
|
||||
}
|
||||
};
|
||||
|
||||
const groupSnapshotsBy = createKey => snapshots =>
|
||||
snapshots.reduce((object, inlineSnapshot) => {
|
||||
const key = createKey(inlineSnapshot);
|
||||
return {...object, [key]: (object[key] || []).concat(inlineSnapshot)};
|
||||
}, {});
|
||||
|
||||
const groupSnapshotsByFrame = groupSnapshotsBy(({frame: {line, column}}) =>
|
||||
typeof line === 'number' && typeof column === 'number'
|
||||
? `${line}:${column - 1}`
|
||||
: ''
|
||||
);
|
||||
const groupSnapshotsByFile = groupSnapshotsBy(({frame: {file}}) => file);
|
||||
|
||||
const indent = (snapshot, numIndents, indentation) => {
|
||||
const lines = snapshot.split('\n'); // Prevent re-indentation of inline snapshots.
|
||||
|
||||
if (
|
||||
lines.length >= 2 &&
|
||||
lines[1].startsWith(indentation.repeat(numIndents + 1))
|
||||
) {
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
return lines
|
||||
.map((line, index) => {
|
||||
if (index === 0) {
|
||||
// First line is either a 1-line snapshot or a blank line.
|
||||
return line;
|
||||
} else if (index !== lines.length - 1) {
|
||||
// Do not indent empty lines.
|
||||
if (line === '') {
|
||||
return line;
|
||||
} // Not last line, indent one level deeper than expect call.
|
||||
|
||||
return indentation.repeat(numIndents + 1) + line;
|
||||
} else {
|
||||
// The last line should be placed on the same level as the expect call.
|
||||
return indentation.repeat(numIndents) + line;
|
||||
}
|
||||
})
|
||||
.join('\n');
|
||||
};
|
||||
|
||||
const getAst = (parsers, inferredParser, text) => {
|
||||
// Flow uses a 'Program' parent node, babel expects a 'File'.
|
||||
let ast = parsers[inferredParser](text);
|
||||
|
||||
if (ast.type !== 'File') {
|
||||
ast = (0, _types.file)(ast, ast.comments, ast.tokens);
|
||||
delete ast.program.comments;
|
||||
}
|
||||
|
||||
return ast;
|
||||
}; // This parser inserts snapshots into the AST.
|
||||
|
||||
const createInsertionParser = (
|
||||
snapshots,
|
||||
snapshotMatcherNames,
|
||||
inferredParser,
|
||||
babelTraverse
|
||||
) => (text, parsers, options) => {
|
||||
// Workaround for https://github.com/prettier/prettier/issues/3150
|
||||
options.parser = inferredParser;
|
||||
const groupedSnapshots = groupSnapshotsByFrame(snapshots);
|
||||
const remainingSnapshots = new Set(snapshots.map(({snapshot}) => snapshot));
|
||||
const ast = getAst(parsers, inferredParser, text);
|
||||
babelTraverse(ast, {
|
||||
CallExpression({node: {arguments: args, callee}}) {
|
||||
if (
|
||||
callee.type !== 'MemberExpression' ||
|
||||
callee.property.type !== 'Identifier' ||
|
||||
callee.property.loc == null
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const {line, column} = callee.property.loc.start;
|
||||
const snapshotsForFrame = groupedSnapshots[`${line}:${column}`];
|
||||
|
||||
if (!snapshotsForFrame) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (snapshotsForFrame.length > 1) {
|
||||
throw new Error(
|
||||
'Jest: Multiple inline snapshots for the same call are not supported.'
|
||||
);
|
||||
}
|
||||
|
||||
snapshotMatcherNames.push(callee.property.name);
|
||||
const snapshotIndex = args.findIndex(
|
||||
({type}) => type === 'TemplateLiteral'
|
||||
);
|
||||
const values = snapshotsForFrame.map(({snapshot}) => {
|
||||
remainingSnapshots.delete(snapshot);
|
||||
return (0, _types.templateLiteral)(
|
||||
[
|
||||
(0, _types.templateElement)({
|
||||
raw: (0, _utils.escapeBacktickString)(snapshot)
|
||||
})
|
||||
],
|
||||
[]
|
||||
);
|
||||
});
|
||||
const replacementNode = values[0];
|
||||
|
||||
if (snapshotIndex > -1) {
|
||||
args[snapshotIndex] = replacementNode;
|
||||
} else {
|
||||
args.push(replacementNode);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (remainingSnapshots.size) {
|
||||
throw new Error(`Jest: Couldn't locate all inline snapshots.`);
|
||||
}
|
||||
|
||||
return ast;
|
||||
}; // This parser formats snapshots to the correct indentation.
|
||||
|
||||
const createFormattingParser = (
|
||||
snapshotMatcherNames,
|
||||
inferredParser,
|
||||
babelTraverse
|
||||
) => (text, parsers, options) => {
|
||||
// Workaround for https://github.com/prettier/prettier/issues/3150
|
||||
options.parser = inferredParser;
|
||||
const ast = getAst(parsers, inferredParser, text);
|
||||
babelTraverse(ast, {
|
||||
CallExpression({node: {arguments: args, callee}}) {
|
||||
if (
|
||||
callee.type !== 'MemberExpression' ||
|
||||
callee.property.type !== 'Identifier' ||
|
||||
!snapshotMatcherNames.includes(callee.property.name) ||
|
||||
!callee.loc ||
|
||||
callee.computed
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
let snapshotIndex;
|
||||
let snapshot;
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const node = args[i];
|
||||
|
||||
if (node.type === 'TemplateLiteral') {
|
||||
snapshotIndex = i;
|
||||
snapshot = node.quasis[0].value.raw;
|
||||
}
|
||||
}
|
||||
|
||||
if (snapshot === undefined || snapshotIndex === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const useSpaces = !options.useTabs;
|
||||
snapshot = indent(
|
||||
snapshot,
|
||||
Math.ceil(
|
||||
useSpaces
|
||||
? callee.loc.start.column / options.tabWidth
|
||||
: callee.loc.start.column / 2 // Each tab is 2 characters.
|
||||
),
|
||||
useSpaces ? ' '.repeat(options.tabWidth) : '\t'
|
||||
);
|
||||
const replacementNode = (0, _types.templateLiteral)(
|
||||
[
|
||||
(0, _types.templateElement)({
|
||||
raw: snapshot
|
||||
})
|
||||
],
|
||||
[]
|
||||
);
|
||||
args[snapshotIndex] = replacementNode;
|
||||
}
|
||||
});
|
||||
return ast;
|
||||
};
|
||||
|
||||
const simpleDetectParser = filePath => {
|
||||
const extname = path.extname(filePath);
|
||||
|
||||
if (/tsx?$/.test(extname)) {
|
||||
return 'typescript';
|
||||
}
|
||||
|
||||
return 'babylon';
|
||||
};
|
11
node_modules/jest-snapshot/build/mock_serializer.d.ts
generated
vendored
Normal file
11
node_modules/jest-snapshot/build/mock_serializer.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import type { NewPlugin } from 'pretty-format';
|
||||
export declare const serialize: NewPlugin['serialize'];
|
||||
export declare const test: NewPlugin['test'];
|
||||
declare const plugin: NewPlugin;
|
||||
export default plugin;
|
52
node_modules/jest-snapshot/build/mock_serializer.js
generated
vendored
Normal file
52
node_modules/jest-snapshot/build/mock_serializer.js
generated
vendored
Normal file
|
@ -0,0 +1,52 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.default = exports.test = exports.serialize = void 0;
|
||||
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
const serialize = (val, config, indentation, depth, refs, printer) => {
|
||||
// Serialize a non-default name, even if config.printFunctionName is false.
|
||||
const name = val.getMockName();
|
||||
const nameString = name === 'jest.fn()' ? '' : ' ' + name;
|
||||
let callsString = '';
|
||||
|
||||
if (val.mock.calls.length !== 0) {
|
||||
const indentationNext = indentation + config.indent;
|
||||
callsString =
|
||||
' {' +
|
||||
config.spacingOuter +
|
||||
indentationNext +
|
||||
'"calls": ' +
|
||||
printer(val.mock.calls, config, indentationNext, depth, refs) +
|
||||
(config.min ? ', ' : ',') +
|
||||
config.spacingOuter +
|
||||
indentationNext +
|
||||
'"results": ' +
|
||||
printer(val.mock.results, config, indentationNext, depth, refs) +
|
||||
(config.min ? '' : ',') +
|
||||
config.spacingOuter +
|
||||
indentation +
|
||||
'}';
|
||||
}
|
||||
|
||||
return '[MockFunction' + nameString + ']' + callsString;
|
||||
};
|
||||
|
||||
exports.serialize = serialize;
|
||||
|
||||
const test = val => val && !!val._isMockFunction;
|
||||
|
||||
exports.test = test;
|
||||
const plugin = {
|
||||
serialize,
|
||||
test
|
||||
};
|
||||
var _default = plugin;
|
||||
exports.default = _default;
|
9
node_modules/jest-snapshot/build/plugins.d.ts
generated
vendored
Normal file
9
node_modules/jest-snapshot/build/plugins.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import prettyFormat = require('pretty-format');
|
||||
export declare const addSerializer: (plugin: prettyFormat.Plugin) => void;
|
||||
export declare const getSerializers: () => import("pretty-format/build/types").Plugins;
|
48
node_modules/jest-snapshot/build/plugins.js
generated
vendored
Normal file
48
node_modules/jest-snapshot/build/plugins.js
generated
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.getSerializers = exports.addSerializer = void 0;
|
||||
|
||||
var _prettyFormat = _interopRequireDefault(require('pretty-format'));
|
||||
|
||||
var _mock_serializer = _interopRequireDefault(require('./mock_serializer'));
|
||||
|
||||
function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : {default: obj};
|
||||
}
|
||||
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
const {
|
||||
DOMCollection,
|
||||
DOMElement,
|
||||
Immutable,
|
||||
ReactElement,
|
||||
ReactTestComponent,
|
||||
AsymmetricMatcher
|
||||
} = _prettyFormat.default.plugins;
|
||||
let PLUGINS = [
|
||||
ReactTestComponent,
|
||||
ReactElement,
|
||||
DOMElement,
|
||||
DOMCollection,
|
||||
Immutable,
|
||||
_mock_serializer.default,
|
||||
AsymmetricMatcher
|
||||
]; // Prepend to list so the last added is the first tested.
|
||||
|
||||
const addSerializer = plugin => {
|
||||
PLUGINS = [plugin].concat(PLUGINS);
|
||||
};
|
||||
|
||||
exports.addSerializer = addSerializer;
|
||||
|
||||
const getSerializers = () => PLUGINS;
|
||||
|
||||
exports.getSerializers = getSerializers;
|
24
node_modules/jest-snapshot/build/printSnapshot.d.ts
generated
vendored
Normal file
24
node_modules/jest-snapshot/build/printSnapshot.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,24 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import chalk = require('chalk');
|
||||
import { DiffOptionsColor } from 'jest-diff';
|
||||
import type { MatchSnapshotConfig } from './types';
|
||||
declare type Chalk = chalk.Chalk;
|
||||
export declare const getSnapshotColorForChalkInstance: (chalkInstance: Chalk) => DiffOptionsColor;
|
||||
export declare const getReceivedColorForChalkInstance: (chalkInstance: Chalk) => DiffOptionsColor;
|
||||
export declare const aSnapshotColor: DiffOptionsColor;
|
||||
export declare const bReceivedColor: DiffOptionsColor;
|
||||
export declare const noColor: (string: string) => string;
|
||||
export declare const HINT_ARG = "hint";
|
||||
export declare const SNAPSHOT_ARG = "snapshot";
|
||||
export declare const PROPERTIES_ARG = "properties";
|
||||
export declare const matcherHintFromConfig: ({ context: { isNot, promise }, hint, inlineSnapshot, matcherName, properties, }: MatchSnapshotConfig, isUpdatable: boolean) => string;
|
||||
export declare const printExpected: (val: unknown) => string;
|
||||
export declare const printReceived: (val: unknown) => string;
|
||||
export declare const printPropertiesAndReceived: (properties: object, received: object, expand: boolean) => string;
|
||||
export declare const printSnapshotAndReceived: (a: string, b: string, received: unknown, expand: boolean) => string;
|
||||
export {};
|
382
node_modules/jest-snapshot/build/printSnapshot.js
generated
vendored
Normal file
382
node_modules/jest-snapshot/build/printSnapshot.js
generated
vendored
Normal file
|
@ -0,0 +1,382 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.printSnapshotAndReceived = exports.printPropertiesAndReceived = exports.printReceived = exports.printExpected = exports.matcherHintFromConfig = exports.PROPERTIES_ARG = exports.SNAPSHOT_ARG = exports.HINT_ARG = exports.noColor = exports.bReceivedColor = exports.aSnapshotColor = exports.getReceivedColorForChalkInstance = exports.getSnapshotColorForChalkInstance = void 0;
|
||||
|
||||
var _chalk = _interopRequireDefault(require('chalk'));
|
||||
|
||||
var _utils = require('expect/build/utils');
|
||||
|
||||
var _jestDiff = require('jest-diff');
|
||||
|
||||
var _jestGetType = _interopRequireDefault(require('jest-get-type'));
|
||||
|
||||
var _jestMatcherUtils = require('jest-matcher-utils');
|
||||
|
||||
var _prettyFormat = _interopRequireDefault(require('pretty-format'));
|
||||
|
||||
var _colors = require('./colors');
|
||||
|
||||
var _dedentLines = require('./dedentLines');
|
||||
|
||||
var _utils2 = require('./utils');
|
||||
|
||||
function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : {default: obj};
|
||||
}
|
||||
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
// Temporary hack because getObjectSubset has known limitations,
|
||||
// is not in the public interface of the expect package,
|
||||
// and the long-term goal is to use a non-serialization diff.
|
||||
const getSnapshotColorForChalkInstance = chalkInstance => {
|
||||
const level = chalkInstance.level;
|
||||
|
||||
if (level === 3) {
|
||||
return chalkInstance
|
||||
.rgb(
|
||||
_colors.aForeground3[0],
|
||||
_colors.aForeground3[1],
|
||||
_colors.aForeground3[2]
|
||||
)
|
||||
.bgRgb(
|
||||
_colors.aBackground3[0],
|
||||
_colors.aBackground3[1],
|
||||
_colors.aBackground3[2]
|
||||
);
|
||||
}
|
||||
|
||||
if (level === 2) {
|
||||
return chalkInstance
|
||||
.ansi256(_colors.aForeground2)
|
||||
.bgAnsi256(_colors.aBackground2);
|
||||
}
|
||||
|
||||
return chalkInstance.magenta.bgYellowBright;
|
||||
};
|
||||
|
||||
exports.getSnapshotColorForChalkInstance = getSnapshotColorForChalkInstance;
|
||||
|
||||
const getReceivedColorForChalkInstance = chalkInstance => {
|
||||
const level = chalkInstance.level;
|
||||
|
||||
if (level === 3) {
|
||||
return chalkInstance
|
||||
.rgb(
|
||||
_colors.bForeground3[0],
|
||||
_colors.bForeground3[1],
|
||||
_colors.bForeground3[2]
|
||||
)
|
||||
.bgRgb(
|
||||
_colors.bBackground3[0],
|
||||
_colors.bBackground3[1],
|
||||
_colors.bBackground3[2]
|
||||
);
|
||||
}
|
||||
|
||||
if (level === 2) {
|
||||
return chalkInstance
|
||||
.ansi256(_colors.bForeground2)
|
||||
.bgAnsi256(_colors.bBackground2);
|
||||
}
|
||||
|
||||
return chalkInstance.cyan.bgWhiteBright; // also known as teal
|
||||
};
|
||||
|
||||
exports.getReceivedColorForChalkInstance = getReceivedColorForChalkInstance;
|
||||
const aSnapshotColor = getSnapshotColorForChalkInstance(_chalk.default);
|
||||
exports.aSnapshotColor = aSnapshotColor;
|
||||
const bReceivedColor = getReceivedColorForChalkInstance(_chalk.default);
|
||||
exports.bReceivedColor = bReceivedColor;
|
||||
|
||||
const noColor = string => string;
|
||||
|
||||
exports.noColor = noColor;
|
||||
const HINT_ARG = 'hint';
|
||||
exports.HINT_ARG = HINT_ARG;
|
||||
const SNAPSHOT_ARG = 'snapshot';
|
||||
exports.SNAPSHOT_ARG = SNAPSHOT_ARG;
|
||||
const PROPERTIES_ARG = 'properties';
|
||||
exports.PROPERTIES_ARG = PROPERTIES_ARG;
|
||||
|
||||
const matcherHintFromConfig = (
|
||||
{context: {isNot, promise}, hint, inlineSnapshot, matcherName, properties},
|
||||
isUpdatable
|
||||
) => {
|
||||
const options = {
|
||||
isNot,
|
||||
promise
|
||||
};
|
||||
|
||||
if (isUpdatable) {
|
||||
options.receivedColor = bReceivedColor;
|
||||
}
|
||||
|
||||
let expectedArgument = '';
|
||||
|
||||
if (typeof properties === 'object') {
|
||||
expectedArgument = PROPERTIES_ARG;
|
||||
|
||||
if (isUpdatable) {
|
||||
options.expectedColor = noColor;
|
||||
}
|
||||
|
||||
if (typeof hint === 'string' && hint.length !== 0) {
|
||||
options.secondArgument = HINT_ARG;
|
||||
options.secondArgumentColor = _jestMatcherUtils.BOLD_WEIGHT;
|
||||
} else if (typeof inlineSnapshot === 'string') {
|
||||
options.secondArgument = SNAPSHOT_ARG;
|
||||
|
||||
if (isUpdatable) {
|
||||
options.secondArgumentColor = aSnapshotColor;
|
||||
} else {
|
||||
options.secondArgumentColor = noColor;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (typeof hint === 'string' && hint.length !== 0) {
|
||||
expectedArgument = HINT_ARG;
|
||||
options.expectedColor = _jestMatcherUtils.BOLD_WEIGHT;
|
||||
} else if (typeof inlineSnapshot === 'string') {
|
||||
expectedArgument = SNAPSHOT_ARG;
|
||||
|
||||
if (isUpdatable) {
|
||||
options.expectedColor = aSnapshotColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (0, _jestMatcherUtils.matcherHint)(
|
||||
matcherName,
|
||||
undefined,
|
||||
expectedArgument,
|
||||
options
|
||||
);
|
||||
}; // Given array of diffs, return string:
|
||||
// * include common substrings
|
||||
// * exclude change substrings which have opposite op
|
||||
// * include change substrings which have argument op
|
||||
// with change color only if there is a common substring
|
||||
|
||||
exports.matcherHintFromConfig = matcherHintFromConfig;
|
||||
|
||||
const joinDiffs = (diffs, op, hasCommon) =>
|
||||
diffs.reduce(
|
||||
(reduced, diff) =>
|
||||
reduced +
|
||||
(diff[0] === _jestDiff.DIFF_EQUAL
|
||||
? diff[1]
|
||||
: diff[0] !== op
|
||||
? ''
|
||||
: hasCommon
|
||||
? (0, _jestMatcherUtils.INVERTED_COLOR)(diff[1])
|
||||
: diff[1]),
|
||||
''
|
||||
);
|
||||
|
||||
const isLineDiffable = received => {
|
||||
const receivedType = (0, _jestGetType.default)(received);
|
||||
|
||||
if (_jestGetType.default.isPrimitive(received)) {
|
||||
return typeof received === 'string';
|
||||
}
|
||||
|
||||
if (
|
||||
receivedType === 'date' ||
|
||||
receivedType === 'function' ||
|
||||
receivedType === 'regexp'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (received instanceof Error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
receivedType === 'object' &&
|
||||
typeof received.asymmetricMatch === 'function'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const printExpected = val =>
|
||||
(0, _jestMatcherUtils.EXPECTED_COLOR)((0, _utils2.minify)(val));
|
||||
|
||||
exports.printExpected = printExpected;
|
||||
|
||||
const printReceived = val =>
|
||||
(0, _jestMatcherUtils.RECEIVED_COLOR)((0, _utils2.minify)(val));
|
||||
|
||||
exports.printReceived = printReceived;
|
||||
|
||||
const printPropertiesAndReceived = (properties, received, expand) => {
|
||||
const aAnnotation = 'Expected properties';
|
||||
const bAnnotation = 'Received value';
|
||||
|
||||
if (isLineDiffable(properties) && isLineDiffable(received)) {
|
||||
return (0, _jestDiff.diffLinesUnified)(
|
||||
(0, _utils2.serialize)(properties).split('\n'),
|
||||
(0, _utils2.serialize)(
|
||||
(0, _utils.getObjectSubset)(received, properties)
|
||||
).split('\n'),
|
||||
{
|
||||
aAnnotation,
|
||||
aColor: _jestMatcherUtils.EXPECTED_COLOR,
|
||||
bAnnotation,
|
||||
bColor: _jestMatcherUtils.RECEIVED_COLOR,
|
||||
changeLineTrailingSpaceColor: _chalk.default.bgYellow,
|
||||
commonLineTrailingSpaceColor: _chalk.default.bgYellow,
|
||||
emptyFirstOrLastLinePlaceholder: '↵',
|
||||
// U+21B5
|
||||
expand,
|
||||
includeChangeCounts: true
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
|
||||
aAnnotation,
|
||||
bAnnotation
|
||||
);
|
||||
return (
|
||||
printLabel(aAnnotation) +
|
||||
printExpected(properties) +
|
||||
'\n' +
|
||||
printLabel(bAnnotation) +
|
||||
printReceived(received)
|
||||
);
|
||||
};
|
||||
|
||||
exports.printPropertiesAndReceived = printPropertiesAndReceived;
|
||||
const MAX_DIFF_STRING_LENGTH = 20000;
|
||||
|
||||
const printSnapshotAndReceived = (a, b, received, expand) => {
|
||||
const aAnnotation = 'Snapshot';
|
||||
const bAnnotation = 'Received';
|
||||
const aColor = aSnapshotColor;
|
||||
const bColor = bReceivedColor;
|
||||
const options = {
|
||||
aAnnotation,
|
||||
aColor,
|
||||
bAnnotation,
|
||||
bColor,
|
||||
changeLineTrailingSpaceColor: noColor,
|
||||
commonLineTrailingSpaceColor: _chalk.default.bgYellow,
|
||||
emptyFirstOrLastLinePlaceholder: '↵',
|
||||
// U+21B5
|
||||
expand,
|
||||
includeChangeCounts: true
|
||||
};
|
||||
|
||||
if (typeof received === 'string') {
|
||||
if (
|
||||
a.length >= 2 &&
|
||||
a.startsWith('"') &&
|
||||
a.endsWith('"') &&
|
||||
b === (0, _prettyFormat.default)(received)
|
||||
) {
|
||||
// If snapshot looks like default serialization of a string
|
||||
// and received is string which has default serialization.
|
||||
if (!a.includes('\n') && !b.includes('\n')) {
|
||||
// If neither string is multiline,
|
||||
// display as labels and quoted strings.
|
||||
let aQuoted = a;
|
||||
let bQuoted = b;
|
||||
|
||||
if (
|
||||
a.length - 2 <= MAX_DIFF_STRING_LENGTH &&
|
||||
b.length - 2 <= MAX_DIFF_STRING_LENGTH
|
||||
) {
|
||||
const diffs = (0, _jestDiff.diffStringsRaw)(
|
||||
a.slice(1, -1),
|
||||
b.slice(1, -1),
|
||||
true
|
||||
);
|
||||
const hasCommon = diffs.some(
|
||||
diff => diff[0] === _jestDiff.DIFF_EQUAL
|
||||
);
|
||||
aQuoted =
|
||||
'"' + joinDiffs(diffs, _jestDiff.DIFF_DELETE, hasCommon) + '"';
|
||||
bQuoted =
|
||||
'"' + joinDiffs(diffs, _jestDiff.DIFF_INSERT, hasCommon) + '"';
|
||||
}
|
||||
|
||||
const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
|
||||
aAnnotation,
|
||||
bAnnotation
|
||||
);
|
||||
return (
|
||||
printLabel(aAnnotation) +
|
||||
aColor(aQuoted) +
|
||||
'\n' +
|
||||
printLabel(bAnnotation) +
|
||||
bColor(bQuoted)
|
||||
);
|
||||
} // Else either string is multiline, so display as unquoted strings.
|
||||
|
||||
a = (0, _utils2.deserializeString)(a); // hypothetical expected string
|
||||
|
||||
b = received; // not serialized
|
||||
} // Else expected had custom serialization or was not a string
|
||||
// or received has custom serialization.
|
||||
|
||||
return a.length <= MAX_DIFF_STRING_LENGTH &&
|
||||
b.length <= MAX_DIFF_STRING_LENGTH
|
||||
? (0, _jestDiff.diffStringsUnified)(a, b, options)
|
||||
: (0, _jestDiff.diffLinesUnified)(a.split('\n'), b.split('\n'), options);
|
||||
}
|
||||
|
||||
if (isLineDiffable(received)) {
|
||||
const aLines2 = a.split('\n');
|
||||
const bLines2 = b.split('\n'); // Fall through to fix a regression for custom serializers
|
||||
// like jest-snapshot-serializer-raw that ignore the indent option.
|
||||
|
||||
const b0 = (0, _utils2.serialize)(received, 0);
|
||||
|
||||
if (b0 !== b) {
|
||||
const aLines0 = (0, _dedentLines.dedentLines)(aLines2);
|
||||
|
||||
if (aLines0 !== null) {
|
||||
// Compare lines without indentation.
|
||||
const bLines0 = b0.split('\n');
|
||||
return (0, _jestDiff.diffLinesUnified2)(
|
||||
aLines2,
|
||||
bLines2,
|
||||
aLines0,
|
||||
bLines0,
|
||||
options
|
||||
);
|
||||
}
|
||||
} // Fall back because:
|
||||
// * props include a multiline string
|
||||
// * text has more than one adjacent line
|
||||
// * markup does not close
|
||||
|
||||
return (0, _jestDiff.diffLinesUnified)(aLines2, bLines2, options);
|
||||
}
|
||||
|
||||
const printLabel = (0, _jestMatcherUtils.getLabelPrinter)(
|
||||
aAnnotation,
|
||||
bAnnotation
|
||||
);
|
||||
return (
|
||||
printLabel(aAnnotation) +
|
||||
aColor(a) +
|
||||
'\n' +
|
||||
printLabel(bAnnotation) +
|
||||
bColor(b)
|
||||
);
|
||||
};
|
||||
|
||||
exports.printSnapshotAndReceived = printSnapshotAndReceived;
|
16
node_modules/jest-snapshot/build/snapshot_resolver.d.ts
generated
vendored
Normal file
16
node_modules/jest-snapshot/build/snapshot_resolver.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import type { Config } from '@jest/types';
|
||||
export declare type SnapshotResolver = {
|
||||
testPathForConsistencyCheck: string;
|
||||
resolveSnapshotPath(testPath: Config.Path, extension?: string): Config.Path;
|
||||
resolveTestPath(snapshotPath: Config.Path, extension?: string): Config.Path;
|
||||
};
|
||||
export declare const EXTENSION = "snap";
|
||||
export declare const DOT_EXTENSION: string;
|
||||
export declare const isSnapshotPath: (path: string) => boolean;
|
||||
export declare const buildSnapshotResolver: (config: Config.ProjectConfig) => SnapshotResolver;
|
159
node_modules/jest-snapshot/build/snapshot_resolver.js
generated
vendored
Normal file
159
node_modules/jest-snapshot/build/snapshot_resolver.js
generated
vendored
Normal file
|
@ -0,0 +1,159 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.buildSnapshotResolver = exports.isSnapshotPath = exports.DOT_EXTENSION = exports.EXTENSION = void 0;
|
||||
|
||||
var path = _interopRequireWildcard(require('path'));
|
||||
|
||||
var _chalk = _interopRequireDefault(require('chalk'));
|
||||
|
||||
function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : {default: obj};
|
||||
}
|
||||
|
||||
function _getRequireWildcardCache() {
|
||||
if (typeof WeakMap !== 'function') return null;
|
||||
var cache = new WeakMap();
|
||||
_getRequireWildcardCache = function () {
|
||||
return cache;
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
function _interopRequireWildcard(obj) {
|
||||
if (obj && obj.__esModule) {
|
||||
return obj;
|
||||
}
|
||||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
||||
return {default: obj};
|
||||
}
|
||||
var cache = _getRequireWildcardCache();
|
||||
if (cache && cache.has(obj)) {
|
||||
return cache.get(obj);
|
||||
}
|
||||
var newObj = {};
|
||||
var hasPropertyDescriptor =
|
||||
Object.defineProperty && Object.getOwnPropertyDescriptor;
|
||||
for (var key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
var desc = hasPropertyDescriptor
|
||||
? Object.getOwnPropertyDescriptor(obj, key)
|
||||
: null;
|
||||
if (desc && (desc.get || desc.set)) {
|
||||
Object.defineProperty(newObj, key, desc);
|
||||
} else {
|
||||
newObj[key] = obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
newObj.default = obj;
|
||||
if (cache) {
|
||||
cache.set(obj, newObj);
|
||||
}
|
||||
return newObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
const EXTENSION = 'snap';
|
||||
exports.EXTENSION = EXTENSION;
|
||||
const DOT_EXTENSION = '.' + EXTENSION;
|
||||
exports.DOT_EXTENSION = DOT_EXTENSION;
|
||||
|
||||
const isSnapshotPath = path => path.endsWith(DOT_EXTENSION);
|
||||
|
||||
exports.isSnapshotPath = isSnapshotPath;
|
||||
const cache = new Map();
|
||||
|
||||
const buildSnapshotResolver = config => {
|
||||
const key = config.rootDir;
|
||||
|
||||
if (!cache.has(key)) {
|
||||
cache.set(key, createSnapshotResolver(config.snapshotResolver));
|
||||
}
|
||||
|
||||
return cache.get(key);
|
||||
};
|
||||
|
||||
exports.buildSnapshotResolver = buildSnapshotResolver;
|
||||
|
||||
function createSnapshotResolver(snapshotResolverPath) {
|
||||
return typeof snapshotResolverPath === 'string'
|
||||
? createCustomSnapshotResolver(snapshotResolverPath)
|
||||
: createDefaultSnapshotResolver();
|
||||
}
|
||||
|
||||
function createDefaultSnapshotResolver() {
|
||||
return {
|
||||
resolveSnapshotPath: testPath =>
|
||||
path.join(
|
||||
path.join(path.dirname(testPath), '__snapshots__'),
|
||||
path.basename(testPath) + DOT_EXTENSION
|
||||
),
|
||||
resolveTestPath: snapshotPath =>
|
||||
path.resolve(
|
||||
path.dirname(snapshotPath),
|
||||
'..',
|
||||
path.basename(snapshotPath, DOT_EXTENSION)
|
||||
),
|
||||
testPathForConsistencyCheck: path.posix.join(
|
||||
'consistency_check',
|
||||
'__tests__',
|
||||
'example.test.js'
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
function createCustomSnapshotResolver(snapshotResolverPath) {
|
||||
const custom = require(snapshotResolverPath);
|
||||
|
||||
const keys = [
|
||||
['resolveSnapshotPath', 'function'],
|
||||
['resolveTestPath', 'function'],
|
||||
['testPathForConsistencyCheck', 'string']
|
||||
];
|
||||
keys.forEach(([propName, requiredType]) => {
|
||||
if (typeof custom[propName] !== requiredType) {
|
||||
throw new TypeError(mustImplement(propName, requiredType));
|
||||
}
|
||||
});
|
||||
const customResolver = {
|
||||
resolveSnapshotPath: testPath =>
|
||||
custom.resolveSnapshotPath(testPath, DOT_EXTENSION),
|
||||
resolveTestPath: snapshotPath =>
|
||||
custom.resolveTestPath(snapshotPath, DOT_EXTENSION),
|
||||
testPathForConsistencyCheck: custom.testPathForConsistencyCheck
|
||||
};
|
||||
verifyConsistentTransformations(customResolver);
|
||||
return customResolver;
|
||||
}
|
||||
|
||||
function mustImplement(propName, requiredType) {
|
||||
return (
|
||||
_chalk.default.bold(
|
||||
`Custom snapshot resolver must implement a \`${propName}\` as a ${requiredType}.`
|
||||
) +
|
||||
'\nDocumentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver'
|
||||
);
|
||||
}
|
||||
|
||||
function verifyConsistentTransformations(custom) {
|
||||
const resolvedSnapshotPath = custom.resolveSnapshotPath(
|
||||
custom.testPathForConsistencyCheck
|
||||
);
|
||||
const resolvedTestPath = custom.resolveTestPath(resolvedSnapshotPath);
|
||||
|
||||
if (resolvedTestPath !== custom.testPathForConsistencyCheck) {
|
||||
throw new Error(
|
||||
_chalk.default.bold(
|
||||
`Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${custom.testPathForConsistencyCheck}')) === ${resolvedTestPath}`
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
21
node_modules/jest-snapshot/build/types.d.ts
generated
vendored
Normal file
21
node_modules/jest-snapshot/build/types.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import type { MatcherState } from 'expect';
|
||||
import type SnapshotState from './State';
|
||||
export declare type Context = MatcherState & {
|
||||
snapshotState: SnapshotState;
|
||||
};
|
||||
export declare type MatchSnapshotConfig = {
|
||||
context: Context;
|
||||
hint?: string;
|
||||
inlineSnapshot?: string;
|
||||
isInline: boolean;
|
||||
matcherName: string;
|
||||
properties?: object;
|
||||
received: any;
|
||||
};
|
||||
export declare type SnapshotData = Record<string, string>;
|
1
node_modules/jest-snapshot/build/types.js
generated
vendored
Normal file
1
node_modules/jest-snapshot/build/types.js
generated
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
'use strict';
|
27
node_modules/jest-snapshot/build/utils.d.ts
generated
vendored
Normal file
27
node_modules/jest-snapshot/build/utils.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
import type { Config } from '@jest/types';
|
||||
import type { SnapshotData } from './types';
|
||||
export declare const SNAPSHOT_VERSION = "1";
|
||||
export declare const SNAPSHOT_GUIDE_LINK = "https://goo.gl/fbAQLP";
|
||||
export declare const SNAPSHOT_VERSION_WARNING: string;
|
||||
export declare const testNameToKey: (testName: Config.Path, count: number) => string;
|
||||
export declare const keyToTestName: (key: string) => string;
|
||||
export declare const getSnapshotData: (snapshotPath: Config.Path, update: Config.SnapshotUpdateState) => {
|
||||
data: SnapshotData;
|
||||
dirty: boolean;
|
||||
};
|
||||
export declare const addExtraLineBreaks: (string: string) => string;
|
||||
export declare const removeExtraLineBreaks: (string: string) => string;
|
||||
export declare const removeLinesBeforeExternalMatcherTrap: (stack: string) => string;
|
||||
export declare const serialize: (val: unknown, indent?: number) => string;
|
||||
export declare const minify: (val: unknown) => string;
|
||||
export declare const deserializeString: (stringified: string) => string;
|
||||
export declare const escapeBacktickString: (str: string) => string;
|
||||
export declare const ensureDirectoryExists: (filePath: Config.Path) => void;
|
||||
export declare const saveSnapshotFile: (snapshotData: SnapshotData, snapshotPath: Config.Path) => void;
|
||||
export declare const deepMerge: (target: any, source: any) => any;
|
338
node_modules/jest-snapshot/build/utils.js
generated
vendored
Normal file
338
node_modules/jest-snapshot/build/utils.js
generated
vendored
Normal file
|
@ -0,0 +1,338 @@
|
|||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', {
|
||||
value: true
|
||||
});
|
||||
exports.deepMerge = exports.saveSnapshotFile = exports.ensureDirectoryExists = exports.escapeBacktickString = exports.deserializeString = exports.minify = exports.serialize = exports.removeLinesBeforeExternalMatcherTrap = exports.removeExtraLineBreaks = exports.addExtraLineBreaks = exports.getSnapshotData = exports.keyToTestName = exports.testNameToKey = exports.SNAPSHOT_VERSION_WARNING = exports.SNAPSHOT_GUIDE_LINK = exports.SNAPSHOT_VERSION = void 0;
|
||||
|
||||
var path = _interopRequireWildcard(require('path'));
|
||||
|
||||
var fs = _interopRequireWildcard(require('graceful-fs'));
|
||||
|
||||
var _naturalCompare = _interopRequireDefault(require('natural-compare'));
|
||||
|
||||
var _chalk = _interopRequireDefault(require('chalk'));
|
||||
|
||||
var _prettyFormat = _interopRequireDefault(require('pretty-format'));
|
||||
|
||||
var _plugins = require('./plugins');
|
||||
|
||||
function _interopRequireDefault(obj) {
|
||||
return obj && obj.__esModule ? obj : {default: obj};
|
||||
}
|
||||
|
||||
function _getRequireWildcardCache() {
|
||||
if (typeof WeakMap !== 'function') return null;
|
||||
var cache = new WeakMap();
|
||||
_getRequireWildcardCache = function () {
|
||||
return cache;
|
||||
};
|
||||
return cache;
|
||||
}
|
||||
|
||||
function _interopRequireWildcard(obj) {
|
||||
if (obj && obj.__esModule) {
|
||||
return obj;
|
||||
}
|
||||
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
||||
return {default: obj};
|
||||
}
|
||||
var cache = _getRequireWildcardCache();
|
||||
if (cache && cache.has(obj)) {
|
||||
return cache.get(obj);
|
||||
}
|
||||
var newObj = {};
|
||||
var hasPropertyDescriptor =
|
||||
Object.defineProperty && Object.getOwnPropertyDescriptor;
|
||||
for (var key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
var desc = hasPropertyDescriptor
|
||||
? Object.getOwnPropertyDescriptor(obj, key)
|
||||
: null;
|
||||
if (desc && (desc.get || desc.set)) {
|
||||
Object.defineProperty(newObj, key, desc);
|
||||
} else {
|
||||
newObj[key] = obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
newObj.default = obj;
|
||||
if (cache) {
|
||||
cache.set(obj, newObj);
|
||||
}
|
||||
return newObj;
|
||||
}
|
||||
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var jestWriteFile =
|
||||
global[Symbol.for('jest-native-write-file')] || fs.writeFileSync;
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var jestReadFile =
|
||||
global[Symbol.for('jest-native-read-file')] || fs.readFileSync;
|
||||
var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
||||
var jestExistsFile =
|
||||
global[Symbol.for('jest-native-exists-file')] || fs.existsSync;
|
||||
const SNAPSHOT_VERSION = '1';
|
||||
exports.SNAPSHOT_VERSION = SNAPSHOT_VERSION;
|
||||
const SNAPSHOT_VERSION_REGEXP = /^\/\/ Jest Snapshot v(.+),/;
|
||||
const SNAPSHOT_GUIDE_LINK = 'https://goo.gl/fbAQLP';
|
||||
exports.SNAPSHOT_GUIDE_LINK = SNAPSHOT_GUIDE_LINK;
|
||||
|
||||
const SNAPSHOT_VERSION_WARNING = _chalk.default.yellow(
|
||||
`${_chalk.default.bold('Warning')}: Before you upgrade snapshots, ` +
|
||||
`we recommend that you revert any local changes to tests or other code, ` +
|
||||
`to ensure that you do not store invalid state.`
|
||||
);
|
||||
|
||||
exports.SNAPSHOT_VERSION_WARNING = SNAPSHOT_VERSION_WARNING;
|
||||
|
||||
const writeSnapshotVersion = () =>
|
||||
`// Jest Snapshot v${SNAPSHOT_VERSION}, ${SNAPSHOT_GUIDE_LINK}`;
|
||||
|
||||
const validateSnapshotVersion = snapshotContents => {
|
||||
const versionTest = SNAPSHOT_VERSION_REGEXP.exec(snapshotContents);
|
||||
const version = versionTest && versionTest[1];
|
||||
|
||||
if (!version) {
|
||||
return new Error(
|
||||
_chalk.default.red(
|
||||
`${_chalk.default.bold(
|
||||
'Outdated snapshot'
|
||||
)}: No snapshot header found. ` +
|
||||
`Jest 19 introduced versioned snapshots to ensure all developers ` +
|
||||
`on a project are using the same version of Jest. ` +
|
||||
`Please update all snapshots during this upgrade of Jest.\n\n`
|
||||
) + SNAPSHOT_VERSION_WARNING
|
||||
);
|
||||
}
|
||||
|
||||
if (version < SNAPSHOT_VERSION) {
|
||||
return new Error(
|
||||
_chalk.default.red(
|
||||
`${_chalk.default.red.bold(
|
||||
'Outdated snapshot'
|
||||
)}: The version of the snapshot ` +
|
||||
`file associated with this test is outdated. The snapshot file ` +
|
||||
`version ensures that all developers on a project are using ` +
|
||||
`the same version of Jest. ` +
|
||||
`Please update all snapshots during this upgrade of Jest.\n\n`
|
||||
) +
|
||||
`Expected: v${SNAPSHOT_VERSION}\n` +
|
||||
`Received: v${version}\n\n` +
|
||||
SNAPSHOT_VERSION_WARNING
|
||||
);
|
||||
}
|
||||
|
||||
if (version > SNAPSHOT_VERSION) {
|
||||
return new Error(
|
||||
_chalk.default.red(
|
||||
`${_chalk.default.red.bold(
|
||||
'Outdated Jest version'
|
||||
)}: The version of this ` +
|
||||
`snapshot file indicates that this project is meant to be used ` +
|
||||
`with a newer version of Jest. The snapshot file version ensures ` +
|
||||
`that all developers on a project are using the same version of ` +
|
||||
`Jest. Please update your version of Jest and re-run the tests.\n\n`
|
||||
) +
|
||||
`Expected: v${SNAPSHOT_VERSION}\n` +
|
||||
`Received: v${version}`
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
function isObject(item) {
|
||||
return item && typeof item === 'object' && !Array.isArray(item);
|
||||
}
|
||||
|
||||
const testNameToKey = (testName, count) => testName + ' ' + count;
|
||||
|
||||
exports.testNameToKey = testNameToKey;
|
||||
|
||||
const keyToTestName = key => {
|
||||
if (!/ \d+$/.test(key)) {
|
||||
throw new Error('Snapshot keys must end with a number.');
|
||||
}
|
||||
|
||||
return key.replace(/ \d+$/, '');
|
||||
};
|
||||
|
||||
exports.keyToTestName = keyToTestName;
|
||||
|
||||
const getSnapshotData = (snapshotPath, update) => {
|
||||
const data = Object.create(null);
|
||||
let snapshotContents = '';
|
||||
let dirty = false;
|
||||
|
||||
if (jestExistsFile(snapshotPath)) {
|
||||
try {
|
||||
snapshotContents = jestReadFile(snapshotPath, 'utf8'); // eslint-disable-next-line no-new-func
|
||||
|
||||
const populate = new Function('exports', snapshotContents);
|
||||
populate(data);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const validationResult = validateSnapshotVersion(snapshotContents);
|
||||
const isInvalid = snapshotContents && validationResult;
|
||||
|
||||
if (update === 'none' && isInvalid) {
|
||||
throw validationResult;
|
||||
}
|
||||
|
||||
if ((update === 'all' || update === 'new') && isInvalid) {
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
return {
|
||||
data,
|
||||
dirty
|
||||
};
|
||||
}; // Add extra line breaks at beginning and end of multiline snapshot
|
||||
// to make the content easier to read.
|
||||
|
||||
exports.getSnapshotData = getSnapshotData;
|
||||
|
||||
const addExtraLineBreaks = string =>
|
||||
string.includes('\n') ? `\n${string}\n` : string; // Remove extra line breaks at beginning and end of multiline snapshot.
|
||||
// Instead of trim, which can remove additional newlines or spaces
|
||||
// at beginning or end of the content from a custom serializer.
|
||||
|
||||
exports.addExtraLineBreaks = addExtraLineBreaks;
|
||||
|
||||
const removeExtraLineBreaks = string =>
|
||||
string.length > 2 && string.startsWith('\n') && string.endsWith('\n')
|
||||
? string.slice(1, -1)
|
||||
: string;
|
||||
|
||||
exports.removeExtraLineBreaks = removeExtraLineBreaks;
|
||||
|
||||
const removeLinesBeforeExternalMatcherTrap = stack => {
|
||||
const lines = stack.split('\n');
|
||||
|
||||
for (let i = 0; i < lines.length; i += 1) {
|
||||
// It's a function name specified in `packages/expect/src/index.ts`
|
||||
// for external custom matchers.
|
||||
if (lines[i].includes('__EXTERNAL_MATCHER_TRAP__')) {
|
||||
return lines.slice(i + 1).join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
return stack;
|
||||
};
|
||||
|
||||
exports.removeLinesBeforeExternalMatcherTrap = removeLinesBeforeExternalMatcherTrap;
|
||||
const escapeRegex = true;
|
||||
const printFunctionName = false;
|
||||
|
||||
const serialize = (val, indent = 2) =>
|
||||
normalizeNewlines(
|
||||
(0, _prettyFormat.default)(val, {
|
||||
escapeRegex,
|
||||
indent,
|
||||
plugins: (0, _plugins.getSerializers)(),
|
||||
printFunctionName
|
||||
})
|
||||
);
|
||||
|
||||
exports.serialize = serialize;
|
||||
|
||||
const minify = val =>
|
||||
(0, _prettyFormat.default)(val, {
|
||||
escapeRegex,
|
||||
min: true,
|
||||
plugins: (0, _plugins.getSerializers)(),
|
||||
printFunctionName
|
||||
}); // Remove double quote marks and unescape double quotes and backslashes.
|
||||
|
||||
exports.minify = minify;
|
||||
|
||||
const deserializeString = stringified =>
|
||||
stringified.slice(1, -1).replace(/\\("|\\)/g, '$1');
|
||||
|
||||
exports.deserializeString = deserializeString;
|
||||
|
||||
const escapeBacktickString = str => str.replace(/`|\\|\${/g, '\\$&');
|
||||
|
||||
exports.escapeBacktickString = escapeBacktickString;
|
||||
|
||||
const printBacktickString = str => '`' + escapeBacktickString(str) + '`';
|
||||
|
||||
const ensureDirectoryExists = filePath => {
|
||||
try {
|
||||
fs.mkdirSync(path.join(path.dirname(filePath)), {
|
||||
recursive: true
|
||||
});
|
||||
} catch {}
|
||||
};
|
||||
|
||||
exports.ensureDirectoryExists = ensureDirectoryExists;
|
||||
|
||||
const normalizeNewlines = string => string.replace(/\r\n|\r/g, '\n');
|
||||
|
||||
const saveSnapshotFile = (snapshotData, snapshotPath) => {
|
||||
const snapshots = Object.keys(snapshotData)
|
||||
.sort(_naturalCompare.default)
|
||||
.map(
|
||||
key =>
|
||||
'exports[' +
|
||||
printBacktickString(key) +
|
||||
'] = ' +
|
||||
printBacktickString(normalizeNewlines(snapshotData[key])) +
|
||||
';'
|
||||
);
|
||||
ensureDirectoryExists(snapshotPath);
|
||||
jestWriteFile(
|
||||
snapshotPath,
|
||||
writeSnapshotVersion() + '\n\n' + snapshots.join('\n\n') + '\n'
|
||||
);
|
||||
};
|
||||
|
||||
exports.saveSnapshotFile = saveSnapshotFile;
|
||||
|
||||
const deepMergeArray = (target, source) => {
|
||||
const mergedOutput = Array.from(target);
|
||||
source.forEach((sourceElement, index) => {
|
||||
const targetElement = mergedOutput[index];
|
||||
|
||||
if (Array.isArray(target[index])) {
|
||||
mergedOutput[index] = deepMergeArray(target[index], sourceElement);
|
||||
} else if (isObject(targetElement)) {
|
||||
mergedOutput[index] = deepMerge(target[index], sourceElement);
|
||||
} else {
|
||||
// Source does not exist in target or target is primitive and cannot be deep merged
|
||||
mergedOutput[index] = sourceElement;
|
||||
}
|
||||
});
|
||||
return mergedOutput;
|
||||
}; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
|
||||
const deepMerge = (target, source) => {
|
||||
if (isObject(target) && isObject(source)) {
|
||||
const mergedOutput = {...target};
|
||||
Object.keys(source).forEach(key => {
|
||||
if (isObject(source[key]) && !source[key].$$typeof) {
|
||||
if (!(key in target))
|
||||
Object.assign(mergedOutput, {
|
||||
[key]: source[key]
|
||||
});
|
||||
else mergedOutput[key] = deepMerge(target[key], source[key]);
|
||||
} else if (Array.isArray(source[key])) {
|
||||
mergedOutput[key] = deepMergeArray(target[key], source[key]);
|
||||
} else {
|
||||
Object.assign(mergedOutput, {
|
||||
[key]: source[key]
|
||||
});
|
||||
}
|
||||
});
|
||||
return mergedOutput;
|
||||
} else if (Array.isArray(target) && Array.isArray(source)) {
|
||||
return deepMergeArray(target, source);
|
||||
}
|
||||
|
||||
return target;
|
||||
};
|
||||
|
||||
exports.deepMerge = deepMerge;
|
Loading…
Add table
Add a link
Reference in a new issue