diff --git a/src/browser/commands/assert-view/assert-view-results.js b/src/browser/commands/assert-view/assert-view-results.js index 5b8933a70..0919ecb98 100644 --- a/src/browser/commands/assert-view/assert-view-results.js +++ b/src/browser/commands/assert-view/assert-view-results.js @@ -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) { @@ -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 ); }), diff --git a/src/browser/commands/assert-view/capture-processors/assert-refs.js b/src/browser/commands/assert-view/capture-processors/assert-refs.js index 3541dd984..1d01f1c64 100644 --- a/src/browser/commands/assert-view/capture-processors/assert-refs.js +++ b/src/browser/commands/assert-view/capture-processors/assert-refs.js @@ -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, diff --git a/src/browser/commands/assert-view/capture-processors/update-refs.js b/src/browser/commands/assert-view/capture-processors/update-refs.js index df77941ee..20f1e9c36 100644 --- a/src/browser/commands/assert-view/capture-processors/update-refs.js +++ b/src/browser/commands/assert-view/capture-processors/update-refs.js @@ -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 }); + }; diff --git a/src/browser/commands/assert-view/errors/invalid-png-error.js b/src/browser/commands/assert-view/errors/invalid-png-error.js deleted file mode 100644 index f8c853bd7..000000000 --- a/src/browser/commands/assert-view/errors/invalid-png-error.js +++ /dev/null @@ -1,10 +0,0 @@ -"use strict"; - -module.exports = class InvalidPngError extends Error { - constructor(message) { - super(); - - this.name = this.constructor.name; - this.message = message; - } -}; diff --git a/src/browser/commands/assert-view/errors/invalid-ref-image-error.ts b/src/browser/commands/assert-view/errors/invalid-ref-image-error.ts new file mode 100644 index 000000000..1aa391d85 --- /dev/null +++ b/src/browser/commands/assert-view/errors/invalid-ref-image-error.ts @@ -0,0 +1,19 @@ +import { BaseStateError } from "./base-state-error"; +import { ImageInfo, RefImageInfo } from "../../../../types"; + +type InvalidRefImageErrorConstructor = 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( + this: InvalidRefImageErrorConstructor, + data: InvalidRefImageError, + ): T { + return new this(data.stateName, data.currImg, data.refImg); + } +} diff --git a/src/browser/commands/assert-view/index.js b/src/browser/commands/assert-view/index.js index 93beba354..086dee051 100644 --- a/src/browser/commands/assert-view/index.js +++ b/src/browser/commands/assert-view/index.js @@ -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; @@ -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, { @@ -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 { diff --git a/test/src/browser/commands/assert-view/index.js b/test/src/browser/commands/assert-view/index.js index f222b1979..07393b140 100644 --- a/test/src/browser/commands/assert-view/index.js +++ b/test/src/browser/commands/assert-view/index.js @@ -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(); @@ -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 () => {