Skip to content

Commit

Permalink
(fix): mark test as broken when a request error occurs (fixes #649, via
Browse files Browse the repository at this point in the history
  • Loading branch information
matthrobin authored Nov 24, 2023
1 parent 8d48435 commit 659d653
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
25 changes: 18 additions & 7 deletions packages/newman-reporter-allure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface PmItem {
name: string;
passed: boolean;
failedAssertions: string[];
requestError?: string;
consoleLogs: string[];
requestData?: PmRequestData;
responseData?: PmResponseData;
Expand Down Expand Up @@ -381,6 +382,7 @@ class AllureReporter {
}

const failedAssertions = this.currentRunningItem?.pmItem.failedAssertions;
const requestError = this.currentRunningItem?.pmItem.requestError;

if (response && failedAssertions?.length) {
const msg = this.escape(failedAssertions.join(", "));
Expand All @@ -402,6 +404,11 @@ class AllureReporter {
if (this.currentRunningItem) {
this.endTest(this.currentRunningItem.allureTest, status, { message: error.message });
}
} else if (requestError) {
const errorMsg = this.escape(requestError);
if (this.currentRunningItem) {
this.endTest(this.currentRunningItem?.allureTest, Status.BROKEN, { message: errorMsg });
}
} else if (this.currentRunningItem) {
this.endTest(this.currentRunningItem?.allureTest, Status.PASSED);
}
Expand Down Expand Up @@ -440,25 +447,29 @@ class AllureReporter {
response: Response;
},
) {
if (err) {
return;
}

const req = args.request;

const url = `${
req.url.protocol || ""
}://${args.request.url.getHost()}${req.url.getPathWithQuery()}`;

const respStream = args.response.stream;
const respBody = (respStream && Buffer.from(respStream).toString()) || "";

this.runningItems[this.runningItems.length - 1].pmItem.requestData = {
url: url,
method: req.method,
body: req.body,
};

if (err) {
if (this.currentRunningItem) {
this.currentRunningItem.pmItem.passed = false;
this.currentRunningItem.pmItem.requestError = err.message;
}
return;
}

const respStream = args.response.stream;
const respBody = (respStream && Buffer.from(respStream).toString()) || "";

this.runningItems[this.runningItems.length - 1].pmItem.responseData = {
status: args.response.status,
code: args.response.code,
Expand Down
3 changes: 3 additions & 0 deletions packages/newman-reporter-allure/test/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ export const handlers = [
rest.get("http://example.com/test", (req, res, ctx) => {
return res(ctx.status(200));
}),
rest.get("http://example.com/timeout", (req, res, ctx) => {
return res.networkError("Timeout");
}),
];
60 changes: 60 additions & 0 deletions packages/newman-reporter-allure/test/specs/requestError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable @typescript-eslint/quotes */
import { Status } from "allure-js-commons";
import { expect } from "expect";
import { after, before, afterEach, test } from "mocha";
import { runNewman } from "../helpers/runNewman";
import { server } from "../mocks/server";

before(() => server.listen());
afterEach(() => server.resetHandlers());
after(() => server.close());

test("Mark test as failed when a request error occurs", async () => {
const [result] = await runNewman({
item: [
{
name: "testReq",
event: [
{
listen: "test",
script: {
exec: [
'pm.test("Status code is 200", function () {',
" pm.response.to.have.status(200);",
"});",
],
type: "text/javascript",
},
},
],
request: {
method: "GET",
header: [],
url: {
host: ["example", "com"],
path: ["timeout"],
},
},
response: [],
},
],
});
expect(result.status).toBe(Status.BROKEN);
expect(result.parameters).toEqual([
{ name: "Request", value: "GET - http://example.com/timeout" },
]);
expect(result.steps).toEqual([
{
status: "failed" as any,
stage: "finished" as any,
statusDetails: {},
steps: [],
attachments: [],
parameters: [],
name: "Status code is 200",
start: expect.any(Number),
stop: expect.any(Number),
},
]);
expect(result.statusDetails.message).toBe("Timeout");
});

0 comments on commit 659d653

Please sign in to comment.