Skip to content

Commit

Permalink
Merge pull request #35 from joomcode/fix/update-config-after-do-befor…
Browse files Browse the repository at this point in the history
…e-functions

fix: update pack config by values from `doBeforePack` functions
  • Loading branch information
uid11 authored Sep 23, 2023
2 parents 49cb04d + 071675e commit 15a0eb2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 23 deletions.
5 changes: 5 additions & 0 deletions autotests/tests/main/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ it('exists', {meta: {testId: '1'}, testIdleTimeout: 35_000, testTimeout: 90_000}

await expect(customPackProperties.name, 'custom pack properties is correct').eql('allTests');

await expect(
customPackProperties.internalPackRunId,
'dynamic custom pack properties is correct',
).gt(0);

const mainPage = await navigateToPage(Main, {language});

await expect(mainPage.pageParams, 'pageParams is correct after navigateToPage').eql({language});
Expand Down
5 changes: 5 additions & 0 deletions src/utils/events/registerStartE2edRunEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {EVENTS_DIRECTORY_PATH, TMP_DIRECTORY_PATH} from '../../constants/interna

import {createDirectory, removeDirectory, writeStartInfo} from '../fs';
import {generalLog, writeLogsToFile} from '../generalLog';
import {getFullPackConfig, updateConfig} from '../getFullPackConfig';
import {compilePack, setPackTimeout} from '../pack';
import {getStartInfo} from '../startInfo';

Expand All @@ -22,6 +23,10 @@ export const registerStartE2edRunEvent = async (): Promise<void> => {

await runBeforePackFunctions(startInfo);

const fullPackConfig = getFullPackConfig();

updateConfig(fullPackConfig, startInfo);

const {e2ed, runEnvironment} = startInfo;
const isDockerRun = runEnvironment === RunEnvironment.Docker;
const startMessage = `Run tests ${isDockerRun ? 'in docker' : 'local'} with e2ed@${e2ed.version}`;
Expand Down
15 changes: 15 additions & 0 deletions src/utils/fs/readStartInfoSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {readFileSync} from 'node:fs';

import {READ_FILE_OPTIONS, START_INFO_PATH} from '../../constants/internal';

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

/**
* Read start info from tmp directory in sync manner.
* @internal
*/
export const readStartInfoSync = (): StartInfo => {
const startInfoJsonString = readFileSync(START_INFO_PATH, READ_FILE_OPTIONS);

return JSON.parse(startInfoJsonString) as StartInfo;
};
23 changes: 0 additions & 23 deletions src/utils/getFullPackConfig.ts

This file was deleted.

40 changes: 40 additions & 0 deletions src/utils/getFullPackConfig/getFullPackConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// eslint-disable-next-line import/no-internal-modules
import {readStartInfoSync} from '../fs/readStartInfoSync';

import {updateConfig} from './updateConfig';

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

let updatedConfig: FullPackConfig | undefined;

/**
* Get full pack configuration object.
* This function can only be called after the `E2edRunEvent` is registered,
* because the packs with configurations (`e2ed/packs`) is compiled when this event is registered.
*/
export const getFullPackConfig = <
CustomPackProperties = unknown,
CustomReportProperties = unknown,
SkipTests = unknown,
TestMeta = unknown,
>(): FullPackConfig<CustomPackProperties, CustomReportProperties, SkipTests, TestMeta> => {
if (updatedConfig === undefined) {
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const {fullPackConfig} = require<typeof import('../../testcaferc')>('../../testcaferc');

updatedConfig = fullPackConfig;

try {
const startInfo = readStartInfoSync();

updateConfig(updatedConfig, startInfo);
} catch (error) {}
}

return updatedConfig as unknown as FullPackConfig<
CustomPackProperties,
CustomReportProperties,
SkipTests,
TestMeta
>;
};
3 changes: 3 additions & 0 deletions src/utils/getFullPackConfig/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {getFullPackConfig} from './getFullPackConfig';
/** @internal */
export {updateConfig} from './updateConfig';
20 changes: 20 additions & 0 deletions src/utils/getFullPackConfig/updateConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type {FullPackConfig, StartInfo} from '../../types/internal';

/**
* Updates full pack config by values from `startInfo` (getted by `doBeforePack` functions).
* @internal
*/
export const updateConfig = (fullPackConfig: FullPackConfig, startInfo: StartInfo): void => {
for (const field of Object.keys(fullPackConfig) as (keyof FullPackConfig)[]) {
if (field === 'doAfterPack' || field === 'doBeforePack') {
continue;
}

if (typeof fullPackConfig[field] === 'function') {
continue;
}

// @ts-expect-error: full pack config have different types of field values
fullPackConfig[field] = startInfo.fullPackConfig[field]; // eslint-disable-line no-param-reassign
}
};

0 comments on commit 15a0eb2

Please sign in to comment.