Skip to content

Commit

Permalink
Add noColors option to matcher (#5)
Browse files Browse the repository at this point in the history
feat(matcher): add noColors options
  • Loading branch information
elisherer authored and anescobar1991 committed Aug 6, 2017
1 parent 923daf9 commit 40b43b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
```
Expand Down
5 changes: 5 additions & 0 deletions __tests__/src/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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"
`;
12 changes: 12 additions & 0 deletions __tests__/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.'); }

Expand Down

0 comments on commit 40b43b3

Please sign in to comment.