|
8 | 8 | import { Reporter, Test, TestResult } from '@jest/reporters';
|
9 | 9 | import { QaseApi } from 'qaseio';
|
10 | 10 | import chalk from 'chalk';
|
| 11 | +import { execSync } from 'child_process'; |
| 12 | +import { readFileSync } from 'fs'; |
11 | 13 |
|
12 | 14 | enum Envs {
|
13 | 15 | report = 'QASE_REPORT',
|
@@ -70,6 +72,7 @@ class QaseReporter implements Reporter {
|
70 | 72 | this.api = new QaseApi(
|
71 | 73 | this.getEnv(Envs.apiToken) || this.options.apiToken || '',
|
72 | 74 | this.getEnv(Envs.basePath) || this.options.basePath,
|
| 75 | + this.createHeaders(), |
73 | 76 | );
|
74 | 77 |
|
75 | 78 | this.log(chalk`{yellow Current PID: ${process.pid}}`);
|
@@ -119,7 +122,7 @@ class QaseReporter implements Reporter {
|
119 | 122 | (created) => {
|
120 | 123 | if (created) {
|
121 | 124 | this.runId = created.result?.id;
|
122 |
| - process.env.QASE_RUN_ID = this.runId!.toString(); |
| 125 | + process.env.QASE_RUN_ID = String(this?.runId); |
123 | 126 | this.log(chalk`{green Using run ${this.runId} to publish test results}`);
|
124 | 127 | } else {
|
125 | 128 | this.log(chalk`{red Could not create run in project ${this.options.projectCode}}`);
|
@@ -342,6 +345,43 @@ class QaseReporter implements Reporter {
|
342 | 345 | return caseObject;
|
343 | 346 | });
|
344 | 347 | }
|
| 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 | + } |
345 | 385 | }
|
346 | 386 |
|
347 | 387 | export = QaseReporter;
|
0 commit comments