Skip to content

Commit

Permalink
fix: make invalid png error similar to assert view errors (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowusr authored and KuznetsovRoman committed Nov 13, 2024
1 parent dd57550 commit c91e897
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/browser/commands/assert-view/assert-view-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { ImageDiffError } = require("./errors/image-diff-error");
const { NoRefImageError } = require("./errors/no-ref-image-error");
const { InvalidRefImageError } = require("./errors/invalid-ref-image-error");

module.exports = class AssertViewResults {
static fromRawObject(results) {
Expand All @@ -10,6 +11,7 @@ module.exports = class AssertViewResults {
return (
(res.name === ImageDiffError.name && ImageDiffError.fromObject(res)) ||
(res.name === NoRefImageError.name && NoRefImageError.fromObject(res)) ||
(res.name === InvalidRefImageError.name && InvalidRefImageError.fromObject(res)) ||
res
);
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
const Promise = require("bluebird");
const { ImageDiffError } = require("../errors/image-diff-error");
const { NoRefImageError } = require("../errors/no-ref-image-error");
const { InvalidRefImageError } = require("../errors/invalid-ref-image-error");

exports.handleNoRefImage = (currImg, refImg, stateName) => {
return Promise.reject(NoRefImageError.create(stateName, currImg, refImg));
};

exports.handleInvalidRefImage = (currImg, refImg, stateName) => {
return Promise.reject(new InvalidRefImageError(stateName, currImg, refImg));
};

exports.handleImageDiff = (currImg, refImg, stateName, opts) => {
const {
tolerance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
const fs = require("fs-extra");
const { WorkerEvents } = require("../../../../events");

exports.handleNoRefImage = exports.handleImageDiff = async (currImg, refImg, state, { emitter }) => {
await fs.copy(currImg.path, refImg.path);
emitter.emit(WorkerEvents.UPDATE_REFERENCE, { state, refImg });
};
exports.handleNoRefImage =
exports.handleInvalidRefImage =
exports.handleImageDiff =
async (currImg, refImg, state, { emitter }) => {
await fs.copy(currImg.path, refImg.path);
emitter.emit(WorkerEvents.UPDATE_REFERENCE, { state, refImg });
};
10 changes: 0 additions & 10 deletions src/browser/commands/assert-view/errors/invalid-png-error.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/browser/commands/assert-view/errors/invalid-ref-image-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { BaseStateError } from "./base-state-error";
import { ImageInfo, RefImageInfo } from "../../../../types";

type InvalidRefImageErrorConstructor<T> = new (stateName: string, currImg: ImageInfo, refImg: RefImageInfo) => T;

export class InvalidRefImageError extends BaseStateError {
constructor(stateName: string, currImg: ImageInfo, refImg: RefImageInfo) {
super(stateName, currImg, refImg);

this.message = `reference image at ${refImg.path} is not a valid png`;
}

static fromObject<T extends InvalidRefImageError>(
this: InvalidRefImageErrorConstructor<T>,
data: InvalidRefImageError,
): T {
return new this(data.stateName, data.currImg, data.refImg);
}
}
9 changes: 6 additions & 3 deletions src/browser/commands/assert-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const RuntimeConfig = require("../../../config/runtime-config");
const AssertViewResults = require("./assert-view-results");
const { BaseStateError } = require("./errors/base-state-error");
const { AssertViewError } = require("./errors/assert-view-error");
const InvalidPngError = require("./errors/invalid-png-error");

const getIgnoreDiffPixelCountRatio = value => {
const percent = _.isString(value) && value.endsWith("%") ? parseFloat(value.slice(0, -1)) : false;
Expand Down Expand Up @@ -42,7 +41,7 @@ module.exports.default = browser => {
disableAnimation,
} = config;

const { handleNoRefImage, handleImageDiff } = getCaptureProcessors();
const { handleNoRefImage, handleImageDiff, handleInvalidRefImage } = getCaptureProcessors();

const assertView = async (state, selectors, opts) => {
opts = _.defaults(opts, assertViewOpts, {
Expand Down Expand Up @@ -112,7 +111,11 @@ module.exports.default = browser => {
try {
validatePng(refBuffer);
} catch (err) {
throw new InvalidPngError(`Reference image in ${refImg.path} is not a valid png`);
await currImgInst.save(currImg.path);

return handleInvalidRefImage(currImg, refImg, state, { emitter }).catch(e =>
handleCaptureProcessorError(e),
);
}

const {
Expand Down
14 changes: 11 additions & 3 deletions test/src/browser/commands/assert-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const validator = require("png-validator");
const { AssertViewError } = require("src/browser/commands/assert-view/errors/assert-view-error");
const { ImageDiffError } = require("src/browser/commands/assert-view/errors/image-diff-error");
const { NoRefImageError } = require("src/browser/commands/assert-view/errors/no-ref-image-error");
const InvalidPngError = require("src/browser/commands/assert-view/errors/invalid-png-error");
const RuntimeConfig = require("src/config/runtime-config");
const updateRefs = require("src/browser/commands/assert-view/capture-processors/update-refs");
const { mkExistingBrowser_: mkBrowser_, mkSessionStub_ } = require("../../utils");
const { InvalidRefImageError } = require("src/browser/commands/assert-view/errors/invalid-ref-image-error");

describe("assertView command", () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -99,12 +99,20 @@ describe("assertView command", () => {

afterEach(() => sandbox.restore());

it("should throw an error on invalid reference", async () => {
it("should save InvalidRefImageError to assertViewResults", async () => {
const constructorStub = sandbox.stub().returns(Object.create(InvalidRefImageError.prototype));
sandbox.stub(InvalidRefImageError, "constructor").callsFake(constructorStub);

fs.readFile.withArgs("/ref/invalid").resolves("invalidPngBuffer");
validator.pngValidator.withArgs("invalidPngBuffer").throws();
const browser = await initBrowser_({ browser: stubBrowser_({ getScreenshotPath: () => "/ref/invalid" }) });

await assert.isRejected(browser.publicAPI.assertView("plain", ".selector"), InvalidPngError);
await browser.publicAPI.assertView("plain", ".selector");

const [error] = browser.publicAPI.executionContext.testplaneCtx.assertViewResults.get();
assert.instanceOf(error, InvalidRefImageError);
assert.equal(error.stateName, "plain");
assert.equal(error.refImg.path, "/ref/invalid");
});

it("should wait for all selectors to exist", async () => {
Expand Down

0 comments on commit c91e897

Please sign in to comment.