Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,4 @@
**/__mocks__/**
**/__tests__/**
src
yarn.lock

View File

@@ -0,0 +1,189 @@
'use strict';
const fs = require('fs'); /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/var _require =
require('./utils');const saveSnapshotFile = _require.saveSnapshotFile,getSnapshotData = _require.getSnapshotData,getSnapshotPath = _require.getSnapshotPath,keyToTestName = _require.keyToTestName,serialize = _require.serialize,testNameToKey = _require.testNameToKey,unescape = _require.unescape;
class SnapshotState {
constructor(testPath, options) {
this._snapshotPath = options.snapshotPath || getSnapshotPath(testPath);var _getSnapshotData =
getSnapshotData(
this._snapshotPath,
options.updateSnapshot);const data = _getSnapshotData.data,dirty = _getSnapshotData.dirty;
this._snapshotData = data;
this._dirty = dirty;
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 (keyToTestName(uncheckedKey) === testName) {
this._uncheckedKeys.delete(uncheckedKey);
}
});
}
_addSnapshot(key, receivedSerialized) {
this._dirty = true;
this._snapshotData[key] = receivedSerialized;
}
save() {
const isEmpty = Object.keys(this._snapshotData).length === 0;
const status = {
deleted: false,
saved: false };
if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) {
saveSnapshotFile(this._snapshotData, this._snapshotPath);
status.saved = true;
} else if (isEmpty && fs.existsSync(this._snapshotPath)) {
if (this._updateSnapshot === 'all') {
fs.unlinkSync(this._snapshotPath);
}
status.deleted = true;
}
return status;
}
getUncheckedCount() {
return this._uncheckedKeys.size || 0;
}
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) {
this._counters.set(testName, (this._counters.get(testName) || 0) + 1);
const count = Number(this._counters.get(testName));
if (!key) {
key = testNameToKey(testName, count);
}
this._uncheckedKeys.delete(key);
const receivedSerialized = serialize(received);
const expected = this._snapshotData[key];
const pass = expected === receivedSerialized;
const hasSnapshot = this._snapshotData[key] !== undefined;
if (pass) {
// 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 || !fs.existsSync(this._snapshotPath)) && (
this._updateSnapshot === 'new' || this._updateSnapshot === 'all'))
{
if (this._updateSnapshot === 'all') {
if (!pass) {
if (hasSnapshot) {
this.updated++;
} else {
this.added++;
}
this._addSnapshot(key, receivedSerialized);
} else {
this.matched++;
}
} else {
this._addSnapshot(key, receivedSerialized);
this.added++;
}
return {
actual: '',
count,
expected: '',
pass: true };
} else {
if (!pass) {
this.unmatched++;
return {
actual: unescape(receivedSerialized),
count,
expected: expected ? unescape(expected) : null,
pass: false };
} else {
this.matched++;
return {
actual: '',
count,
expected: '',
pass: true };
}
}
}}
module.exports = SnapshotState;

View File

@@ -0,0 +1,157 @@
'use strict';
const fs = require('fs'); /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/const path = require('path');const diff = require('jest-diff');var _require = require('jest-matcher-utils');const EXPECTED_COLOR = _require.EXPECTED_COLOR,ensureNoExpected = _require.ensureNoExpected,matcherHint = _require.matcherHint,RECEIVED_COLOR = _require.RECEIVED_COLOR;
const SnapshotState = require('./State');var _require2 =
require('./plugins');const addSerializer = _require2.addSerializer,getSerializers = _require2.getSerializers;var _require3 =
require('./utils');const SNAPSHOT_EXTENSION = _require3.SNAPSHOT_EXTENSION;
const fileExists = (filePath, hasteFS) =>
hasteFS.exists(filePath) || fs.existsSync(filePath);
const cleanup = (hasteFS, update) => {
const pattern = '\\.' + SNAPSHOT_EXTENSION + '$';
const files = hasteFS.matchFiles(pattern);
const filesRemoved = files.
filter(
snapshotFile =>
!fileExists(
path.resolve(
path.dirname(snapshotFile),
'..',
path.basename(snapshotFile, '.' + SNAPSHOT_EXTENSION)),
hasteFS)).
map(snapshotFile => {
if (update === 'all') {
fs.unlinkSync(snapshotFile);
}
}).length;
return {
filesRemoved };
};
const toMatchSnapshot = function (received, testName) {
this.dontThrow && this.dontThrow();const
currentTestName = this.currentTestName,isNot = this.isNot,snapshotState = this.snapshotState;
if (isNot) {
throw new Error('Jest: `.not` cannot be used with `.toMatchSnapshot()`.');
}
if (!snapshotState) {
throw new Error('Jest: snapshot state must be initialized.');
}
const result = snapshotState.match(
testName || currentTestName || '',
received);const
count = result.count,pass = result.pass;let
actual = result.actual,expected = result.expected;
let report;
if (pass) {
return { message: '', pass: true };
} else if (!expected) {
report = () =>
`New snapshot was ${RECEIVED_COLOR('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.`;
} else {
expected = (expected || '').trim();
actual = (actual || '').trim();
const diffMessage = diff(expected, actual, {
aAnnotation: 'Snapshot',
bAnnotation: 'Received',
expand: snapshotState.expand });
report = () =>
`${RECEIVED_COLOR('Received value')} does not match ` +
`${EXPECTED_COLOR('stored snapshot ' + count)}.\n\n` + (
diffMessage ||
RECEIVED_COLOR('- ' + (expected || '')) +
'\n' +
EXPECTED_COLOR('+ ' + actual));
}
// Passing the the actual and expected objects so that a custom reporter
// could access them, for example in order to display a custom visual diff,
// or create a different error message
return {
actual,
expected,
message: () =>
matcherHint('.toMatchSnapshot', 'value', '') + '\n\n' + report(),
name: 'toMatchSnapshot',
pass: false,
report };
};
const toThrowErrorMatchingSnapshot = function (received, expected) {
this.dontThrow && this.dontThrow();const
isNot = this.isNot;
if (isNot) {
throw new Error(
'Jest: `.not` cannot be used with `.toThrowErrorMatchingSnapshot()`.');
}
ensureNoExpected(expected, '.toThrowErrorMatchingSnapshot');
let error;
try {
received();
} catch (e) {
error = e;
}
if (error === undefined) {
throw new Error(
matcherHint('.toThrowErrorMatchingSnapshot', '() => {}', '') +
'\n\n' +
`Expected the function to throw an error.\n` +
`But it didn't throw anything.`);
}
return toMatchSnapshot.call(this, error.message);
};
module.exports = {
EXTENSION: SNAPSHOT_EXTENSION,
SnapshotState,
addSerializer,
cleanup,
getSerializers,
toMatchSnapshot,
toThrowErrorMatchingSnapshot };

View File

@@ -0,0 +1,27 @@
'use strict';var _require$plugins =
require('pretty-format').plugins;const HTMLElement = _require$plugins.HTMLElement,Immutable = _require$plugins.Immutable,ReactElement = _require$plugins.ReactElement,ReactTestComponent = _require$plugins.ReactTestComponent; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/let PLUGINS = [HTMLElement, ReactElement, ReactTestComponent].concat(Immutable); // Prepend to list so the last added is the first tested.
exports.addSerializer = plugin => {PLUGINS = [plugin].concat(PLUGINS);};exports.getSerializers = () => PLUGINS;

View File

@@ -0,0 +1,192 @@
'use strict';
const fs = require('fs'); /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/const path = require('path');const chalk = require('chalk');var _require = require('jest-util');const createDirectory = _require.createDirectory;const prettyFormat = require('pretty-format');const naturalCompare = require('natural-compare');var _require2 = require('./plugins');const getSerializers = _require2.getSerializers;const SNAPSHOT_EXTENSION = 'snap';
const SNAPSHOT_VERSION = '1';
const SNAPSHOT_VERSION_REGEXP = /^\/\/ Jest Snapshot v(.+),/;
const SNAPSHOT_GUIDE_LINK = 'https://goo.gl/fbAQLP';
const SNAPSHOT_VERSION_WARNING = chalk.yellow(
`${chalk.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.`);
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.red(
`${chalk.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.red(
`${chalk.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.red(
`${chalk.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;
};
const testNameToKey = (testName, count) =>
testName + ' ' + count;
const keyToTestName = key => {
if (!/ \d+$/.test(key)) {
throw new Error('Snapshot keys must end with a number.');
}
return key.replace(/ \d+$/, '');
};
const getSnapshotPath = testPath =>
path.join(
path.join(path.dirname(testPath), '__snapshots__'),
path.basename(testPath) + '.' + SNAPSHOT_EXTENSION);
const getSnapshotData = (snapshotPath, update) => {
const data = Object.create(null);
let snapshotContents = '';
let dirty = false;
if (fs.existsSync(snapshotPath)) {
try {
snapshotContents = fs.readFileSync(snapshotPath, 'utf8');
// eslint-disable-next-line no-new-func
const populate = new Function('exports', snapshotContents);
populate(data);
} catch (e) {}
}
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 };
};
// Extra line breaks at the beginning and at the end of the snapshot are useful
// to make the content of the snapshot easier to read
const addExtraLineBreaks = string =>
string.includes('\n') ? `\n${string}\n` : string;
const serialize = data => {
return addExtraLineBreaks(
normalizeNewlines(
prettyFormat(data, {
escapeRegex: true,
plugins: getSerializers(),
printFunctionName: false })));
};
// unescape double quotes
const unescape = data => data.replace(/\\(")/g, '$1');
const printBacktickString = str => {
return '`' + str.replace(/`|\\|\${/g, '\\$&') + '`';
};
const ensureDirectoryExists = filePath => {
try {
createDirectory(path.join(path.dirname(filePath)));
} catch (e) {}
};
const normalizeNewlines = string => string.replace(/\r\n|\r/g, '\n');
const saveSnapshotFile = (
snapshotData,
snapshotPath) =>
{
const snapshots = Object.keys(snapshotData).
sort(naturalCompare).
map(
key =>
'exports[' +
printBacktickString(key) +
'] = ' +
printBacktickString(normalizeNewlines(snapshotData[key])) +
';');
ensureDirectoryExists(snapshotPath);
fs.writeFileSync(
snapshotPath,
writeSnapshotVersion() + '\n\n' + snapshots.join('\n\n') + '\n');
};
module.exports = {
SNAPSHOT_EXTENSION,
SNAPSHOT_GUIDE_LINK,
SNAPSHOT_VERSION,
SNAPSHOT_VERSION_WARNING,
ensureDirectoryExists,
getSnapshotData,
getSnapshotPath,
keyToTestName,
saveSnapshotFile,
serialize,
testNameToKey,
unescape };

View File

@@ -0,0 +1,51 @@
{
"_args": [
[
"jest-snapshot@20.0.3",
"C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project"
]
],
"_from": "jest-snapshot@20.0.3",
"_id": "jest-snapshot@20.0.3",
"_inBundle": false,
"_integrity": "sha1-W4R+GtsaTZCFKn+fElCG4YfHZWY=",
"_location": "/react-scripts/jest-snapshot",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "jest-snapshot@20.0.3",
"name": "jest-snapshot",
"escapedName": "jest-snapshot",
"rawSpec": "20.0.3",
"saveSpec": null,
"fetchSpec": "20.0.3"
},
"_requiredBy": [
"/react-scripts/jest-jasmine2",
"/react-scripts/jest/jest-cli"
],
"_resolved": "https://registry.npmjs.org/jest-snapshot/-/jest-snapshot-20.0.3.tgz",
"_spec": "20.0.3",
"_where": "C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project",
"bugs": {
"url": "https://github.com/facebook/jest/issues"
},
"dependencies": {
"chalk": "^1.1.3",
"jest-diff": "^20.0.3",
"jest-matcher-utils": "^20.0.3",
"jest-util": "^20.0.3",
"natural-compare": "^1.4.0",
"pretty-format": "^20.0.3"
},
"homepage": "https://github.com/facebook/jest#readme",
"license": "BSD-3-Clause",
"main": "build/index.js",
"name": "jest-snapshot",
"repository": {
"type": "git",
"url": "git+https://github.com/facebook/jest.git"
},
"version": "20.0.3"
}