jest-specific-snapshot
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.addSerializer = undefined; | ||
exports.toMatchSpecificSnapshot = exports.addSerializer = undefined; | ||
@@ -36,24 +36,25 @@ var _path = require('path'); | ||
expect.extend({ | ||
toMatchSpecificSnapshot: function toMatchSpecificSnapshot(received, snapshotFile) { | ||
var absoluteSnapshotFile = getAbsolutePathToSnapshot(this.testPath, snapshotFile); | ||
function toMatchSpecificSnapshot(received, snapshotFile) { | ||
var absoluteSnapshotFile = getAbsolutePathToSnapshot(this.testPath, snapshotFile); | ||
var commonSnapshotState = this.snapshotState; | ||
var snapshotState = snapshotsStateMap.get(absoluteSnapshotFile); | ||
var commonSnapshotState = this.snapshotState; | ||
var snapshotState = snapshotsStateMap.get(absoluteSnapshotFile); | ||
if (!snapshotState) { | ||
snapshotState = new _jestSnapshot.SnapshotState(absoluteSnapshotFile, { | ||
updateSnapshot: commonSnapshotState._updateSnapshot, | ||
snapshotPath: absoluteSnapshotFile | ||
}); | ||
snapshotsStateMap.set(absoluteSnapshotFile, snapshotState); | ||
} | ||
if (!snapshotState) { | ||
snapshotState = new _jestSnapshot.SnapshotState(absoluteSnapshotFile, { | ||
updateSnapshot: commonSnapshotState._updateSnapshot, | ||
snapshotPath: absoluteSnapshotFile | ||
}); | ||
snapshotsStateMap.set(absoluteSnapshotFile, snapshotState); | ||
} | ||
var newThis = Object.assign({}, this, { snapshotState: snapshotState }); | ||
var patchedToMatchSnapshot = _jestSnapshot.toMatchSnapshot.bind(newThis); | ||
var newThis = Object.assign({}, this, { snapshotState: snapshotState }); | ||
var patchedToMatchSnapshot = _jestSnapshot.toMatchSnapshot.bind(newThis); | ||
return patchedToMatchSnapshot(received); | ||
} | ||
}); | ||
return patchedToMatchSnapshot(received); | ||
} | ||
exports.addSerializer = _jestSnapshot.addSerializer; | ||
expect.extend({ toMatchSpecificSnapshot: toMatchSpecificSnapshot }); | ||
exports.addSerializer = _jestSnapshot.addSerializer; | ||
exports.toMatchSpecificSnapshot = toMatchSpecificSnapshot; |
{ | ||
"name": "jest-specific-snapshot", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/igor-dv/jest-specific-snapshot", |
@@ -1,12 +0,14 @@ | ||
# Jest Specific Snapshot # | ||
# Jest Specific Snapshot | ||
Jest matcher for multiple snapshot files per test | ||
# Installation # | ||
<b>You can read about the implementation [here](https://medium.com/@davydkin.igor/adding-multi-snapshot-testing-to-jest-b61f23cf17ca)</b> | ||
# Installation | ||
```sh | ||
npm i -D jest-specific-snapshot | ||
npm i -D jest-specific-snapshot | ||
``` | ||
# Example # | ||
# Example | ||
@@ -16,3 +18,3 @@ ```js | ||
// extend jest to have 'toMatchSpecificSnapshot' matcher | ||
require('jest-specifics-snapshot'); | ||
require('jest-specific-snapshot'); | ||
@@ -32,7 +34,7 @@ test('test', () => { | ||
## With Custom Serializer ## | ||
## With Custom Serializer | ||
```js | ||
// extend jest to have 'toMatchSpecificSnapshot' matcher | ||
const addSerializer = require('jest-specifics-snapshot').addSerializer; | ||
const addSerializer = require('jest-specific-snapshot').addSerializer; | ||
@@ -42,11 +44,26 @@ addSerializer(/* Add custom serializer here */); | ||
test('test', () => { | ||
expect(/* thing that matches the custom serializer */) | ||
.toMatchSpecificSnapshot('./specific/custom_serializer/test.shot'); | ||
expect(/* thing that matches the custom serializer */).toMatchSpecificSnapshot( | ||
'./specific/custom_serializer/test.shot' | ||
); | ||
}); | ||
``` | ||
``` | ||
# Limitations # | ||
## Extend `toMatchSpecificSnapshot` | ||
1. Snapshot files should have an extension **other** than `.snap`, since it conflicts with jest. | ||
2. In order to handle the `--updateSnapshot` (`-u`) parameter provided from CLI, there is an abuse of the `SnapshotState._updateSnapshot` private field. TBD - try to use the `globalConfig` to get this state. | ||
3. `.toMatchSpecificSnapshot` does ignore a custom serializers strategy. In order to support custom serializers, you should use the `addSerializer` method explicitly. | ||
```js | ||
const toMatchSpecificSnapshot = require('jest-specifics-snapshot').toMatchSpecificSnapshot; | ||
expect.extend({ | ||
toMatchDecoratedSpecificSnapshot(received, snapshotFile) { | ||
// You can modify received data or create dynamic snapshot path | ||
const data = doSomeThing(received); | ||
return toMatchSpecificSnapshot.call(this, data, snapshotFile); | ||
}, | ||
}); | ||
``` | ||
# Limitations | ||
1. Snapshot files should have an extension **other** than `.snap`, since it conflicts with jest. | ||
2. In order to handle the `--updateSnapshot` (`-u`) parameter provided from CLI, there is an abuse of the `SnapshotState._updateSnapshot` private field. TBD - try to use the `globalConfig` to get this state. | ||
3. `.toMatchSpecificSnapshot` does ignore a custom serializers strategy. In order to support custom serializers, you should use the `addSerializer` method explicitly. |
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
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
7319
67
9