jest-snapshot
Advanced tools
Comparing version 15.1.1 to 15.2.0-alpha.c681f819
@@ -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": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
15643
436
6
2
2
6
1
+ Addednatural-compare@^1.4.0
+ Addedjest-diff@15.2.0-alpha.c681f819(transitive)
+ Addedjest-file-exists@15.2.0-alpha.c681f819(transitive)
+ Addedjest-matcher-utils@15.2.0-alpha.c681f819(transitive)
+ Addedjest-mock@15.2.0-alpha.c681f819(transitive)
+ Addedjest-util@15.2.0-alpha.c681f819(transitive)
+ Addednatural-compare@1.4.0(transitive)
+ Addedpretty-format@4.2.3(transitive)
- Removedjest-diff@15.1.0(transitive)
- Removedjest-file-exists@15.0.0(transitive)
- Removedjest-matcher-utils@15.1.0(transitive)
- Removedjest-mock@15.0.0(transitive)
- Removedjest-util@15.1.1(transitive)
- Removedpretty-format@3.8.0(transitive)
Updatedpretty-format@~4.2.1