Skip to content

Commit d90298c

Browse files
authored
fix(options): auto-detect colors if noColors option is not specified
1 parent 3d1f054 commit d90298c

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ See [the examples](./examples/README.md) for more detailed usage or read about a
107107
* `customDiffDir`: A custom absolute path of a directory to keep this diff in
108108
* `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.
109109
* `diffDirection`: (default: `horizontal`) (options `horizontal` or `vertical`) Changes diff image layout direction
110-
* `noColors`: (default `false`) Removes coloring from console output, useful if storing the results in a file
110+
* `noColors`: Removes coloring from console output, useful if storing the results in a file
111111
* `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.
112112
* `failureThresholdType`: (default `pixel`) (options `percent` or `pixel`) Sets the type of threshold that would trigger a failure.
113113
* `updatePassedSnapshot`: (default `false`) Updates a snapshot even if it passed the threshold against the existing one.

__tests__/__snapshots__/index.spec.js.snap

+15
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,23 @@ exports[`toMatchImageSnapshot should fail when snapshot has a difference beyond
3434
See diff for details: path/to/result.png"
3535
`;
3636

37+
exports[`toMatchImageSnapshot should not style error message if colors not supported 2`] = `
38+
"Expected image to match or be a close match to snapshot but was 40% different from snapshot (600 differing pixels).
39+
See diff for details: path/to/result.png"
40+
`;
41+
42+
exports[`toMatchImageSnapshot should style error message if colors supported 2`] = `
43+
"Expected image to match or be a close match to snapshot but was 40% different from snapshot (600 differing pixels).
44+
See diff for details: path/to/result.png"
45+
`;
46+
3747
exports[`toMatchImageSnapshot should throw an error if used with .not matcher 1`] = `"Jest: \`.not\` cannot be used with \`.toMatchImageSnapshot()\`."`;
3848

49+
exports[`toMatchImageSnapshot should use noColors options if passed as false and style error message 2`] = `
50+
"Expected image to match or be a close match to snapshot but was 40% different from snapshot (600 differing pixels).
51+
See diff for details: path/to/result.png"
52+
`;
53+
3954
exports[`toMatchImageSnapshot should use noColors options if passed as true and not style error message 2`] = `
4055
"Expected image to match or be a close match to snapshot but was 40% different from snapshot (600 differing pixels).
4156
See diff for details: path/to/result.png"

__tests__/index.spec.js

+53-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ const fs = require('fs');
1717
const path = require('path');
1818

1919
describe('toMatchImageSnapshot', () => {
20-
function setupMock(diffImageToSnapshotResult) {
20+
function setupMock(diffImageToSnapshotResult, mockSupportsColor = true) {
2121
jest.doMock('../src/diff-snapshot', () => ({
2222
runDiffImageToSnapshot: jest.fn(() => diffImageToSnapshotResult),
2323
}));
2424

25+
jest.mock('supports-color', () => mockSupportsColor);
26+
2527
const mockFs = Object.assign({}, fs, {
2628
existsSync: jest.fn(),
2729
unlinkSync: jest.fn(),
@@ -139,6 +141,56 @@ describe('toMatchImageSnapshot', () => {
139141
.toThrowErrorMatchingSnapshot();
140142
});
141143

144+
it('should use noColors options if passed as false and style error message', () => {
145+
const mockDiffResult = {
146+
pass: false,
147+
diffOutputPath: 'path/to/result.png',
148+
diffRatio: 0.4,
149+
diffPixelCount: 600,
150+
};
151+
const mockSupportsColor = false;
152+
153+
setupMock(mockDiffResult, mockSupportsColor);
154+
const { toMatchImageSnapshot } = require('../src/index');
155+
expect.extend({ toMatchImageSnapshot });
156+
157+
expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot({ noColors: false }))
158+
.toThrowErrorMatchingSnapshot();
159+
});
160+
161+
it('should not style error message if colors not supported ', () => {
162+
const mockDiffResult = {
163+
pass: false,
164+
diffOutputPath: 'path/to/result.png',
165+
diffRatio: 0.4,
166+
diffPixelCount: 600,
167+
};
168+
const mockSupportsColor = false;
169+
170+
setupMock(mockDiffResult, mockSupportsColor);
171+
const { toMatchImageSnapshot } = require('../src/index');
172+
expect.extend({ toMatchImageSnapshot });
173+
174+
expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
175+
.toThrowErrorMatchingSnapshot();
176+
});
177+
178+
it('should style error message if colors supported ', () => {
179+
const mockDiffResult = {
180+
pass: false,
181+
diffOutputPath: 'path/to/result.png',
182+
diffRatio: 0.4,
183+
diffPixelCount: 600,
184+
};
185+
186+
setupMock(mockDiffResult);
187+
const { toMatchImageSnapshot } = require('../src/index');
188+
expect.extend({ toMatchImageSnapshot });
189+
190+
expect(() => expect('pretendthisisanimagebuffer').toMatchImageSnapshot())
191+
.toThrowErrorMatchingSnapshot();
192+
});
193+
142194
it('should use custom pixelmatch configuration if passed in', () => {
143195
const mockTestContext = {
144196
testPath: 'path/to/test.spec.js',

src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function configureToMatchImageSnapshot({
128128
customSnapshotsDir: commonCustomSnapshotsDir,
129129
customDiffDir: commonCustomDiffDir,
130130
diffDirection: commonDiffDirection = 'horizontal',
131-
noColors: commonNoColors = false,
131+
noColors: commonNoColors,
132132
failureThreshold: commonFailureThreshold = 0,
133133
failureThresholdType: commonFailureThresholdType = 'pixel',
134134
updatePassedSnapshot: commonUpdatePassedSnapshot = false,
@@ -155,7 +155,11 @@ function configureToMatchImageSnapshot({
155155
const {
156156
testPath, currentTestName, isNot, snapshotState,
157157
} = this;
158-
const chalk = new Chalk({ enabled: !noColors });
158+
const chalkOptions = {};
159+
if (typeof noColors !== 'undefined') {
160+
chalkOptions.enabled = !noColors;
161+
}
162+
const chalk = new Chalk(chalkOptions);
159163

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

0 commit comments

Comments
 (0)