Skip to content

Commit

Permalink
fix(screenshot): alert user when toMatchScreenshot uses NaN (#4891)
Browse files Browse the repository at this point in the history
ensures that `deviceScaleFactor` is properly narrowed to type `number`
from `number | undefined`. prior to this commit, the property _could_ be
set to `undefined`. this would result in multiplying by `undefined`,
producing `NaN` in our calculation of mismatched pixels. this value
would be used in a comparison operator, which would always return false.

that is, if `deviceScaleFactor` is `undefined`, we would always fail a
test using this matcher. that logic doesn't actually change - instead,
we throw a much more explicit error instead of silently failing
  • Loading branch information
rwaskiewicz committed Oct 5, 2023
1 parent 8129089 commit a251946
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/testing/jest/jest-27-and-under/matchers/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ export function toMatchScreenshot(compare: d.ScreenshotDiff, opts: d.MatchScreen
}

if (typeof compare.mismatchedPixels !== 'number') {
throw new Error(`expect toMatchScreenshot() value is not a screenshot compare`);
throw new Error(
`expect toMatchScreenshot() value is not a valid screenshot compare object - 'mismatchedPixels' has type '${typeof compare.mismatchedPixels}', but should be a number`,
);
}

if (typeof compare.deviceScaleFactor !== 'number') {
throw new Error(
`expect toMatchScreenshot() value is not a valid screenshot compare object - 'deviceScaleFactor' has type '${typeof compare.deviceScaleFactor}', but should be a number`,
);
}

const device = compare.device || compare.userAgent;
Expand Down

0 comments on commit a251946

Please sign in to comment.