Skip to content

Commit

Permalink
FI-1237 fix: state of "full mocks" after first retry
Browse files Browse the repository at this point in the history
  • Loading branch information
uid11 committed Jul 30, 2024
1 parent feded3a commit 62c2f19
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 77 deletions.
2 changes: 2 additions & 0 deletions autotests/configurator/mapLogPayloadInConsole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:')
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down
14 changes: 14 additions & 0 deletions scripts/checkBranch.ts
Original file line number Diff line number Diff line change
@@ -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"');
2 changes: 0 additions & 2 deletions src/types/retries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export type RetriesState = Readonly<{
retryIndex: number;
startLastRetryTimeInMs: UtcTimeInMs;
successfulTestRunNamesHash: Record<string, true>;
visitedTestNamesHash: Record<string, true>;
visitedTestRunEventsFileName: readonly string[];
}>;

Expand All @@ -25,7 +24,6 @@ export type RetriesState = Readonly<{
export type RunRetryOptions = Readonly<{
runLabel: RunLabel;
successfulTestRunNamesHash: VisitedTestNamesHash;
visitedTestNamesHash: VisitedTestNamesHash;
}>;

/**
Expand Down
11 changes: 0 additions & 11 deletions src/utils/fullMocks/getShouldApplyMocks.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/utils/fullMocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ export {enableFullMocks} from './enableFullMocks';
/** @internal */
export {getResponseFromFullMocks} from './getResponseFromFullMocks';
/** @internal */
export {getShouldApplyMocks} from './getShouldApplyMocks';
/** @internal */
export {writeResponseToFullMocks} from './writeResponseToFullMocks';
2 changes: 0 additions & 2 deletions src/utils/globalState/index.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/utils/globalState/visitedTestNamesHash.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/utils/pack/runPackWithArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ export const runPackWithArgs = async (): Promise<void> => {
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);
};
8 changes: 6 additions & 2 deletions src/utils/report/writeHtmlReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,11 +29,13 @@ export const writeHtmlReport = async (reportData: ReportData): Promise<void> =>

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}`,
);
};
10 changes: 2 additions & 8 deletions src/utils/retry/processRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import type {RetriesState, UtcTimeInMs} from '../../types/internal';
* @internal
*/
export const processRetry = async (retriesState: RetriesState): Promise<void> => {
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;
Expand All @@ -35,7 +29,7 @@ export const processRetry = async (retriesState: RetriesState): Promise<void> =>
try {
await writeLogsToFile();

await runRetry({runLabel, successfulTestRunNamesHash, visitedTestNamesHash});
await runRetry({runLabel, successfulTestRunNamesHash});

setReadonlyProperty(retriesState, 'isLastRetrySuccessful', true);
} catch (error) {
Expand Down
1 change: 0 additions & 1 deletion src/utils/retry/runPackWithRetries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const runPackWithRetries = async (): Promise<void> => {
retryIndex: 1,
startLastRetryTimeInMs: 0 as UtcTimeInMs,
successfulTestRunNamesHash: Object.create(null) as {},
visitedTestNamesHash: Object.create(null) as {},
visitedTestRunEventsFileName: [],
};

Expand Down
6 changes: 0 additions & 6 deletions src/utils/retry/updateRetriesStateAfterRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const updateRetriesStateAfterRetry = async (retriesState: RetriesState):
maxRetriesCount,
retryIndex,
successfulTestRunNamesHash,
visitedTestNamesHash,
visitedTestRunEventsFileName,
} = retriesState;
const {
Expand All @@ -44,7 +43,6 @@ export const updateRetriesStateAfterRetry = async (retriesState: RetriesState):
);

successfulTestRunNamesHash[name] = true;
visitedTestNamesHash[name] = true;
}

for (const {runId} of newFullTestRuns) {
Expand Down Expand Up @@ -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<Mutable<RetriesState>> = {
concurrency: concurrencyForNextRetry,
failedTestNamesInLastRetry,
Expand Down
4 changes: 3 additions & 1 deletion src/utils/test/getRunTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 15 additions & 7 deletions src/utils/test/runTestFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<void> => {
export const runTestFn = async ({
runId,
retry,
testController,
testStaticOptions,
}: Options): Promise<void> => {
const testRunEvent = getTestRunEvent(runId);
const testTimeout = getTestTimeout();

Expand All @@ -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);
}
Expand Down
11 changes: 3 additions & 8 deletions src/utils/tests/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<void> => {
export const runTests = async ({runLabel}: RunRetryOptions): Promise<void> => {
setRunLabel(runLabel);
setVisitedTestNamesHash(visitedTestNamesHash);

try {
const {browserInitTimeout, enableLiveMode, resourceUsageReadingInternal} = getFullPackConfig();
Expand Down Expand Up @@ -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]')) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/tests/stripExtraLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 62c2f19

Please sign in to comment.