Socket
Socket
Sign inDemoInstall

jest-snapshot

Package Overview
Dependencies
Maintainers
5
Versions
280
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-snapshot - npm Package Compare versions

Comparing version 15.1.1 to 15.2.0-alpha.c681f819

build/State.js

192

build/index.js

@@ -15,91 +15,133 @@ /**

const SnapshotFile = require('./SnapshotFile');
const diff = require('jest-diff');
const fileExists = require('jest-file-exists');
const fs = require('fs');
const matcher = require('./matcher');
const path = require('path');
const SnapshotState = require('./State');var _require =
const EXTENSION = SnapshotFile.SNAPSHOT_EXTENSION;
const patchAttr = (attr, state) => {
attr.onStart = function (onStart) {
return function (context) {
state.setSpecName(context.getFullName());
state.setCounter(0);
if (onStart) {
onStart(context);
}
};
}(attr.onStart);
};
const patchJasmine = (jasmine, state) => {
jasmine.Spec = (realSpec => {
const Spec = function Spec(attr) {
patchAttr(attr, state);
realSpec.call(this, attr);
};
Spec.prototype = realSpec.prototype;
for (const statics in realSpec) {
if (Object.prototype.hasOwnProperty.call(realSpec, statics)) {
Spec[statics] = realSpec[statics];
}
require('jest-matcher-utils');const EXPECTED_COLOR = _require.EXPECTED_COLOR;const ensureNoExpected = _require.ensureNoExpected;const matcherHint = _require.matcherHint;const RECEIVED_COLOR = _require.RECEIVED_COLOR;var _require2 =
require('./utils');const SNAPSHOT_EXTENSION = _require2.SNAPSHOT_EXTENSION;
const cleanup = (hasteFS, update) => {
const pattern = '\\.' + SNAPSHOT_EXTENSION + '$';
const files = hasteFS.matchFiles(pattern);
const filesRemoved = files.
filter(snapshotFile => !fileExists(
path.resolve(
path.dirname(snapshotFile),
'..',
path.basename(snapshotFile, '.' + SNAPSHOT_EXTENSION)),
hasteFS)).
map(snapshotFile => {
if (update) {
fs.unlinkSync(snapshotFile);
}
return Spec;
})(jasmine.Spec);
}).
length;
return {
filesRemoved };
};
module.exports = {
EXTENSION,
SnapshotFile: SnapshotFile.SnapshotFile,
cleanup(hasteFS, update) {
const pattern = '\\.' + EXTENSION + '$';
const files = hasteFS.matchFiles(pattern);
const filesRemoved = files.
filter(snapshotFile => !fileExists(
path.resolve(
path.dirname(snapshotFile),
'..',
path.basename(snapshotFile, '.' + EXTENSION)),
let snapshotState;
hasteFS)).
const initializeSnapshotState =
(testFile, update) => new SnapshotState(testFile, update);
map(snapshotFile => {
if (update) {
fs.unlinkSync(snapshotFile);
}
}).
length;
const getSnapshotState = () => snapshotState;
return {
filesRemoved };
const toMatchSnapshot = function (received, expected) {
this.dontThrow();const
currentTestName = this.currentTestName;const isNot = this.isNot;const snapshotState = this.snapshotState;
},
matcher,
getSnapshotState: (jasmine, filePath) => {
let _index = 0;
let _name = '';
/* $FlowFixMe */
const state = Object.assign(Object.create(null), {
getCounter: () => _index,
getSpecName: () => _name,
incrementCounter: () => ++_index,
setCounter(index) {
_index = index;
},
setSpecName(name) {
_name = name;
},
snapshot: SnapshotFile.forFile(filePath),
added: 0,
updated: 0,
matched: 0,
unmatched: 0 });
if (isNot) {
throw new Error(
'Jest: `.not` cannot be used with `.toMatchSnapshot()`.');
}
patchJasmine(jasmine, state);
return state;
} };
ensureNoExpected(expected, '.toMatchSnapshot');
if (!snapshotState) {
throw new Error('Jest: snapshot state must be initialized.');
}
const result = snapshotState.match(currentTestName, received);const
pass = result.pass;
if (pass) {
return { pass: true, message: '' };
} else {const
count = result.count;const expected = result.expected;const actual = result.actual;
const expectedString = expected.trim();
const actualString = actual.trim();
const diffMessage = diff(
expectedString,
actualString,
{
aAnnotation: 'Snapshot',
bAnnotation: 'Received' });
const message =
matcherHint('.toMatchSnapshot', 'value', '') + '\n\n' +
`${ RECEIVED_COLOR('Received value') } does not match ` +
`${ EXPECTED_COLOR('stored snapshot ' + count) }.\n\n` + (
diffMessage ||
RECEIVED_COLOR('- ' + expectedString) + '\n' +
EXPECTED_COLOR('+ ' + actualString));
return { pass: false, message };
}
};
const toThrowErrorMatchingSnapshot = function (received, expected) {
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,
cleanup,
getSnapshotState,
initializeSnapshotState,
toMatchSnapshot,
toThrowErrorMatchingSnapshot };

@@ -18,2 +18,3 @@ /**

const fs = require('fs');
const naturalCompare = require('natural-compare');
const path = require('path');

@@ -97,3 +98,4 @@ const prettyFormat = require('pretty-format');

return addExtraLineBreaks(prettyFormat(data, {
plugins: jsxLikePlugins }));
plugins: jsxLikePlugins,
printFunctionName: false }));

@@ -110,3 +112,4 @@ }

if ((this._dirty || this._uncheckedKeys.size) && !isEmpty) {
const snapshots = Object.keys(this._content).sort().map(key =>
const snapshots = Object.keys(this._content).sort(naturalCompare).
map(key =>
'exports[`' + escape(key) + '`] = `' +

@@ -113,0 +116,0 @@ escape(this._content[key]) + '`;');

{
"name": "jest-snapshot",
"version": "15.1.1",
"version": "15.2.0-alpha.c681f819",
"repository": {

@@ -11,6 +11,8 @@ "type": "git",

"dependencies": {
"jest-diff": "^15.1.0",
"jest-file-exists": "^15.0.0",
"jest-util": "^15.1.1",
"pretty-format": "^3.7.0"
"jest-diff": "^15.2.0-alpha.c681f819",
"jest-file-exists": "^15.2.0-alpha.c681f819",
"jest-matcher-utils": "^15.2.0-alpha.c681f819",
"jest-util": "^15.2.0-alpha.c681f819",
"natural-compare": "^1.4.0",
"pretty-format": "~4.2.1"
},

@@ -17,0 +19,0 @@ "scripts": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc