From 8ec788b0bced08943bdeef3d636a8d0e87417056 Mon Sep 17 00:00:00 2001 From: nvim Date: Mon, 9 Sep 2024 10:57:14 +0200 Subject: [PATCH 1/4] WIP: Add basic custom reporter Ref: https://github.com/beda-software/fhir-client-testing-tool/issues/5 --- package.json | 1 + src/custom-reporter.js | 22 +++++++++++++++++ src/modules/test_runs/testRun.controller.ts | 2 ++ yarn.lock | 27 +++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 src/custom-reporter.js diff --git a/package.json b/package.json index fba7bfd..783ba3f 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "flatted": "^3.3.1", "http-proxy": "^1.18.1", "http-proxy-middleware": "^3.0.0", + "jest-progress-bar-reporter": "^1.0.25", "pg": "^8.12.0", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1", diff --git a/src/custom-reporter.js b/src/custom-reporter.js new file mode 100644 index 0000000..33f2ffa --- /dev/null +++ b/src/custom-reporter.js @@ -0,0 +1,22 @@ +class CustomReporter { + constructor(globalConfig, options) { + this._globalConfig = globalConfig; + this._options = options; + this.totalTests = 0; + this.testCount = 0; + } + + onRunStart(test, testSuiteResults) { + testSuiteResults.testResults.forEach((suite) => { + this.totalTests += suite.numPassingTests + suite.numFailingTests + suite.numPendingTests; + }); + console.log(`Total tests found: ${this.totalTests}`); + } + + onTestResult(test, testResult, aggregatedResult) { + this.testCount += testResult.numPassingTests + testResult.numFailingTests + testResult.numPendingTests; + console.log(`Progress: ${this.testCount}/${this.totalTests}`); + } +} + +module.exports = CustomReporter; diff --git a/src/modules/test_runs/testRun.controller.ts b/src/modules/test_runs/testRun.controller.ts index f250e8f..71e6690 100644 --- a/src/modules/test_runs/testRun.controller.ts +++ b/src/modules/test_runs/testRun.controller.ts @@ -40,6 +40,8 @@ export class TestRunController { globals: JSON.stringify({ SESSION_ID: sessionId, }), + reporters: ['default', '/src/custom-reporter.js'], + // reporters: ['default', 'jest-progress-bar-reporter'], }, ...optionsWithTest, }; diff --git a/yarn.lock b/yarn.lock index ea0c13f..9a1e78b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1712,6 +1712,14 @@ caniuse-lite@^1.0.30001646: resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001649.tgz#3ec700309ca0da2b0d3d5fb03c411b191761c992" integrity sha512-fJegqZZ0ZX8HOWr6rcafGr72+xcgJKI9oWfDW5DrD7ExUtgZC7a7R7ZYmZqplh7XDocFdGeIFn7roAxhOeYrPQ== +chalk@4.1.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.1.tgz#c80b3fab28bf6371e6863325eee67e618b77e6ad" + integrity sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg== + dependencies: + ansi-styles "^4.1.0" + supports-color "^7.1.0" + chalk@4.1.2, chalk@^4.0.0, chalk@^4.0.2, chalk@^4.1.0, chalk@^4.1.1, chalk@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" @@ -3312,6 +3320,15 @@ jest-pnp-resolver@^1.2.2: resolved "https://registry.yarnpkg.com/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz#930b1546164d4ad5937d5540e711d4d38d4cad2e" integrity sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w== +jest-progress-bar-reporter@^1.0.25: + version "1.0.25" + resolved "https://registry.yarnpkg.com/jest-progress-bar-reporter/-/jest-progress-bar-reporter-1.0.25.tgz#c292431023bd86947ead8cceba9e8f665f21a092" + integrity sha512-iFWtsl0j+OeHeMCu5fXL8pSN3h06NQlG/UPpcuO2PP2C+hr81K397+Mh34rU3GgA9kQga6ctu7gfOEOgUlKFug== + dependencies: + chalk "4.1.1" + moment "^2.29.4" + progress "2.0.3" + jest-regex-util@^29.6.3: version "29.6.3" resolved "https://registry.yarnpkg.com/jest-regex-util/-/jest-regex-util-29.6.3.tgz#4a556d9c776af68e1c5f48194f4d0327d24e8a52" @@ -3782,6 +3799,11 @@ mkdirp@^2.1.3: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-2.1.6.tgz#964fbcb12b2d8c5d6fbc62a963ac95a273e2cc19" integrity sha512-+hEnITedc8LAtIP9u3HJDFIdcLV2vXP33sqLLIzkv1Db1zO/1OxbvYf0Y1OC/S/Qo5dxHXepofhmxL02PsKe+A== +moment@^2.29.4: + version "2.30.1" + resolved "https://registry.yarnpkg.com/moment/-/moment-2.30.1.tgz#f8c91c07b7a786e30c59926df530b4eac96974ae" + integrity sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how== + ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" @@ -4209,6 +4231,11 @@ process-nextick-args@~2.0.0: resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2" integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag== +progress@2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" + integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== + prompts@^2.0.1: version "2.4.2" resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069" From 8a143868e4c30e3fb5ed2b812db51ec18adfee80 Mon Sep 17 00:00:00 2001 From: nvim Date: Mon, 9 Sep 2024 14:39:59 +0200 Subject: [PATCH 2/4] Report about progress via console log Ref: https://github.com/beda-software/fhir-client-testing-tool/issues/5 --- src/custom-reporter.js | 18 ++++++++---------- src/modules/test_runs/testRun.controller.ts | 3 +-- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/custom-reporter.js b/src/custom-reporter.js index 33f2ffa..1164e69 100644 --- a/src/custom-reporter.js +++ b/src/custom-reporter.js @@ -2,20 +2,18 @@ class CustomReporter { constructor(globalConfig, options) { this._globalConfig = globalConfig; this._options = options; - this.totalTests = 0; - this.testCount = 0; + this.totalSuites = 0; + this.suitesCount = 0; } - onRunStart(test, testSuiteResults) { - testSuiteResults.testResults.forEach((suite) => { - this.totalTests += suite.numPassingTests + suite.numFailingTests + suite.numPendingTests; - }); - console.log(`Total tests found: ${this.totalTests}`); + onRunStart(test) { + this.totalSuites = test.numTotalTestSuites; + console.log('Total test suites:', this.totalSuites); } - onTestResult(test, testResult, aggregatedResult) { - this.testCount += testResult.numPassingTests + testResult.numFailingTests + testResult.numPendingTests; - console.log(`Progress: ${this.testCount}/${this.totalTests}`); + onTestResult() { + this.suitesCount += 1; + console.log(`Progress: ${this.suitesCount}/${this.totalSuites}`); } } diff --git a/src/modules/test_runs/testRun.controller.ts b/src/modules/test_runs/testRun.controller.ts index 71e6690..8e03ac0 100644 --- a/src/modules/test_runs/testRun.controller.ts +++ b/src/modules/test_runs/testRun.controller.ts @@ -40,8 +40,7 @@ export class TestRunController { globals: JSON.stringify({ SESSION_ID: sessionId, }), - reporters: ['default', '/src/custom-reporter.js'], - // reporters: ['default', 'jest-progress-bar-reporter'], + reporters: ['default', ['/src/custom-reporter.js', { SESSION_ID: sessionId }]], }, ...optionsWithTest, }; From be3807b14c8f7f682d78f7ce90ac4cf5f6676815 Mon Sep 17 00:00:00 2001 From: nvim Date: Mon, 9 Sep 2024 16:51:27 +0200 Subject: [PATCH 3/4] Update progress in real time --- src/custom-reporter.js | 21 ++++++++++--- src/modules/test_runs/testRun.controller.ts | 33 ++++++++++++++++----- src/modules/test_runs/testRun.dto.ts | 2 +- src/modules/test_runs/testRun.entity.ts | 15 ++++++++++ src/modules/test_runs/testRun.service.ts | 4 +++ 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/custom-reporter.js b/src/custom-reporter.js index 1164e69..02e6601 100644 --- a/src/custom-reporter.js +++ b/src/custom-reporter.js @@ -4,16 +4,29 @@ class CustomReporter { this._options = options; this.totalSuites = 0; this.suitesCount = 0; + this.testRunId = this._options.TEST_RUN_ID; + this.testRunService = this._options.TEST_RUN_SERVICE; } - onRunStart(test) { + async onRunStart(test) { this.totalSuites = test.numTotalTestSuites; - console.log('Total test suites:', this.totalSuites); + const testRun = await this.testRunService.findOne(this.testRunId); + await this.testRunService.update({ ...testRun, ...{ suiteTotal: this.totalSuites, status: 'running' } }); } - onTestResult() { + onTestStart() { + // Just for the info; + } + + async onTestResult() { this.suitesCount += 1; - console.log(`Progress: ${this.suitesCount}/${this.totalSuites}`); + 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' } }); } } diff --git a/src/modules/test_runs/testRun.controller.ts b/src/modules/test_runs/testRun.controller.ts index 8e03ac0..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,21 +46,34 @@ export class TestRunController { globals: JSON.stringify({ SESSION_ID: sessionId, }), - reporters: ['default', ['/src/custom-reporter.js', { 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); } From 5c4f297d6463c2025271cdee5052cc9638edf330 Mon Sep 17 00:00:00 2001 From: nvim Date: Mon, 9 Sep 2024 16:55:46 +0200 Subject: [PATCH 4/4] Remove external reporter Ref: https://github.com/beda-software/fhir-client-testing-tool/issues/5 --- package.json | 1 - yarn.lock | 27 --------------------------- 2 files changed, 28 deletions(-) diff --git a/package.json b/package.json index 783ba3f..fba7bfd 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "flatted": "^3.3.1", "http-proxy": "^1.18.1", "http-proxy-middleware": "^3.0.0", - "jest-progress-bar-reporter": "^1.0.25", "pg": "^8.12.0", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1", diff --git a/yarn.lock b/yarn.lock index 9a1e78b..ea0c13f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1712,14 +1712,6 @@ caniuse-lite@^1.0.30001646: resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001649.tgz#3ec700309ca0da2b0d3d5fb03c411b191761c992" integrity sha512-fJegqZZ0ZX8HOWr6rcafGr72+xcgJKI9oWfDW5DrD7ExUtgZC7a7R7ZYmZqplh7XDocFdGeIFn7roAxhOeYrPQ== -chalk@4.1.1: - version "4.1.1" - resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.1.tgz#c80b3fab28bf6371e6863325eee67e618b77e6ad" - integrity sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg== - dependencies: - ansi-styles "^4.1.0" - supports-color "^7.1.0" - chalk@4.1.2, chalk@^4.0.0, chalk@^4.0.2, chalk@^4.1.0, chalk@^4.1.1, chalk@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" @@ -3320,15 +3312,6 @@ jest-pnp-resolver@^1.2.2: resolved "https://registry.yarnpkg.com/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz#930b1546164d4ad5937d5540e711d4d38d4cad2e" integrity sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w== -jest-progress-bar-reporter@^1.0.25: - version "1.0.25" - resolved "https://registry.yarnpkg.com/jest-progress-bar-reporter/-/jest-progress-bar-reporter-1.0.25.tgz#c292431023bd86947ead8cceba9e8f665f21a092" - integrity sha512-iFWtsl0j+OeHeMCu5fXL8pSN3h06NQlG/UPpcuO2PP2C+hr81K397+Mh34rU3GgA9kQga6ctu7gfOEOgUlKFug== - dependencies: - chalk "4.1.1" - moment "^2.29.4" - progress "2.0.3" - jest-regex-util@^29.6.3: version "29.6.3" resolved "https://registry.yarnpkg.com/jest-regex-util/-/jest-regex-util-29.6.3.tgz#4a556d9c776af68e1c5f48194f4d0327d24e8a52" @@ -3799,11 +3782,6 @@ mkdirp@^2.1.3: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-2.1.6.tgz#964fbcb12b2d8c5d6fbc62a963ac95a273e2cc19" integrity sha512-+hEnITedc8LAtIP9u3HJDFIdcLV2vXP33sqLLIzkv1Db1zO/1OxbvYf0Y1OC/S/Qo5dxHXepofhmxL02PsKe+A== -moment@^2.29.4: - version "2.30.1" - resolved "https://registry.yarnpkg.com/moment/-/moment-2.30.1.tgz#f8c91c07b7a786e30c59926df530b4eac96974ae" - integrity sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how== - ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" @@ -4231,11 +4209,6 @@ process-nextick-args@~2.0.0: resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2" integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag== -progress@2.0.3: - version "2.0.3" - resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" - integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== - prompts@^2.0.1: version "2.4.2" resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"