Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom reporter #10

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading