Skip to content

Commit

Permalink
fix(index): use counters instead of index from snapshotState to keep …
Browse files Browse the repository at this point in the history
…track of snapshot index
  • Loading branch information
anescobar1991 committed Jun 26, 2017
1 parent 2d919c1 commit c34d432
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
4 changes: 2 additions & 2 deletions __tests__/src/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Object {
}
`;

exports[`toMatchImageSnapshot should fail when diff result is unknown 1`] = `
exports[`toMatchImageSnapshot should fail when diff result is unknown 2`] = `
"Expected image to match or be a close match to snapshot.
See diff for details: path/to/result.png"
`;

exports[`toMatchImageSnapshot should fail when snapshot has a difference beyond allowed threshold 1`] = `
exports[`toMatchImageSnapshot should fail when snapshot has a difference beyond allowed threshold 2`] = `
"Expected image to match or be a close match to snapshot.
See diff for details: path/to/result.png"
`;
Expand Down
12 changes: 6 additions & 6 deletions __tests__/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('toMatchImageSnapshot', () => {
currentTestName: 'test1',
isNot: false,
snapshotState: {
_index: 0,
_counters: new Map(),
_updateSnapshot: 'none',
updated: undefined,
added: true,
Expand All @@ -98,7 +98,7 @@ describe('toMatchImageSnapshot', () => {
currentTestName: 'test',
isNot: false,
snapshotState: {
_index: 0,
_counters: new Map(),
_updateSnapshot: 'none',
updated: undefined,
added: true,
Expand All @@ -122,7 +122,7 @@ describe('toMatchImageSnapshot', () => {
currentTestName: 'test',
isNot: false,
snapshotState: {
_index: 0,
_counters: new Map(),
_updateSnapshot: 'none',
updated: undefined,
added: true,
Expand All @@ -146,7 +146,7 @@ describe('toMatchImageSnapshot', () => {
currentTestName: 'test1',
isNot: false,
snapshotState: {
_index: 0,
_counters: new Map(),
_updateSnapshot: 'all',
updated: undefined,
added: true,
Expand All @@ -170,7 +170,7 @@ describe('toMatchImageSnapshot', () => {
currentTestName: 'test1',
isNot: false,
snapshotState: {
_index: 0,
_counters: new Map(),
update: false,
updated: undefined,
added: true,
Expand All @@ -190,7 +190,7 @@ describe('toMatchImageSnapshot', () => {
currentTestName: 'test1',
isNot: false,
snapshotState: {
_index: 0,
_counters: new Map(),
update: true,
updated: undefined,
added: undefined,
Expand Down
12 changes: 4 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

/* eslint-disable no-underscore-dangle */
const kebabCase = require('lodash/kebabCase');
const merge = require('lodash/merge');
const path = require('path');
Expand All @@ -27,18 +27,14 @@ function toMatchImageSnapshot(received, { customSnapshotIdentifier = '', customD
let { snapshotState } = this;
if (isNot) { throw new Error('Jest: `.not` cannot be used with `.toMatchImageSnapshot()`.'); }

updateSnapshotState(
snapshotState, { _index: snapshotState._index += 1 } // eslint-disable-line no-underscore-dangle
);

// eslint-disable-next-line no-underscore-dangle
const snapshotIdentifier = customSnapshotIdentifier || kebabCase(`${path.basename(testPath)}-${currentTestName}-${snapshotState._index}`);
updateSnapshotState(snapshotState, { _counters: snapshotState._counters.set(currentTestName, (snapshotState._counters.get(currentTestName) || 0) + 1) }); // eslint-disable-line max-len
const snapshotIdentifier = customSnapshotIdentifier || kebabCase(`${path.basename(testPath)}-${currentTestName}-${snapshotState._counters.get(currentTestName)}`);

const result = diffImageToSnapshot({
imageData: received,
snapshotIdentifier,
snapshotsDir: path.join(path.dirname(testPath), '__image_snapshots__'),
updateSnapshot: snapshotState._updateSnapshot === 'all', // eslint-disable-line no-underscore-dangle
updateSnapshot: snapshotState._updateSnapshot === 'all',
customDiffConfig,
});
let pass = true;
Expand Down

0 comments on commit c34d432

Please sign in to comment.