Skip to content

Commit 61da55a

Browse files
author
Serhat Bolsu
committed
Allure report integrated with attachment in api request
1 parent f5fb414 commit 61da55a

File tree

5 files changed

+131
-14
lines changed

5 files changed

+131
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/node_modules/*
22
.idea/*
33
.env
4+
/allure-*/*

jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
// clearMocks: false,
1919

2020
// Indicates whether the coverage information should be collected while executing the test
21-
// collectCoverage: false,
21+
collectCoverage: false,
2222

2323
// An array of glob patterns indicating a set of files for which coverage information should be collected
2424
// collectCoverageFrom: undefined,
@@ -97,7 +97,7 @@ module.exports = {
9797
// projects: undefined,
9898

9999
// Use this configuration option to add custom reporters to Jest
100-
// reporters: undefined,
100+
reporters: ["default", "jest-allure"],
101101

102102
// Automatically reset mock state between every test
103103
// resetMocks: false,
@@ -126,7 +126,7 @@ module.exports = {
126126
setupFiles: ["<rootDir>/config/setup.js"],
127127

128128
// A list of paths to modules that run some code to configure or set up the testing framework before each test
129-
// setupFilesAfterEnv: [],
129+
setupFilesAfterEnv: ["jest-allure/dist/setup"],
130130

131131
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
132132
// snapshotSerializers: [],

package-lock.json

Lines changed: 98 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"eslint-plugin-jest": "^23.9.0",
3838
"husky": "^4.2.5",
3939
"jest": "^25.4.0",
40+
"jest-allure": "^0.1.1",
4041
"superagent": "^5.2.2",
4142
"superagent-defaults": "^0.1.14"
4243
},

resources/BaseApi.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,68 @@ class BaseApi {
1717
this.request.set(this.headers);
1818
}
1919

20+
setAllureAttachment(res) {
21+
// TODO: Handle other types that allure report can show properly.
22+
if (Object.keys(res.body).length !== 0) {
23+
const contentType = res.headers['content-type']
24+
.includes('application/json') ? 'application/json' : 'txt';
25+
const obj = contentType === 'application/json' ? JSON.stringify(res.body) : res.body;
26+
reporter.addAttachment(res.req.path, obj, contentType);
27+
}
28+
}
29+
2030
async get(url, headers={}, query={}, body={}) {
2131
url = url.startsWith('/') ? url : `/${url}`;
2232
if (Object.keys(body).length !== 0) {
23-
return this.request.get(this.baseUrl + url)
33+
const res = await this.request.get(this.baseUrl + url)
2434
.send(body)
2535
.set(headers)
2636
.query(query);
37+
this.setAllureAttachment(res);
38+
return res;
2739
} else {
28-
return this.request.get(this.baseUrl + url)
40+
const res = await this.request.get(this.baseUrl + url)
2941
.send({})
3042
.set(headers)
3143
.query(query);
44+
this.setAllureAttachment(res);
45+
return res;
3246
}
3347
}
3448

3549
async post(url, body={}, headers={}) {
3650
url = url.startsWith('/') ? url : `/${url}`;
37-
return this.request.post(this.baseUrl + url)
51+
const res = await this.request.post(this.baseUrl + url)
3852
.send(body)
3953
.set(headers);
54+
this.setAllureAttachment(res);
55+
return res;
4056
}
4157

4258
async put(url, body={}, headers ={}) {
4359
url = url.startsWith('/') ? url : `/${url}`;
44-
return this.request.put(this.baseUrl + url)
60+
const res = await this.request.put(this.baseUrl + url)
4561
.send(body)
4662
.set(headers);
63+
this.setAllureAttachment(res);
64+
return res;
4765
}
4866

4967
async patch(url, body={}, headers ={}) {
5068
url = url.startsWith('/') ? url : `/${url}`;
51-
return this.request.patch(this.baseUrl + url)
69+
const res = await this.request.patch(this.baseUrl + url)
5270
.send(body)
5371
.set(headers);
72+
this.setAllureAttachment(res);
73+
return res;
5474
}
5575

5676
async delete(url, headers ={}) {
5777
url = url.startsWith('/') ? url : `/${url}`;
58-
return this.request.delete(this.baseUrl + url)
78+
const res = await this.request.delete(this.baseUrl + url)
5979
.set(headers);
80+
this.setAllureAttachment(res);
81+
return res;
6082
}
6183
}
6284

0 commit comments

Comments
 (0)