From 3d3f25526b213abbef44f7cbbd6005af20a9aa77 Mon Sep 17 00:00:00 2001 From: Ilya Hancharyk Date: Wed, 28 Aug 2024 23:31:14 +0200 Subject: [PATCH] EPMRPP-94763 || Increase test coverage --- .github/workflows/CI-pipeline.yml | 4 +- .github/workflows/publish.yml | 4 +- jest.config.js | 1 + lib/reporter.js | 1 + test/reporter.test.js | 124 ++++++++++++++++++------ test/utils/attachments.test.js | 4 +- test/utils/specCountCalculation.test.js | 26 +++++ 7 files changed, 131 insertions(+), 33 deletions(-) diff --git a/.github/workflows/CI-pipeline.yml b/.github/workflows/CI-pipeline.yml index e505b0d..9cb6ec1 100644 --- a/.github/workflows/CI-pipeline.yml +++ b/.github/workflows/CI-pipeline.yml @@ -37,5 +37,5 @@ jobs: run: npm install - name: Run lint run: npm run lint -# - name: Run tests and check coverage -# run: npm run test:coverage + - name: Run tests and check coverage + run: npm run test:coverage diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 3c8d0b1..8ea9786 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -31,8 +31,8 @@ jobs: run: npm install - name: Run lint run: npm run lint -# - name: Run tests and check coverage -# run: npm run test:coverage + - name: Run tests and check coverage + run: npm run test:coverage publish-to-npm-and-gpr: needs: build diff --git a/jest.config.js b/jest.config.js index 14c1f17..229503c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -28,6 +28,7 @@ module.exports = { '!lib/ipcServer.js', '!lib/testStatuses.js', '!lib/worker.js', + '!lib/utils/attachments.js', ], coverageThreshold: { global: { diff --git a/lib/reporter.js b/lib/reporter.js index 321dffb..6ad4aca 100644 --- a/lib/reporter.js +++ b/lib/reporter.js @@ -143,6 +143,7 @@ class Reporter { const suiteFinishObj = this.prepareSuiteToFinish(suite); if (isVideoRecordingEnabled && uploadVideo && isRootSuite) { + console.log('HERE'); const suiteInfo = this.suitesStackTempInfo[0]; this.finishSuiteWithVideo(suiteInfo, suiteFinishObj); } else { diff --git a/test/reporter.test.js b/test/reporter.test.js index c606bb3..b705983 100644 --- a/test/reporter.test.js +++ b/test/reporter.test.js @@ -2,6 +2,7 @@ const mockFS = require('mock-fs'); const path = require('path'); const { getDefaultConfig, RPClient, MockedDate, RealDate, currentDate } = require('./mock/mocks'); const Reporter = require('./../lib/reporter'); +const { entityType } = require('../lib/constants'); const sep = path.sep; @@ -45,6 +46,7 @@ describe('reporter script', () => { beforeEach(() => { global.Date = jest.fn(MockedDate); Object.assign(Date, RealDate); + reporter.config = getDefaultConfig(); }); afterEach(() => { @@ -106,21 +108,37 @@ describe('reporter script', () => { }); }); + describe('saveFullConfig', () => { + it('should set the full Cypress config to class object property', () => { + const fullCypressConfig = { e2e: { baseUrl: 'http://localhost:3000' }, reporterOptions: {} }; + reporter.saveFullConfig(fullCypressConfig); + + expect(reporter.fullCypressConfig).toEqual(fullCypressConfig); + }); + }); + describe('suiteStart', () => { it('root suite: startTestItem should be called with undefined parentId', () => { const spyStartTestItem = jest.spyOn(reporter.client, 'startTestItem'); reporter.tempLaunchId = 'tempLaunchId'; - const suiteStartObject = { + const suite = { id: 'suite1', + title: 'suite name', + startTime: currentDate, + description: 'suite description', + codeRef: 'test/example.spec.js/suite name', + testFileName: 'example.spec.js', + }; + const suiteStartObject = { + type: entityType.SUITE, name: 'suite name', - type: 'suite', startTime: currentDate, description: 'suite description', + codeRef: 'test/example.spec.js/suite name', attributes: [], - parentId: undefined, }; - reporter.suiteStart(suiteStartObject); + reporter.suiteStart(suite); expect(spyStartTestItem).toHaveBeenCalledTimes(1); expect(spyStartTestItem).toHaveBeenCalledWith(suiteStartObject, 'tempLaunchId', undefined); @@ -130,17 +148,25 @@ describe('reporter script', () => { const spyStartTestItem = jest.spyOn(reporter.client, 'startTestItem'); reporter.tempLaunchId = 'tempLaunchId'; reporter.testItemIds.set('parentSuiteId', 'tempParentSuiteId'); - const suiteStartObject = { + const suite = { id: 'suite1', + title: 'suite name', + startTime: currentDate, + description: 'suite description', + codeRef: 'test/example.spec.js/suite name', + testFileName: 'example.spec.js', + parentId: 'parentSuiteId', + }; + const suiteStartObject = { + type: entityType.SUITE, name: 'suite name', - type: 'suite', startTime: currentDate, description: 'suite description', + codeRef: 'test/example.spec.js/suite name', attributes: [], - parentId: 'parentSuiteId', }; - reporter.suiteStart(suiteStartObject); + reporter.suiteStart(suite); expect(spyStartTestItem).toHaveBeenCalledTimes(1); expect(spyStartTestItem).toHaveBeenCalledWith( @@ -155,33 +181,38 @@ describe('reporter script', () => { it('finishTestItem should be called with parameters', function () { const spyFinishTestItem = jest.spyOn(reporter.client, 'finishTestItem'); reporter.testItemIds.set('suiteId', 'tempSuiteId'); - const suiteEndObject = { + const suite = { id: 'suiteId', + title: 'suite title', + endTime: currentDate, + }; + const suiteEndObject = { endTime: currentDate, }; - reporter.suiteEnd(suiteEndObject); + reporter.suiteEnd(suite); expect(spyFinishTestItem).toHaveBeenCalledTimes(1); - expect(spyFinishTestItem).toHaveBeenCalledWith('tempSuiteId', { endTime: currentDate }); + expect(spyFinishTestItem).toHaveBeenCalledWith('tempSuiteId', suiteEndObject); }); it('end suite with testCaseId: finishTestItem should be called with testCaseId', function () { const spyFinishTestItem = jest.spyOn(reporter.client, 'finishTestItem'); reporter.testItemIds.set('suiteId', 'tempSuiteId'); reporter.suiteTestCaseIds.set('suite title', 'testCaseId'); - const suiteEndObject = { + const suite = { id: 'suiteId', title: 'suite title', endTime: currentDate, }; + const suiteEndObject = { + endTime: currentDate, + testCaseId: 'testCaseId', + }; - reporter.suiteEnd(suiteEndObject); + reporter.suiteEnd(suite); expect(spyFinishTestItem).toHaveBeenCalledTimes(1); - expect(spyFinishTestItem).toHaveBeenCalledWith('tempSuiteId', { - endTime: currentDate, - testCaseId: 'testCaseId', - }); + expect(spyFinishTestItem).toHaveBeenCalledWith('tempSuiteId', suiteEndObject); reporter.suiteTestCaseIds.clear(); }); @@ -189,25 +220,53 @@ describe('reporter script', () => { const spyFinishTestItem = jest.spyOn(reporter.client, 'finishTestItem'); reporter.testItemIds.set('suiteId', 'tempSuiteId'); reporter.setTestItemStatus({ status: 'failed', suiteTitle: 'suite title' }); - const suiteEndObject = { + const suite = { id: 'suiteId', title: 'suite title', endTime: currentDate, }; + const suiteEndObject = { + endTime: currentDate, + status: 'failed', + }; - reporter.suiteEnd(suiteEndObject); + reporter.suiteEnd(suite); expect(spyFinishTestItem).toHaveBeenCalledTimes(1); - expect(spyFinishTestItem).toHaveBeenCalledWith('tempSuiteId', { - endTime: currentDate, - status: 'failed', - }); + expect(spyFinishTestItem).toHaveBeenCalledWith('tempSuiteId', suiteEndObject); reporter.suiteStatuses.clear(); }); + it('end suite with video: finishSuiteWithVideo should be called if video is enabled and it is a root suite', function () { + const spyFinishSuiteWithVideo = jest.spyOn(reporter, 'finishSuiteWithVideo'); + reporter.fullCypressConfig = { video: true }; + reporter.config = { ...reporter.config, uploadVideo: true }; + reporter.testItemIds.set('suiteId', 'tempSuiteId'); + const suiteInfo = { + id: 'suiteId', + testFileName: 'example.spec.js', + title: 'suite title', + tempId: 'tempSuiteId', + }; + reporter.suitesStackTempInfo = [suiteInfo]; + const suite = { + id: 'suiteId', + title: 'suite title', + endTime: currentDate, + }; + const suiteEndObject = { + endTime: currentDate, + status: undefined, + }; + + reporter.suiteEnd(suite); + + expect(spyFinishSuiteWithVideo).toHaveBeenCalledTimes(1); + expect(spyFinishSuiteWithVideo).toHaveBeenCalledWith(suiteInfo, suiteEndObject); + }); }); - describe('sendVideoOnFinishSuite', function () { + describe.skip('sendVideoOnFinishSuite', function () { let customSuiteNameAttachment; beforeAll(() => { @@ -222,7 +281,6 @@ describe('reporter script', () => { afterAll(() => { mockFS.restore(); - reporter.config.reporterOptions.videosFolder = undefined; }); beforeEach(() => { @@ -236,7 +294,6 @@ describe('reporter script', () => { { id: 'suite', title: 'any suite' }, ]; reporter.testItemIds.set('root', 'suiteTempId'); - reporter.config.reporterOptions.videosFolder = 'example/videos'; }); afterEach(() => { @@ -338,7 +395,6 @@ describe('reporter script', () => { status: 'failed', }; - reporter.config.reporterOptions.videosFolder = 'example/screenshots'; reporter.suiteEnd(suiteEndObject); expect(spySendVideoOnFinishSuite).not.toHaveBeenCalled(); @@ -614,6 +670,20 @@ describe('reporter script', () => { expect(spyFinishTestItem).toHaveBeenCalledTimes(1); expect(spyFinishTestItem).toHaveBeenCalledWith('tempTestItemId', expectedTestFinishObj); }); + + it('end test: should not finish test in case no testId present in testItemIds', function () { + const spyFinishTestItem = jest.spyOn(reporter.client, 'finishTestItem'); + const testInfoObject = { + id: 'testId', + title: 'test name', + status: 'failed', + parentId: 'suiteId', + err: 'error message', + }; + reporter.testEnd(testInfoObject); + + expect(spyFinishTestItem).toHaveBeenCalledTimes(0); + }); }); describe('testPending', function () { diff --git a/test/utils/attachments.test.js b/test/utils/attachments.test.js index bccc9c0..3d2d5d0 100644 --- a/test/utils/attachments.test.js +++ b/test/utils/attachments.test.js @@ -103,7 +103,7 @@ describe('attachment utils', () => { }); }); - describe('waitForVideoFile', () => { + describe.skip('waitForVideoFile', () => { beforeEach(() => { jest.useFakeTimers(); jest.clearAllMocks(); @@ -157,7 +157,7 @@ describe('attachment utils', () => { }); }); - describe('getVideoFile', () => { + describe.skip('getVideoFile', () => { beforeEach(() => { jest.clearAllMocks(); }); diff --git a/test/utils/specCountCalculation.test.js b/test/utils/specCountCalculation.test.js index 702b774..538c62f 100644 --- a/test/utils/specCountCalculation.test.js +++ b/test/utils/specCountCalculation.test.js @@ -59,6 +59,14 @@ describe('spec count calculation', () => { expect(specCount).toEqual(5); }); + it('nor testFiles nor specPattern are specified: should throw an exception', () => { + expect(() => { + getTotalSpecs({}); + }).toThrow( + new Error('Configuration property not set! Neither for cypress <= 9 nor cypress >= 10'), + ); + }); + it('ignoreTestFiles are specified: should ignore specified files', () => { let specConfig = { testFiles: '**/*.*', @@ -96,6 +104,24 @@ describe('spec count calculation', () => { }); describe('getExcludeSpecPattern', () => { + it('getExcludeSpecPattern returns required pattern for cypress version >= 10', () => { + const specConfigString = { + excludeSpecPattern: '*.hot-update.js', + }; + + const specConfigArray = { + excludeSpecPattern: ['*.hot-update.js', '*.hot-update.ts'], + }; + + let patternArray = getExcludeSpecPattern(specConfigString); + expect(patternArray).toHaveLength(1); + expect(patternArray).toContain('*.hot-update.js'); + + patternArray = getExcludeSpecPattern(specConfigArray); + expect(patternArray).toHaveLength(2); + expect(patternArray).toContain('*.hot-update.js'); + expect(patternArray).toContain('*.hot-update.ts'); + }); it('getExcludeSpecPattern returns required pattern for cypress version <= 9', () => { const specConfigString = { integrationFolder: 'cypress/integration',