From 40b43b3132c8766e84990a9d5904a8b1cc8cf8d3 Mon Sep 17 00:00:00 2001 From: Eli Sherer Date: Sun, 6 Aug 2017 03:08:10 +0300 Subject: [PATCH] Add noColors option to matcher (#5) feat(matcher): add noColors options --- README.md | 8 ++++++-- __tests__/src/__snapshots__/index.spec.js.snap | 5 +++++ __tests__/src/index.spec.js | 12 ++++++++++++ src/index.js | 6 ++++-- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 35033cb..19ea050 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,17 @@ Jest matcher that performs image comparisons using [Blink-diff](https://github.c ### Optional configuration: -`toMatchImageSnapshot()` takes an optional options object where you can provide your own [blink-diff configuration parameters](http://yahoo.github.io/blink-diff/#object-usage) and/or a custom snapshot identifier string: +`toMatchImageSnapshot()` takes an optional options object where you can provide your own [blink-diff configuration parameters](http://yahoo.github.io/blink-diff/#object-usage) and/or a custom snapshot identifier string and/or forcing no styled output for possibly storing the results in a file: ```javascript it('should demonstrate this matcher`s usage with a custom blink-diff config', () => { ... const blinkDiffConfig = { perceptual: true }; - expect(image).toMatchImageSnapshot({ customDiffConfig: blinkDiffConfig, customSnapshotIdentifier: 'customSnapshotName' }); + expect(image).toMatchImageSnapshot({ + customDiffConfig: blinkDiffConfig, + customSnapshotIdentifier: 'customSnapshotName', + noColors: true // the default is false + }); }); ``` diff --git a/__tests__/src/__snapshots__/index.spec.js.snap b/__tests__/src/__snapshots__/index.spec.js.snap index 47b9cdc..71afb43 100644 --- a/__tests__/src/__snapshots__/index.spec.js.snap +++ b/__tests__/src/__snapshots__/index.spec.js.snap @@ -21,3 +21,8 @@ exports[`toMatchImageSnapshot should fail when snapshot has a difference beyond `; exports[`toMatchImageSnapshot should throw an error if used with .not matcher 1`] = `"Jest: \`.not\` cannot be used with \`.toMatchImageSnapshot()\`."`; + +exports[`toMatchImageSnapshot should use noColors options if passed as true and not style error message 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 169b806..4935c88 100644 --- a/__tests__/src/index.spec.js +++ b/__tests__/src/index.spec.js @@ -68,6 +68,18 @@ describe('toMatchImageSnapshot', () => { .toThrowErrorMatchingSnapshot(); }); + it('should use noColors options if passed as true and not style error message', () => { + // code 1 is result too different: https://github.com/yahoo/blink-diff/blob/master/index.js#L267 + const mockDiffResult = { updated: false, code: 1, diffOutputPath: 'path/to/result.png' }; + setupMock(mockDiffResult); + const { toMatchImageSnapshot } = require('../../src/index'); + expect.extend({ toMatchImageSnapshot }); + + + expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot({ noColors: true })) + .toThrowErrorMatchingSnapshot(); + }); + it('should use custom blink-diff configuration if passed in', () => { const mockTestContext = { testPath: 'path/to/test.spec.js', diff --git a/src/index.js b/src/index.js index 00c5652..416b796 100644 --- a/src/index.js +++ b/src/index.js @@ -15,15 +15,17 @@ const kebabCase = require('lodash/kebabCase'); const merge = require('lodash/merge'); const path = require('path'); -const chalk = require('chalk'); +const Chalk = require('chalk').constructor; const { diffImageToSnapshot } = require('./diff-snapshot'); function updateSnapshotState(oldSnapshotState, newSnapshotState) { return merge({}, oldSnapshotState, newSnapshotState); } -function toMatchImageSnapshot(received, { customSnapshotIdentifier = '', customDiffConfig = {} } = {}) { +function toMatchImageSnapshot(received, { customSnapshotIdentifier = '', customDiffConfig = {}, noColors = false } = {}) { const { testPath, currentTestName, isNot } = this; + const chalk = new Chalk({ enabled: !noColors }); + let { snapshotState } = this; if (isNot) { throw new Error('Jest: `.not` cannot be used with `.toMatchImageSnapshot()`.'); }