|
| 1 | +/* |
| 2 | + * SonarQube JavaScript Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +import { describe, it, type Mock } from 'node:test'; |
| 18 | +import { ProgressReport } from '../../src/helpers/progress-report.js'; |
| 19 | +import { expect } from 'expect'; |
| 20 | + |
| 21 | +describe('progress-report', () => { |
| 22 | + it('should report progress', async ({ mock }) => { |
| 23 | + mock.timers.enable({ apis: ['setInterval'] }); |
| 24 | + mock.method(console, 'log'); |
| 25 | + const progressReport = new ProgressReport(5); |
| 26 | + const consoleLogMock = (console.log as Mock<typeof console.log>).mock; |
| 27 | + expect(consoleLogMock.calls[0].arguments[0]).toEqual(`5 source files to be analyzed`); |
| 28 | + expect(consoleLogMock.callCount()).toEqual(1); |
| 29 | + // we pass next file |
| 30 | + progressReport.nextFile('file1.ts'); |
| 31 | + // passing next file should not trigger a console.log |
| 32 | + expect(consoleLogMock.callCount()).toEqual(1); |
| 33 | + |
| 34 | + // after 10 seconds have passed, there is a new log |
| 35 | + mock.timers.tick(10000); |
| 36 | + expect(consoleLogMock.calls[1].arguments[0]).toEqual( |
| 37 | + `1/5 file analyzed, current file: file1.ts`, |
| 38 | + ); |
| 39 | + expect(consoleLogMock.callCount()).toEqual(2); |
| 40 | + |
| 41 | + // another 10 seconds, we should log next file |
| 42 | + mock.timers.tick(10000); |
| 43 | + expect(consoleLogMock.calls[2].arguments[0]).toEqual( |
| 44 | + `1/5 file analyzed, current file: file1.ts`, |
| 45 | + ); |
| 46 | + expect(consoleLogMock.callCount()).toEqual(3); |
| 47 | + |
| 48 | + progressReport.stop(); |
| 49 | + // another 10 seconds, nothing else has been logged because we have stopped the reporter |
| 50 | + mock.timers.tick(10000); |
| 51 | + expect(consoleLogMock.calls[3].arguments[0]).toEqual(`5/5 source files have been analyzed`); |
| 52 | + expect(consoleLogMock.callCount()).toEqual(4); |
| 53 | + }); |
| 54 | +}); |
0 commit comments