From c34d4321773dd142cb7a0f45377325fc7b84ce2f Mon Sep 17 00:00:00 2001 From: Andres Escobar Date: Mon, 26 Jun 2017 14:17:59 -0700 Subject: [PATCH] fix(index): use counters instead of index from snapshotState to keep track of snapshot index --- __tests__/src/__snapshots__/index.spec.js.snap | 4 ++-- __tests__/src/index.spec.js | 12 ++++++------ src/index.js | 12 ++++-------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/__tests__/src/__snapshots__/index.spec.js.snap b/__tests__/src/__snapshots__/index.spec.js.snap index 07a6aac..47b9cdc 100644 --- a/__tests__/src/__snapshots__/index.spec.js.snap +++ b/__tests__/src/__snapshots__/index.spec.js.snap @@ -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" `; diff --git a/__tests__/src/index.spec.js b/__tests__/src/index.spec.js index 14286a7..169b806 100644 --- a/__tests__/src/index.spec.js +++ b/__tests__/src/index.spec.js @@ -74,7 +74,7 @@ describe('toMatchImageSnapshot', () => { currentTestName: 'test1', isNot: false, snapshotState: { - _index: 0, + _counters: new Map(), _updateSnapshot: 'none', updated: undefined, added: true, @@ -98,7 +98,7 @@ describe('toMatchImageSnapshot', () => { currentTestName: 'test', isNot: false, snapshotState: { - _index: 0, + _counters: new Map(), _updateSnapshot: 'none', updated: undefined, added: true, @@ -122,7 +122,7 @@ describe('toMatchImageSnapshot', () => { currentTestName: 'test', isNot: false, snapshotState: { - _index: 0, + _counters: new Map(), _updateSnapshot: 'none', updated: undefined, added: true, @@ -146,7 +146,7 @@ describe('toMatchImageSnapshot', () => { currentTestName: 'test1', isNot: false, snapshotState: { - _index: 0, + _counters: new Map(), _updateSnapshot: 'all', updated: undefined, added: true, @@ -170,7 +170,7 @@ describe('toMatchImageSnapshot', () => { currentTestName: 'test1', isNot: false, snapshotState: { - _index: 0, + _counters: new Map(), update: false, updated: undefined, added: true, @@ -190,7 +190,7 @@ describe('toMatchImageSnapshot', () => { currentTestName: 'test1', isNot: false, snapshotState: { - _index: 0, + _counters: new Map(), update: true, updated: undefined, added: undefined, diff --git a/src/index.js b/src/index.js index 1c312a4..00c5652 100644 --- a/src/index.js +++ b/src/index.js @@ -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'); @@ -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;