Skip to content

Commit

Permalink
FI-1072 fix: fail tests run if doAfterPack/doBeforePack throw an …
Browse files Browse the repository at this point in the history
…error

fix: do not lose error fields after `replaceFields`
  • Loading branch information
uid11 committed Dec 13, 2023
1 parent 8795c2b commit a0514f5
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 39 deletions.
2 changes: 1 addition & 1 deletion autotests/configurator/mapBackendResponseToLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type {MapBackendResponseToLog} from 'autotests/types/packSpecific';
*/
export const mapBackendResponseToLog: MapBackendResponseToLog = ({
duration,
statusCode,
request,
statusCode,
}) => {
if (statusCode >= 400) {
return undefined;
Expand Down
29 changes: 14 additions & 15 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@ Modules in the dependency graph should only import the modules above them:
12. `utils/valueToString`
13. `utils/error`
14. `utils/asserts`
15. `utils/userlandHooks`
15. `utils/userland`
16. `utils/fn`
17. `utils/environment`
18. `utils/getFullPackConfig`
19. `utils/runLabel`
20. `utils/generalLog`
21. `utils/runArrayOfFunctionsSafely`
22. `utils/fs`
23. `selectors`
24. `Route`
25. `ApiRoute`
26. `PageRoute`
27. `testController`
28. `useContext`
29. `context`
30. `utils/log`
31. `utils/waitForEvents`
32. `utils/expect`
33. `expect`
34. ...
21. `utils/fs`
22. `selectors`
23. `Route`
24. `ApiRoute`
25. `PageRoute`
26. `testController`
27. `useContext`
28. `context`
29. `utils/log`
30. `utils/waitForEvents`
31. `utils/expect`
32. `expect`
33. ...
2 changes: 1 addition & 1 deletion src/actions/pages/navigateToPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {LogEventType} from '../../constants/internal';
import {getDurationWithUnits} from '../../utils/getDurationWithUnits';
import {log} from '../../utils/log';
import {getUserlandHooks} from '../../utils/userlandHooks';
import {getUserlandHooks} from '../../utils/userland';

import {createPageInstance} from './createPageInstance';

Expand Down
3 changes: 2 additions & 1 deletion src/configurator/getReplacedObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const getReplacedObject = <Value extends object>(

const replacedObject = (Array.isArray(value) ? [] : {}) as Value;

const keys: PropertyKey[] = Object.keys(value);
const keys: PropertyKey[] =
value instanceof Error ? Object.getOwnPropertyNames(value) : Object.keys(value);

keys.push(...Object.getOwnPropertySymbols(value));

Expand Down
2 changes: 2 additions & 0 deletions src/constants/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export const enum ExitCode {
HasErrors = 2,
NoRetries = 3,
NoReportData = 4,
HasErrorsInDoAfterPackFunctions = 5,
HasErrorsInDoBeforePackFunctions = 6,
}
2 changes: 1 addition & 1 deletion src/createTestFunction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {setUserlandHooks} from './utils/userlandHooks';
import {setUserlandHooks} from './utils/userland';
import {test} from './test';

import type {AnyPack, GetPackParameters, TestFunction, UserlandHooks} from './types/internal';
Expand Down
9 changes: 8 additions & 1 deletion src/utils/events/registerEndE2edRunEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ExitCode} from '../../constants/internal';
import {processExit} from '../exit';
import {failMessage, generalLog, okMessage} from '../generalLog';
import {collectReportData, getLiteReport, writeHtmlReport, writeLiteJsonReport} from '../report';
import {setReadonlyProperty} from '../setReadonlyProperty';

import {collectFullEventsData} from './collectFullEventsData';
import {runAfterPackFunctions} from './runAfterPackFunctions';
Expand All @@ -29,7 +30,13 @@ export const registerEndE2edRunEvent = async (): Promise<void> => {

const liteReport = getLiteReport(reportData);

await runAfterPackFunctions(liteReport);
try {
await runAfterPackFunctions(liteReport);
} catch (error) {
generalLog('Caught an error on run "after pack" functions', {error});

setReadonlyProperty(reportData, 'exitCode', ExitCode.HasErrorsInDoAfterPackFunctions);
}

const {customReportProperties} = liteReport;

Expand Down
2 changes: 1 addition & 1 deletion src/utils/events/registerEndTestRunEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {cloneWithoutLogEvents} from '../clone';
import {getRunErrorFromError} from '../error';
import {writeTestRunToJsonFile} from '../fs';
import {generalLog, logEndTestRunEvent, writeLogsToFile} from '../generalLog';
import {getUserlandHooks} from '../userlandHooks';
import {getUserlandHooks} from '../userland';

import {calculateTestRunStatus} from './calculateTestRunStatus';
import {getTestRunEvent} from './getTestRunEvent';
Expand Down
12 changes: 10 additions & 2 deletions src/utils/events/registerStartE2edRunEvent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {RunEnvironment} from '../../configurator';
import {EVENTS_DIRECTORY_PATH, TMP_DIRECTORY_PATH} from '../../constants/internal';
import {EVENTS_DIRECTORY_PATH, ExitCode, TMP_DIRECTORY_PATH} from '../../constants/internal';

import {E2edError} from '../error';
import {setGlobalExitCode} from '../exit';
import {createDirectory, removeDirectory, writeStartInfo} from '../fs';
import {generalLog, writeLogsToFile} from '../generalLog';
import {getFullPackConfig, updateConfig} from '../getFullPackConfig';
Expand All @@ -21,7 +23,13 @@ export const registerStartE2edRunEvent = async (): Promise<void> => {

const startInfo = getStartInfo();

await runBeforePackFunctions(startInfo);
try {
await runBeforePackFunctions(startInfo);
} catch (cause) {
setGlobalExitCode(ExitCode.HasErrorsInDoBeforePackFunctions);

throw new E2edError('Caught an error on running "before pack" functions', {cause});
}

const fullPackConfig = getFullPackConfig();

Expand Down
12 changes: 10 additions & 2 deletions src/utils/events/runAfterPackFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {generalLog} from '../generalLog';
import {getFullPackConfig} from '../getFullPackConfig';
import {runArrayOfFunctionsSafely} from '../runArrayOfFunctionsSafely';
import {runArrayOfUserlandFunctions} from '../userland';

import type {CustomReportPropertiesPlaceholder, LiteReport, Void} from '../../types/internal';

Expand All @@ -19,5 +20,12 @@ export const runAfterPackFunctions = async (liteReport: LiteReport): Promise<voi
Object.assign<LiteReport, Partial<LiteReport>>(liteReport, {customReportProperties: result});
};

await runArrayOfFunctionsSafely(functions, () => args, processCurrentFunctionResult);
const message =
functions.length > 0
? `Will be run ${functions.length} after pack function${functions.length > 1 ? 's' : ''}`
: 'There are no after pack functions';

generalLog(message);

await runArrayOfUserlandFunctions(functions, () => args, processCurrentFunctionResult);
};
12 changes: 10 additions & 2 deletions src/utils/events/runBeforePackFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {runArrayOfFunctionsSafely} from '../runArrayOfFunctionsSafely';
import {generalLog} from '../generalLog';
import {runArrayOfUserlandFunctions} from '../userland';

import type {
FullPackConfig,
Expand Down Expand Up @@ -33,7 +34,14 @@ export const runBeforePackFunctions = async (startInfo: StartInfo): Promise<void
});
};

await runArrayOfFunctionsSafely(functions, () => args, processCurrentFunctionResult);
const message =
functions.length > 0
? `Will be run ${functions.length} before pack function${functions.length > 1 ? 's' : ''}`
: 'There are no before pack functions';

generalLog(message);

await runArrayOfUserlandFunctions(functions, () => args, processCurrentFunctionResult);

Object.assign<FullPackConfigWithoutDoBeforePack, Partial<FullPackConfig>>(
startInfo.fullPackConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/exit/getExitCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {assertValueIsDefined} from '../asserts';
import type {Retry} from '../../types/internal';

/**
* Get e2ed exit code by hasErrors flag and array of retries.
* Get e2ed exit code by `hasErrors` flag and array of retries.
* @internal
*/
export const getExitCode = (hasErrors: boolean, retries: readonly Retry[]): ExitCode => {
Expand Down
23 changes: 23 additions & 0 deletions src/utils/exit/globalExitCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {assertValueIsDefined, assertValueIsUndefined} from '../asserts';

import type {ExitCode} from '../../constants/internal';

let globalExitCode: ExitCode | undefined;

/**
* Get global exit code.
* @internal
*/
export const getGlobalExitCode = (): ExitCode | undefined => globalExitCode;

/**
* Set global exit code (once).
* @internal
*/
export const setGlobalExitCode = (exitCode: ExitCode): void => {
assertValueIsUndefined(globalExitCode, 'globalExitCode is not defined', {exitCode});

assertValueIsDefined(exitCode, 'exitCode is defined', {globalExitCode});

globalExitCode = exitCode;
};
2 changes: 2 additions & 0 deletions src/utils/exit/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** @internal */
export {getExitCode} from './getExitCode';
/** @internal */
export {setGlobalExitCode} from './globalExitCode';
/** @internal */
export {processExit} from './processExit';
10 changes: 9 additions & 1 deletion src/utils/exit/processExit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import {ExitCode} from '../../constants/internal';

import {generalLog, writeLogsToFile} from '../generalLog';

import {getGlobalExitCode} from './globalExitCode';

/**
* Exit from e2ed process with correct exit code.
* @internal
*/
export const processExit = async (exitCode: ExitCode = ExitCode.NoReportData): Promise<void> => {
export const processExit = async (
exitCodeFromReport: ExitCode = ExitCode.NoReportData,
): Promise<void> => {
const globalExitCode = getGlobalExitCode();

const exitCode = globalExitCode === undefined ? exitCodeFromReport : globalExitCode;

generalLog(`Exit from e2ed with code ${exitCode}`);

await writeLogsToFile();
Expand Down
2 changes: 1 addition & 1 deletion src/utils/log/logWithPreparedOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {getRunId} from '../../context/runId';

import {generalLog} from '../generalLog';
import {getUserlandHooks} from '../userlandHooks';
import {getUserlandHooks} from '../userland';

import type {LogEventType} from '../../constants/internal';
import type {LogPayload, RunId, UtcTimeInMs} from '../../types/internal';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/test/beforeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {setTestTimeout} from '../../context/testTimeout';
import {getRunLabel} from '../environment';
import {registerStartTestRunEvent} from '../events';
import {getFullPackConfig} from '../getFullPackConfig';
import {getUserlandHooks} from '../userlandHooks';
import {getUserlandHooks} from '../userland';

import {getTestFnAndReject} from './getTestFnAndReject';
import {processBrokenTestRuns} from './processBrokenTestRuns';
Expand Down
4 changes: 4 additions & 0 deletions src/utils/userland/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @internal */
export {getUserlandHooks, setUserlandHooks} from './userlandHooks';
/** @internal */
export {runArrayOfUserlandFunctions} from './runArrayOfUserlandFunctions';
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {generalLog} from './generalLog';
import {E2edError} from '../error';

import type {Fn} from '../types/internal';
import type {Fn} from '../../types/internal';

/**
* Safely run array of userland function (from `doBeforePack`/`doAfterPack`).
* Run array of userland function (from `doBeforePack`/`doAfterPack`).
* @internal
*/
export const runArrayOfFunctionsSafely = async <Args extends readonly unknown[], Return>(
export const runArrayOfUserlandFunctions = async <Args extends readonly unknown[], Return>(
functions: readonly Fn<Args, Return>[],
getCurrentFunctionArgs: () => Args,
processCurrentFunctionResult: (result: Awaited<Return>) => void,
Expand All @@ -21,8 +21,8 @@ export const runArrayOfFunctionsSafely = async <Args extends readonly unknown[],
const result = await fn(...args);

processCurrentFunctionResult(result);
} catch (error) {
generalLog('Caught an error on running current function in array', {args, error, fn});
} catch (cause) {
throw new E2edError('Caught an error on running userland function', {args, cause, fn});
}
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {assertValueIsDefined, assertValueIsUndefined} from './asserts';
import {assertValueIsDefined, assertValueIsUndefined} from '../asserts';

import type {UserlandHooks} from '../types/internal';
import type {UserlandHooks} from '../../types/internal';

let userlandHooks: UserlandHooks | undefined;

Expand Down

0 comments on commit a0514f5

Please sign in to comment.