diff --git a/src/custom-reporter.js b/src/custom-reporter.js new file mode 100644 index 0000000..02e6601 --- /dev/null +++ b/src/custom-reporter.js @@ -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; diff --git a/src/modules/test_runs/testRun.controller.ts b/src/modules/test_runs/testRun.controller.ts index f250e8f..c279793 100644 --- a/src/modules/test_runs/testRun.controller.ts +++ b/src/modules/test_runs/testRun.controller.ts @@ -33,6 +33,12 @@ 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, ...{ @@ -40,20 +46,34 @@ export class TestRunController { globals: JSON.stringify({ SESSION_ID: sessionId, }), + reporters: [ + 'default', + [ + '/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 }); } } diff --git a/src/modules/test_runs/testRun.dto.ts b/src/modules/test_runs/testRun.dto.ts index e673ae5..00d312c 100644 --- a/src/modules/test_runs/testRun.dto.ts +++ b/src/modules/test_runs/testRun.dto.ts @@ -22,7 +22,7 @@ export class CreateTestRunDto { description: 'Run specific test suite', type: json, }) - testResults: jsonbType; + testResults?: jsonbType; } export class InitiateTestRunDto { diff --git a/src/modules/test_runs/testRun.entity.ts b/src/modules/test_runs/testRun.entity.ts index 6d71d0b..dd87a95 100644 --- a/src/modules/test_runs/testRun.entity.ts +++ b/src/modules/test_runs/testRun.entity.ts @@ -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; diff --git a/src/modules/test_runs/testRun.service.ts b/src/modules/test_runs/testRun.service.ts index be1273f..6cf81a3 100644 --- a/src/modules/test_runs/testRun.service.ts +++ b/src/modules/test_runs/testRun.service.ts @@ -16,6 +16,10 @@ export class TestRunService { return this.testRunRepository.save(testRun); } + async update(testRun: TestRun): Promise { + return this.testRunRepository.save(testRun); + } + async findAll(params: any): Promise { return this.testRunRepository.find(params); }