From c596721586c59558a5288c2a9ea814636ca7edc8 Mon Sep 17 00:00:00 2001 From: Kevin Anidjar Date: Thu, 12 Jan 2023 18:23:22 +0100 Subject: [PATCH 1/2] Take last failed screenshot --- src/cypress-qatouch-reporter.ts | 11 ++++----- tests/cypress-qatouch-reporter.spec.ts | 31 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/cypress-qatouch-reporter.ts b/src/cypress-qatouch-reporter.ts index daee833..f45c0ec 100644 --- a/src/cypress-qatouch-reporter.ts +++ b/src/cypress-qatouch-reporter.ts @@ -65,8 +65,8 @@ export default class CypressQaTouchReporter { * @param test The test * @returns first screenshot found or null */ - private _getScreenshot(test: Test): string | null { - if (!this._reporterOptions?.screenshotsFolder) { + private _getScreenshot(test: Test, status: Status): string | null { + if (!this._reporterOptions?.screenshotsFolder || status === Status.passed) { return null; } @@ -76,10 +76,10 @@ export default class CypressQaTouchReporter { return ( glob - .sync(`${this._reporterOptions.screenshotsFolder}/**/*.png`) + .sync(`${this._reporterOptions.screenshotsFolder}/**/*.*`) .map((file) => file) .filter((file) => file.includes(filename)) - .at(0) ?? null + .pop() ?? null ); } @@ -92,8 +92,9 @@ export default class CypressQaTouchReporter { public push(test: Test, status: Status): ChildProcess | null { const testRunResultKey = this.TITLE_REGEXP.exec(test.title)?.[1]; let childProcess: ChildProcess | null = null; + let screenshot = null; - const screenshot = this._getScreenshot(test); + screenshot = this._getScreenshot(test, status); if (testRunResultKey) { (childProcess = spawn(this._process.command, this._process.args, { diff --git a/tests/cypress-qatouch-reporter.spec.ts b/tests/cypress-qatouch-reporter.spec.ts index 75d9198..e1ec0c0 100644 --- a/tests/cypress-qatouch-reporter.spec.ts +++ b/tests/cypress-qatouch-reporter.spec.ts @@ -1,8 +1,10 @@ +/* eslint-disable @typescript-eslint/dot-notation */ import CypressQaTouchReporter from "../src/cypress-qatouch-reporter"; import { Runner, Suite, Test } from "mocha"; import { ReporterOptions } from "../src/interfaces/reporter-options.interface"; import { describe, it, expect, test, jest } from "@jest/globals"; import { Status } from "../src/enums/status.enum"; +import path from "path"; describe("Cypress QATouch reporter", () => { const reporterOptions: ReporterOptions = { @@ -10,6 +12,7 @@ describe("Cypress QATouch reporter", () => { apiToken: "xxxxx", projectKey: "my-project-key", testRunKey: "my-test-run-key", + screenshotsFolder: ".", }; const runner: Runner = new Runner(new Suite("Test suite"), false); @@ -100,4 +103,32 @@ describe("Cypress QATouch reporter", () => { jest.restoreAllMocks(); }); }); + + describe("Get screenshot", () => { + it("should return a screenshot if the status is failed", () => { + const file = reporter["_getScreenshot"]( + new Test(path.basename(__filename)), + Status.failed + ); + + expect(file).not.toBeNull(); + }); + + it("should not return a screenshot if the status is failed but no file is found", () => { + const test = new Test(path.basename(__filename)); + test.parent = new Suite("Foo"); + const file = reporter["_getScreenshot"](test, Status.failed); + + expect(file).toBeNull(); + }); + + it("should not return a screenshot if the status is passed", () => { + const file = reporter["_getScreenshot"]( + new Test(path.basename(__filename)), + Status.passed + ); + + expect(file).toBeNull(); + }); + }); }); From ed24ecc6bde7c6655a5c89a7788167507529cd75 Mon Sep 17 00:00:00 2001 From: Kevin Anidjar Date: Thu, 12 Jan 2023 18:27:37 +0100 Subject: [PATCH 2/2] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 985c200..7d1e2c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kanidjar/cypress-qatouch-reporter", - "version": "1.1.0", + "version": "1.1.1", "description": "Push Cypress test results into QA Touch ", "main": "dist/index.js", "scripts": {