Skip to content

Commit 393a1d4

Browse files
authored
Qase jest v1.3.0 alpha.3 (qase-tms#110)
* [qase-jest] Added ability to pass AnalyticsHeaders into API client * [qase-jest] Updated example * [qase-jest] Added os, npm, arch information to headers * [qase-jest] Added function to get packages version from package.json instead the package-lock.json * [qase-jest] Removed logging from the getPackageVersion
1 parent 91d13df commit 393a1d4

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

qase-jest/examples/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"author": "",
1010
"license": "ISC",
1111
"devDependencies": {
12-
"jest": "^27.0.4",
12+
"jest": "^27.0.4"
13+
},
14+
"dependencies": {
1315
"jest-qase-reporter": "../"
1416
}
1517
}

qase-jest/package-lock.json

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

qase-jest/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jest-qase-reporter",
3-
"version": "v1.3.0-alpha.2",
3+
"version": "v1.3.0-alpha.3",
44
"description": "Qase TMS Jest Reporter",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -30,7 +30,7 @@
3030
},
3131
"license": "Apache-2.0",
3232
"dependencies": {
33-
"qaseio": "^2.0.0-alpha.2"
33+
"qaseio": "^2.0.0-alpha.4"
3434
},
3535
"devDependencies": {
3636
"@hutson/npm-deploy-git-tag": "^6.0.0",

qase-jest/src/index.ts

+41-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
import { Reporter, Test, TestResult } from '@jest/reporters';
99
import { QaseApi } from 'qaseio';
1010
import chalk from 'chalk';
11+
import { execSync } from 'child_process';
12+
import { readFileSync } from 'fs';
1113

1214
enum Envs {
1315
report = 'QASE_REPORT',
@@ -70,6 +72,7 @@ class QaseReporter implements Reporter {
7072
this.api = new QaseApi(
7173
this.getEnv(Envs.apiToken) || this.options.apiToken || '',
7274
this.getEnv(Envs.basePath) || this.options.basePath,
75+
this.createHeaders(),
7376
);
7477

7578
this.log(chalk`{yellow Current PID: ${process.pid}}`);
@@ -119,7 +122,7 @@ class QaseReporter implements Reporter {
119122
(created) => {
120123
if (created) {
121124
this.runId = created.result?.id;
122-
process.env.QASE_RUN_ID = this.runId!.toString();
125+
process.env.QASE_RUN_ID = String(this?.runId);
123126
this.log(chalk`{green Using run ${this.runId} to publish test results}`);
124127
} else {
125128
this.log(chalk`{red Could not create run in project ${this.options.projectCode}}`);
@@ -342,6 +345,43 @@ class QaseReporter implements Reporter {
342345
return caseObject;
343346
});
344347
}
348+
349+
private createHeaders() {
350+
const { version: nodeVersion, platform: os, arch } = process;
351+
const npmVersion = execSync('npm -v', { encoding: 'utf8' }).replace(/['"\n]+/g, '');
352+
const qaseapiVersion = this.getPackageVersion('qaseio');
353+
const jestVersion = this.getPackageVersion('jest');
354+
const jestCaseReporterVersion = this.getPackageVersion('jest-qase-reporter');
355+
const xPlatformHeader = `node=${nodeVersion}; npm=${npmVersion}; os=${os}; arch=${arch}`;
356+
// eslint-disable-next-line max-len
357+
const xClientHeader = `jest=${jestVersion as string}; qase-jest=${jestCaseReporterVersion as string}; qaseapi=${qaseapiVersion as string}`;
358+
359+
return {
360+
'X-Client': xClientHeader,
361+
'X-Platform': xPlatformHeader,
362+
};
363+
}
364+
365+
private getPackageVersion(name: string) {
366+
const UNDEFINED = 'undefined';
367+
try {
368+
const pathToPackageJson = require.resolve(`${name}/package.json`, { paths: [process.cwd()] });
369+
if (pathToPackageJson) {
370+
try {
371+
const packageString = readFileSync(pathToPackageJson, { encoding: 'utf8' });
372+
if (packageString) {
373+
const packageObject = JSON.parse(packageString) as { version: string };
374+
return packageObject.version;
375+
}
376+
return UNDEFINED;
377+
} catch (error) {
378+
return UNDEFINED;
379+
}
380+
}
381+
} catch (error) {
382+
return UNDEFINED;
383+
}
384+
}
345385
}
346386

347387
export = QaseReporter;

qase-jest/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"strict": true, /* Enable all strict type-checking options. */
1313
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
1414
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
15-
"types": ["node", "jest"]
15+
"types": ["node", "jest"],
16+
"resolveJsonModule": true
1617
},
1718
"exclude": [
1819
"node_modules",

0 commit comments

Comments
 (0)