Skip to content

Commit

Permalink
Merge pull request #10 from beda-software/custom-reporter
Browse files Browse the repository at this point in the history
Custom reporter
  • Loading branch information
projkov authored Sep 10, 2024
2 parents 8dcd50f + 5c4f297 commit 0e81966
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
33 changes: 33 additions & 0 deletions src/custom-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class CustomReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
this.totalSuites = 0;
this.suitesCount = 0;
this.testRunId = this._options.TEST_RUN_ID;
this.testRunService = this._options.TEST_RUN_SERVICE;
}

async onRunStart(test) {
this.totalSuites = test.numTotalTestSuites;
const testRun = await this.testRunService.findOne(this.testRunId);
await this.testRunService.update({ ...testRun, ...{ suiteTotal: this.totalSuites, status: 'running' } });
}

onTestStart() {
// Just for the info;
}

async onTestResult() {
this.suitesCount += 1;
const testRun = await this.testRunService.findOne(this.testRunId);
await this.testRunService.update({ ...testRun, ...{ suitePassed: this.suitesCount } });
}

async onRunComplete() {
const testRun = await this.testRunService.findOne(this.testRunId);
await this.testRunService.update({ ...testRun, ...{ status: 'completed' } });
}
}

module.exports = CustomReporter;
32 changes: 26 additions & 6 deletions src/modules/test_runs/testRun.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,47 @@ export class TestRunController {
const testRegex = `/src/suites/${suiteId}/.*\\.(test|spec)\\.[jt]sx?$`;
const optionsWithTest = testId ? { testNamePattern: testId } : {};

const currentSession = await this.sessionService.findOne(sessionId);
const testRun = await this.testRunService.create({
session: currentSession,
suiteId: testId ? testId : suiteId,
});

const options = {
...testOptions,
...{
testRegex: testRegex,
globals: JSON.stringify({
SESSION_ID: sessionId,
}),
reporters: [
'default',
[
'<rootDir>/src/custom-reporter.js',
{
TEST_RUN_ID: testRun.id,
TEST_RUN_SERVICE: this.testRunService,
},
],
],
},
...optionsWithTest,
};

try {
const { results } = await runCLI(options as any, [process.cwd()]);
const currentSession = await this.sessionService.findOne(sessionId);
const testRun = await this.testRunService.create({
session: currentSession,
suiteId: testId ? testId : suiteId,
testResults: results,
const testRunAfterTests = await this.testRunService.findOne(testRun.id);
const finalTestRun = await this.testRunService.update({
...testRunAfterTests,
...{ testResults: results },
});
res.status(200).json({ testRun });
res.status(200).json({ finalTestRun });
} catch (error) {
const failedTestRun = await this.testRunService.findOne(testRun.id);
await this.testRunService.update({
...failedTestRun,
...{ status: 'failed' },
});
res.status(500).json({ message: 'Internal server error', error: error.message });
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/test_runs/testRun.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CreateTestRunDto {
description: 'Run specific test suite',
type: json,
})
testResults: jsonbType;
testResults?: jsonbType;
}

export class InitiateTestRunDto {
Expand Down
15 changes: 15 additions & 0 deletions src/modules/test_runs/testRun.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@ import { Column, CreateDateColumn, Entity, ManyToOne, PrimaryGeneratedColumn } f
import { Session } from '../sessions/session.entity';
import { jsonbType } from 'src/utils/types';

export enum TestRunStatus {
NOT_STARTED = 'not-started',
RUNNING = 'running',
COMPLETED = 'completed',
FAILED = 'failed',
}
@Entity()
export class TestRun {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ name: 'status', type: 'enum', enum: TestRunStatus, default: TestRunStatus.NOT_STARTED })
status: 'not-started' | 'running' | 'completed' | 'failed';

@ManyToOne(() => Session, (session) => session.testEntities)
session: Session;

@Column({ name: 'suite_id', type: 'text' })
suiteId: string;

@Column({ name: 'suite_total', type: 'smallint', nullable: true })
suiteTotal: number;

@Column({ name: 'suite_passed', type: 'smallint', nullable: true })
suitePassed: number;

@Column({ name: 'test_result', type: 'jsonb', nullable: true })
testResults: jsonbType;

Expand Down
4 changes: 4 additions & 0 deletions src/modules/test_runs/testRun.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class TestRunService {
return this.testRunRepository.save(testRun);
}

async update(testRun: TestRun): Promise<TestRun> {
return this.testRunRepository.save(testRun);
}

async findAll(params: any): Promise<TestRun[]> {
return this.testRunRepository.find(params);
}
Expand Down

0 comments on commit 0e81966

Please sign in to comment.