From 62c2f19109e199d561b3c7347f3145c4179f28a8 Mon Sep 17 00:00:00 2001 From: uid11 Date: Tue, 30 Jul 2024 22:19:48 +0300 Subject: [PATCH] FI-1237 fix: state of "full mocks" after first retry --- .../configurator/mapLogPayloadInConsole.ts | 2 ++ package.json | 3 ++- scripts/checkBranch.ts | 14 +++++++++++ src/types/retries.ts | 2 -- src/utils/fullMocks/getShouldApplyMocks.ts | 11 --------- src/utils/fullMocks/index.ts | 2 -- src/utils/globalState/index.ts | 2 -- src/utils/globalState/visitedTestNamesHash.ts | 23 ------------------- src/utils/pack/runPackWithArgs.ts | 3 +-- src/utils/report/writeHtmlReport.ts | 8 +++++-- src/utils/retry/processRetry.ts | 10 ++------ src/utils/retry/runPackWithRetries.ts | 1 - .../retry/updateRetriesStateAfterRetry.ts | 6 ----- src/utils/test/getRunTest.ts | 4 +++- src/utils/test/runTestFn.ts | 22 ++++++++++++------ src/utils/tests/runTests.ts | 11 +++------ src/utils/tests/stripExtraLogs.ts | 2 +- 17 files changed, 49 insertions(+), 77 deletions(-) create mode 100644 scripts/checkBranch.ts delete mode 100644 src/utils/fullMocks/getShouldApplyMocks.ts delete mode 100644 src/utils/globalState/index.ts delete mode 100644 src/utils/globalState/visitedTestNamesHash.ts diff --git a/autotests/configurator/mapLogPayloadInConsole.ts b/autotests/configurator/mapLogPayloadInConsole.ts index b9bbce55..e1ba510d 100644 --- a/autotests/configurator/mapLogPayloadInConsole.ts +++ b/autotests/configurator/mapLogPayloadInConsole.ts @@ -18,6 +18,8 @@ export const mapLogPayloadInConsole: MapLogPayloadInConsole = (message, payload) } if ( + message.startsWith('✓') || + message.startsWith('✘') || message.startsWith('Caught an error when running tests in retry') || message.startsWith('Warning from TestCafe:') || message.startsWith('Usage:') diff --git a/package.json b/package.json index 20dab3cf..b3cc1f8b 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,8 @@ "scripts": { "asserts": "assert-modules-support-case-insensitive-fs ./autotests ./src && assert-package-lock-is-consistent", "precheck:all": "npm run asserts && npm run clear:lint:cache && npm run build", - "check:all": "npm audit && npm run parallel lint test", + "check:all": "npm audit && npm run parallel check:branch lint test", + "check:branch": "node ./build/checkBranch.js", "clear:lint:cache": "rm -f ./build/tsconfig.tsbuildinfo ./node_modules/.cache/lint-*", "lint": "npm run parallel lint:es lint:prettier lint:types", "lint:es": "eslint --cache --cache-location=./node_modules/.cache/lint-es --cache-strategy=content --ext=.ts --max-warnings=0 --report-unused-disable-directives .", diff --git a/scripts/checkBranch.ts b/scripts/checkBranch.ts new file mode 100644 index 00000000..6be50d4d --- /dev/null +++ b/scripts/checkBranch.ts @@ -0,0 +1,14 @@ +/** + * @file Checks that current git branch is `main`. + */ + +import {execFileSync} from 'node:child_process'; + +const currentBranch = execFileSync('git', ['branch', '--show-current'], {encoding: 'utf8'}).trim(); + +if (currentBranch !== 'main') { + throw new Error(`Current branch in not "main": "${currentBranch}"`); +} + +// eslint-disable-next-line no-console +console.log('[OK] Current branch is "main"'); diff --git a/src/types/retries.ts b/src/types/retries.ts index c88fbe2f..4cd0eaa2 100644 --- a/src/types/retries.ts +++ b/src/types/retries.ts @@ -14,7 +14,6 @@ export type RetriesState = Readonly<{ retryIndex: number; startLastRetryTimeInMs: UtcTimeInMs; successfulTestRunNamesHash: Record; - visitedTestNamesHash: Record; visitedTestRunEventsFileName: readonly string[]; }>; @@ -25,7 +24,6 @@ export type RetriesState = Readonly<{ export type RunRetryOptions = Readonly<{ runLabel: RunLabel; successfulTestRunNamesHash: VisitedTestNamesHash; - visitedTestNamesHash: VisitedTestNamesHash; }>; /** diff --git a/src/utils/fullMocks/getShouldApplyMocks.ts b/src/utils/fullMocks/getShouldApplyMocks.ts deleted file mode 100644 index 6624f8ab..00000000 --- a/src/utils/fullMocks/getShouldApplyMocks.ts +++ /dev/null @@ -1,11 +0,0 @@ -import {getVisitedTestNamesHash} from '../globalState'; - -/** - * Returns `true`, if we should apply full mocks for test (by test name). - * @internal - */ -export const getShouldApplyMocks = (testName: string): boolean => { - const visitedTestNamesHash = getVisitedTestNamesHash(); - - return !visitedTestNamesHash?.[testName]; -}; diff --git a/src/utils/fullMocks/index.ts b/src/utils/fullMocks/index.ts index f06274b2..ff410fc1 100644 --- a/src/utils/fullMocks/index.ts +++ b/src/utils/fullMocks/index.ts @@ -3,6 +3,4 @@ export {enableFullMocks} from './enableFullMocks'; /** @internal */ export {getResponseFromFullMocks} from './getResponseFromFullMocks'; /** @internal */ -export {getShouldApplyMocks} from './getShouldApplyMocks'; -/** @internal */ export {writeResponseToFullMocks} from './writeResponseToFullMocks'; diff --git a/src/utils/globalState/index.ts b/src/utils/globalState/index.ts deleted file mode 100644 index f35ec4bd..00000000 --- a/src/utils/globalState/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -/** @internal */ -export {getVisitedTestNamesHash, setVisitedTestNamesHash} from './visitedTestNamesHash'; diff --git a/src/utils/globalState/visitedTestNamesHash.ts b/src/utils/globalState/visitedTestNamesHash.ts deleted file mode 100644 index 1cafbbc1..00000000 --- a/src/utils/globalState/visitedTestNamesHash.ts +++ /dev/null @@ -1,23 +0,0 @@ -import {assertValueIsUndefined} from '../asserts'; - -import type {VisitedTestNamesHash} from '../../types/internal'; - -let visitedTestNamesHash: VisitedTestNamesHash | undefined; - -/** - * Get hash of names of already visited tests (maybe, in previous retries). - * @internal - */ -export const getVisitedTestNamesHash = (): VisitedTestNamesHash | undefined => visitedTestNamesHash; - -/** - * Set hash of names of already visited tests (can only be called once). - * @internal - */ -export const setVisitedTestNamesHash = (newVisitedTestNamesHash: VisitedTestNamesHash): void => { - assertValueIsUndefined(visitedTestNamesHash, 'visitedTestNamesHash is not defined', { - newVisitedTestNamesHash, - }); - - visitedTestNamesHash = newVisitedTestNamesHash; -}; diff --git a/src/utils/pack/runPackWithArgs.ts b/src/utils/pack/runPackWithArgs.ts index 733afdce..99a71b92 100644 --- a/src/utils/pack/runPackWithArgs.ts +++ b/src/utils/pack/runPackWithArgs.ts @@ -16,9 +16,8 @@ export const runPackWithArgs = async (): Promise => { const runLabel = createRunLabel({concurrency, maxRetriesCount: 1, retryIndex: 1}); const successfulTestRunNamesHash = Object.create(null) as VisitedTestNamesHash; - const visitedTestNamesHash = Object.create(null) as VisitedTestNamesHash; - await runTests({runLabel, successfulTestRunNamesHash, visitedTestNamesHash}); + await runTests({runLabel, successfulTestRunNamesHash}); endE2ed(EndE2edReason.LocalRunEnded); }; diff --git a/src/utils/report/writeHtmlReport.ts b/src/utils/report/writeHtmlReport.ts index d1dd7576..d23c92d1 100644 --- a/src/utils/report/writeHtmlReport.ts +++ b/src/utils/report/writeHtmlReport.ts @@ -11,6 +11,8 @@ import {renderReportToHtml} from './render'; import type {FilePathFromRoot, ReportData, UtcTimeInMs} from '../../types/internal'; +const bytesInMb = 1_048_576; + /** * Writes HTML report (`report.html` file) with test runs results. * @internal @@ -27,11 +29,13 @@ export const writeHtmlReport = async (reportData: ReportData): Promise => await writeFile(reportFilePath, String(reportHtml)); - const reportFileSize = await getFileSize(reportFilePath); + const reportFileSizeInBytes = await getFileSize(reportFilePath); + + const reportFileSizeInMb = (reportFileSizeInBytes / bytesInMb).toFixed(2); const durationWithUnits = getDurationWithUnits(Date.now() - startTimeInMs); generalLog( - `HTML report was written (${reportFileSize} bytes) to "${reportFilePath}" in ${durationWithUnits}`, + `HTML report was written (${reportFileSizeInMb} Mb) to "${reportFilePath}" in ${durationWithUnits}`, ); }; diff --git a/src/utils/retry/processRetry.ts b/src/utils/retry/processRetry.ts index 3a77f6cc..fb1057a8 100644 --- a/src/utils/retry/processRetry.ts +++ b/src/utils/retry/processRetry.ts @@ -14,13 +14,7 @@ import type {RetriesState, UtcTimeInMs} from '../../types/internal'; * @internal */ export const processRetry = async (retriesState: RetriesState): Promise => { - const { - concurrency, - maxRetriesCount, - retryIndex, - successfulTestRunNamesHash, - visitedTestNamesHash, - } = retriesState; + const {concurrency, maxRetriesCount, retryIndex, successfulTestRunNamesHash} = retriesState; const runLabel = createRunLabel({concurrency, maxRetriesCount, retryIndex}); const startLastRetryTimeInMs = Date.now() as UtcTimeInMs; @@ -35,7 +29,7 @@ export const processRetry = async (retriesState: RetriesState): Promise => try { await writeLogsToFile(); - await runRetry({runLabel, successfulTestRunNamesHash, visitedTestNamesHash}); + await runRetry({runLabel, successfulTestRunNamesHash}); setReadonlyProperty(retriesState, 'isLastRetrySuccessful', true); } catch (error) { diff --git a/src/utils/retry/runPackWithRetries.ts b/src/utils/retry/runPackWithRetries.ts index 6a2cce33..4fc11afb 100644 --- a/src/utils/retry/runPackWithRetries.ts +++ b/src/utils/retry/runPackWithRetries.ts @@ -21,7 +21,6 @@ export const runPackWithRetries = async (): Promise => { retryIndex: 1, startLastRetryTimeInMs: 0 as UtcTimeInMs, successfulTestRunNamesHash: Object.create(null) as {}, - visitedTestNamesHash: Object.create(null) as {}, visitedTestRunEventsFileName: [], }; diff --git a/src/utils/retry/updateRetriesStateAfterRetry.ts b/src/utils/retry/updateRetriesStateAfterRetry.ts index bab2501d..91348977 100644 --- a/src/utils/retry/updateRetriesStateAfterRetry.ts +++ b/src/utils/retry/updateRetriesStateAfterRetry.ts @@ -21,7 +21,6 @@ export const updateRetriesStateAfterRetry = async (retriesState: RetriesState): maxRetriesCount, retryIndex, successfulTestRunNamesHash, - visitedTestNamesHash, visitedTestRunEventsFileName, } = retriesState; const { @@ -44,7 +43,6 @@ export const updateRetriesStateAfterRetry = async (retriesState: RetriesState): ); successfulTestRunNamesHash[name] = true; - visitedTestNamesHash[name] = true; } for (const {runId} of newFullTestRuns) { @@ -80,10 +78,6 @@ export const updateRetriesStateAfterRetry = async (retriesState: RetriesState): const failedTestNamesInLastRetry = failedNewFullTestRuns.map(({name}) => name); - for (const name of failedTestNamesInLastRetry) { - visitedTestNamesHash[name] = true; - } - const retriesStateUpdate: Partial> = { concurrency: concurrencyForNextRetry, failedTestNamesInLastRetry, diff --git a/src/utils/test/getRunTest.ts b/src/utils/test/getRunTest.ts index 67f079a5..2aa26c08 100644 --- a/src/utils/test/getRunTest.ts +++ b/src/utils/test/getRunTest.ts @@ -49,7 +49,9 @@ export const getRunTest = beforeTest({retry, runId, testFn: test.testFn, testStaticOptions}); - await runTestFn(runId, {context, page, request}, testStaticOptions); + const testController = {context, page, request}; + + await runTestFn({retry, runId, testController, testStaticOptions}); } catch (error) { hasRunError = true; unknownRunError = error; diff --git a/src/utils/test/runTestFn.ts b/src/utils/test/runTestFn.ts index 10a1a216..01657c0f 100644 --- a/src/utils/test/runTestFn.ts +++ b/src/utils/test/runTestFn.ts @@ -3,7 +3,7 @@ import {getTestTimeout} from '../../context/testTimeout'; import {getFullPackConfig} from '../config'; import {getTestRunEvent} from '../events'; -import {enableFullMocks, getShouldApplyMocks} from '../fullMocks'; +import {enableFullMocks} from '../fullMocks'; import {getPromiseWithResolveAndReject} from '../promise'; import type {PlaywrightTestArgs} from '@playwright/test'; @@ -12,15 +12,23 @@ import type {RunId, TestStaticOptions} from '../../types/internal'; const delayForTestRunPromiseResolutionAfterTestTimeoutInMs = 100; +type Options = Readonly<{ + retry: number; + runId: RunId; + testController: PlaywrightTestArgs; + testStaticOptions: TestStaticOptions; +}>; + /** * Runs test function with reject in test run event. * @internal */ -export const runTestFn = async ( - runId: RunId, - testController: PlaywrightTestArgs, - testStaticOptions: TestStaticOptions, -): Promise => { +export const runTestFn = async ({ + runId, + retry, + testController, + testStaticOptions, +}: Options): Promise => { const testRunEvent = getTestRunEvent(runId); const testTimeout = getTestTimeout(); @@ -37,7 +45,7 @@ export const runTestFn = async ( const {fullMocks} = getFullPackConfig(); if (fullMocks?.filterTests(testStaticOptions)) { - const shouldApplyMocks = getShouldApplyMocks(testStaticOptions.name); + const shouldApplyMocks = retry === 1; await enableFullMocks(fullMocks, shouldApplyMocks, testStaticOptions.filePath); } diff --git a/src/utils/tests/runTests.ts b/src/utils/tests/runTests.ts index d72ee2c8..10c20ff3 100644 --- a/src/utils/tests/runTests.ts +++ b/src/utils/tests/runTests.ts @@ -6,7 +6,6 @@ import {getFullPackConfig} from '../config'; import {getRunLabel, setRunLabel} from '../environment'; import {E2edError} from '../error'; import {generalLog} from '../generalLog'; -import {setVisitedTestNamesHash} from '../globalState'; import {startResourceUsageReading} from '../resourceUsage'; import {beforeRunFirstTest} from './beforeRunFirstTest'; @@ -21,12 +20,8 @@ const playwrightCliPath = require.resolve('@playwright/test').replace('index.js' * Rejects, if there are some failed tests. * @internal */ -export const runTests = async ({ - runLabel, - visitedTestNamesHash, -}: RunRetryOptions): Promise => { +export const runTests = async ({runLabel}: RunRetryOptions): Promise => { setRunLabel(runLabel); - setVisitedTestNamesHash(visitedTestNamesHash); try { const {browserInitTimeout, enableLiveMode, resourceUsageReadingInternal} = getFullPackConfig(); @@ -70,10 +65,10 @@ export const runTests = async ({ beforeRunFirstTest(); } - const playwrightProcess = fork(playwrightCliPath, playwrightArgs); + const playwrightProcess = fork(playwrightCliPath, playwrightArgs, {stdio: 'pipe'}); playwrightProcess.stdout?.on('data', (data) => { - const stringData = stripExtraLogs(String(data).trim()); + const stringData = stripExtraLogs(String(data).trim()).trim(); if (stringData !== '') { if (stringData.startsWith('[e2ed]')) { diff --git a/src/utils/tests/stripExtraLogs.ts b/src/utils/tests/stripExtraLogs.ts index f41129da..9b9d748e 100644 --- a/src/utils/tests/stripExtraLogs.ts +++ b/src/utils/tests/stripExtraLogs.ts @@ -13,7 +13,7 @@ export const stripExtraLogs = (text: string): string => { const endIndex = line.lastIndexOf('›'); if (startIndex >= 0) { - lines[index] = line.slice(0, startIndex) + line.slice(endIndex + 1); + lines[index] = line.slice(0, startIndex) + line.slice(endIndex); } } }