Skip to content

Commit

Permalink
fix(options): auto-detect colors if noColors option is not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
avlyalin authored May 27, 2020
1 parent 3d1f054 commit d90298c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
* `customDiffDir`: A custom absolute path of a directory to keep this diff in
* `customSnapshotIdentifier`: A custom name to give this snapshot. If not provided one is computed automatically. When a function is provided it is called with an object containing `testPath`, `currentTestName`, `counter` and `defaultIdentifier` as its first argument. The function must return an identifier to use for the snapshot.
* `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
* `noColors`: (default `false`) Removes coloring from console output, useful if storing the results in a file
* `noColors`: Removes coloring from console output, useful if storing the results in a file
* `failureThreshold`: (default `0`) Sets the threshold that would trigger a test failure based on the `failureThresholdType` selected. This is different to the `customDiffConfig.threshold` above, that is the per pixel failure threshold, this is the failure threshold for the entire comparison.
* `failureThresholdType`: (default `pixel`) (options `percent` or `pixel`) Sets the type of threshold that would trigger a failure.
* `updatePassedSnapshot`: (default `false`) Updates a snapshot even if it passed the threshold against the existing one.
Expand Down
15 changes: 15 additions & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ exports[`toMatchImageSnapshot should fail when snapshot has a difference beyond
See diff for details: path/to/result.png"
`;

exports[`toMatchImageSnapshot should not style error message if colors not supported 2`] = `
"Expected image to match or be a close match to snapshot but was 40% different from snapshot (600 differing pixels).
See diff for details: path/to/result.png"
`;

exports[`toMatchImageSnapshot should style error message if colors supported 2`] = `
"Expected image to match or be a close match to snapshot but was 40% different from snapshot (600 differing pixels).
See diff for details: path/to/result.png"
`;

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 false and style error message 2`] = `
"Expected image to match or be a close match to snapshot but was 40% different from snapshot (600 differing pixels).
See diff for details: path/to/result.png"
`;

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 but was 40% different from snapshot (600 differing pixels).
See diff for details: path/to/result.png"
Expand Down
54 changes: 53 additions & 1 deletion __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ const fs = require('fs');
const path = require('path');

describe('toMatchImageSnapshot', () => {
function setupMock(diffImageToSnapshotResult) {
function setupMock(diffImageToSnapshotResult, mockSupportsColor = true) {
jest.doMock('../src/diff-snapshot', () => ({
runDiffImageToSnapshot: jest.fn(() => diffImageToSnapshotResult),
}));

jest.mock('supports-color', () => mockSupportsColor);

const mockFs = Object.assign({}, fs, {
existsSync: jest.fn(),
unlinkSync: jest.fn(),
Expand Down Expand Up @@ -139,6 +141,56 @@ describe('toMatchImageSnapshot', () => {
.toThrowErrorMatchingSnapshot();
});

it('should use noColors options if passed as false and style error message', () => {
const mockDiffResult = {
pass: false,
diffOutputPath: 'path/to/result.png',
diffRatio: 0.4,
diffPixelCount: 600,
};
const mockSupportsColor = false;

setupMock(mockDiffResult, mockSupportsColor);
const { toMatchImageSnapshot } = require('../src/index');
expect.extend({ toMatchImageSnapshot });

expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot({ noColors: false }))
.toThrowErrorMatchingSnapshot();
});

it('should not style error message if colors not supported ', () => {
const mockDiffResult = {
pass: false,
diffOutputPath: 'path/to/result.png',
diffRatio: 0.4,
diffPixelCount: 600,
};
const mockSupportsColor = false;

setupMock(mockDiffResult, mockSupportsColor);
const { toMatchImageSnapshot } = require('../src/index');
expect.extend({ toMatchImageSnapshot });

expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
.toThrowErrorMatchingSnapshot();
});

it('should style error message if colors supported ', () => {
const mockDiffResult = {
pass: false,
diffOutputPath: 'path/to/result.png',
diffRatio: 0.4,
diffPixelCount: 600,
};

setupMock(mockDiffResult);
const { toMatchImageSnapshot } = require('../src/index');
expect.extend({ toMatchImageSnapshot });

expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
.toThrowErrorMatchingSnapshot();
});

it('should use custom pixelmatch configuration if passed in', () => {
const mockTestContext = {
testPath: 'path/to/test.spec.js',
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function configureToMatchImageSnapshot({
customSnapshotsDir: commonCustomSnapshotsDir,
customDiffDir: commonCustomDiffDir,
diffDirection: commonDiffDirection = 'horizontal',
noColors: commonNoColors = false,
noColors: commonNoColors,
failureThreshold: commonFailureThreshold = 0,
failureThresholdType: commonFailureThresholdType = 'pixel',
updatePassedSnapshot: commonUpdatePassedSnapshot = false,
Expand All @@ -155,7 +155,11 @@ function configureToMatchImageSnapshot({
const {
testPath, currentTestName, isNot, snapshotState,
} = this;
const chalk = new Chalk({ enabled: !noColors });
const chalkOptions = {};
if (typeof noColors !== 'undefined') {
chalkOptions.enabled = !noColors;
}
const chalk = new Chalk(chalkOptions);

const retryTimes = parseInt(global[Symbol.for('RETRY_TIMES')], 10) || 0;

Expand Down

0 comments on commit d90298c

Please sign in to comment.