Skip to content

Commit

Permalink
Merge pull request #6 from kanidjar/fix/take-last-screenshot
Browse files Browse the repository at this point in the history
fix(screenshot): take last failed screenshot
  • Loading branch information
kanidjar authored Jan 12, 2023
2 parents 801e271 + ed24ecc commit 846e4eb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
11 changes: 6 additions & 5 deletions src/cypress-qatouch-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
);
}

Expand All @@ -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, {
Expand Down
31 changes: 31 additions & 0 deletions tests/cypress-qatouch-reporter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* 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 = {
domain: "my-domain",
apiToken: "xxxxx",
projectKey: "my-project-key",
testRunKey: "my-test-run-key",
screenshotsFolder: ".",
};

const runner: Runner = new Runner(new Suite("Test suite"), false);
Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit 846e4eb

Please sign in to comment.