Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Unwrapped Jest test require module on teardown of environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Serkan ÖZAL committed Oct 24, 2021
1 parent b727857 commit c68a9d7
Show file tree
Hide file tree
Showing 32 changed files with 282 additions and 423 deletions.
8 changes: 0 additions & 8 deletions src/wrappers/foresight/ForesightExecutor.ts
Expand Up @@ -10,12 +10,10 @@ import * as EnvironmentSupport from './environment/EnvironmentSupport';
* @param {ExecutionContext} execContext
*/
export function startInvocation(pluginContext: PluginContext, execContext: ExecutionContext) {

execContext.invocationData = ForesightWrapperUtils.createInvocationData(execContext, pluginContext);

const additionalTags: any = execContext.getAdditionalStartTags();
if (additionalTags) {

Object.keys(additionalTags).forEach((tag) => {
execContext.invocationData.tags[tag] = additionalTags[tag];
});
Expand All @@ -28,14 +26,12 @@ export function startInvocation(pluginContext: PluginContext, execContext: Execu
* @param {ExecutionContext} execContext
*/
export function finishInvocation(pluginContext: PluginContext, execContext: ExecutionContext) {

const { invocationData} = execContext;

ForesightWrapperUtils.finishInvocationData(execContext, pluginContext);

const additionalTags: any = execContext.getAdditionalFinishTags();
if (additionalTags) {

Object.keys(additionalTags).forEach((tag) => {
invocationData.tags[tag] = additionalTags[tag];
});
Expand All @@ -50,14 +46,12 @@ export function finishInvocation(pluginContext: PluginContext, execContext: Exec
* @param {ExecutionContext} execContext
*/
export function startTrace(pluginContext: PluginContext, execContext: ExecutionContext) {

ForesightWrapperUtils.startTrace(pluginContext, execContext);

const { rootSpan } = execContext;

const additionalTags: any = execContext.getAdditionalStartTags();
if (additionalTags) {

Object.keys(additionalTags).forEach((tag) => {
rootSpan.tags[tag] = additionalTags[tag];
});
Expand All @@ -72,14 +66,12 @@ export function startTrace(pluginContext: PluginContext, execContext: ExecutionC
* @param {ExecutionContext} execContext
*/
export function finishTrace(pluginContext: PluginContext, execContext: ExecutionContext) {

ForesightWrapperUtils.finishTrace(pluginContext, execContext);

const { rootSpan } = execContext;

const additionalTags: any = execContext.getAdditionalFinishTags();
if (additionalTags) {

Object.keys(additionalTags).forEach((tag) => {
rootSpan.tags[tag] = additionalTags[tag];
});
Expand Down
15 changes: 1 addition & 14 deletions src/wrappers/foresight/ForesightWrapperUtils.ts
Expand Up @@ -99,7 +99,6 @@ export default class ForesightWrapperUtils {
* @param appInfo appInfo
*/
static getDefaultApplicationId(appInfo: ApplicationInfo) {

return ForesightWrapperUtils.createApplicationId(
appInfo.applicationClassName,
appInfo.applicationName,
Expand All @@ -120,7 +119,6 @@ export default class ForesightWrapperUtils {
* @param pluginContext pluginContext
*/
static createPlugins(config: ThundraConfig, pluginContext: PluginContext, pluginsWillBeLoaded: string[]): any[] {

const plugins: any[] = [];
if (config.disableMonitoring) {
return plugins;
Expand All @@ -129,21 +127,18 @@ export default class ForesightWrapperUtils {
if (pluginsWillBeLoaded.includes(TracePlugin.name)
&& !ConfigProvider.get<boolean>(ConfigNames.THUNDRA_TRACE_DISABLE)
&& config.traceConfig.enabled) {

const tracePlugin = new TracePlugin(config.traceConfig);
plugins.push(tracePlugin);
}

if (pluginsWillBeLoaded.includes(LogPlugin.name)
&& ConfigProvider.get<boolean>(ConfigNames.THUNDRA_AGENT_TEST_LOG_ENABLE)
&& config.logConfig.enabled) {

const logPlugin = new LogPlugin(config.logConfig);
plugins.push(logPlugin);
}

if (pluginsWillBeLoaded.includes(InvocationPlugin.name)) {

const invocationPlugin = new InvocationPlugin(config.invocationConfig);
plugins.push(invocationPlugin);
}
Expand All @@ -159,7 +154,6 @@ export default class ForesightWrapperUtils {
* @param consoleRef consoleRef
*/
static createLogPlugin(consoleRef: any) {

const config = ConfigProvider.thundraConfig;

if (ConfigProvider.get<boolean>(ConfigNames.THUNDRA_AGENT_TEST_LOG_ENABLE) && config.logConfig.enabled) {
Expand All @@ -175,7 +169,6 @@ export default class ForesightWrapperUtils {
* @param executor executor
*/
static createPluginContext(apiKey: string, executor: any): PluginContext {

const applicationInfo = ApplicationManager.getApplicationInfo();

return new PluginContext({
Expand All @@ -198,7 +191,6 @@ export default class ForesightWrapperUtils {
* @param testSuiteName testSuiteName
*/
static createTestSuiteExecutionContext(testSuiteName: string): TestSuiteExecutionContext {

const { thundraConfig } = ConfigProvider;
const tracerConfig = get(thundraConfig, 'traceConfig.tracerConfig', {});

Expand Down Expand Up @@ -262,7 +254,6 @@ export default class ForesightWrapperUtils {
* @param context context
*/
static async beforeTestProcess(plugins: any[], context: ExecutionContext) {

for (const plugin of plugins) {
await plugin.beforeInvocation(context);
}
Expand All @@ -275,7 +266,6 @@ export default class ForesightWrapperUtils {
* @param reporter reporter
*/
static async afterTestProcess(plugins: any[], context: ExecutionContext, reporter: Reporter) {

context.finishTimestamp = Date.now();

let reports: any = [];
Expand Down Expand Up @@ -311,7 +301,6 @@ export default class ForesightWrapperUtils {
* @param execContext execContext
*/
static startTrace(pluginContext: PluginContext, execContext: ExecutionContext) {

const { tracer } = execContext;
const traceId = Utils.generateId();
const contextInformation: any = execContext.getContextInformation();
Expand All @@ -337,7 +326,6 @@ export default class ForesightWrapperUtils {
* @param execContext execContext
*/
static finishTrace(pluginContext: PluginContext, execContext: ExecutionContext) {

const {
error,
rootSpan,
Expand All @@ -357,7 +345,6 @@ export default class ForesightWrapperUtils {
* @param pluginContext pluginContext
*/
static createInvocationData(execContext: ExecutionContext, pluginContext: PluginContext): InvocationData {

const invocationData = Utils.initMonitoringData(pluginContext,
MonitoringDataType.INVOCATION) as InvocationData;

Expand Down Expand Up @@ -387,7 +374,6 @@ export default class ForesightWrapperUtils {
* @param pluginContext pluginContext
*/
static finishInvocationData(execContext: ExecutionContext, pluginContext: PluginContext) {

const {
error,
invocationData,
Expand Down Expand Up @@ -429,4 +415,5 @@ export default class ForesightWrapperUtils {
invocationData.outgoingTraceLinks = InvocationTraceSupport.getOutgoingTraceLinks();
invocationData.applicationResourceName = applicationResourceName;
}

}
6 changes: 0 additions & 6 deletions src/wrappers/foresight/TestRunnerSupport.ts
Expand Up @@ -81,7 +81,6 @@ export const setTestStatusReportFreq = (freq: number) => {

const sendTestRunStatus = async () => {
if (!testRunScope || !initialized) {

ThundraLogger.debug('<ThundraRunnerSupport> Test run not started. TestRunStatus will not send.');
return;
}
Expand Down Expand Up @@ -116,23 +115,20 @@ const sendTestRunStatus = async () => {
};

export const startTestRunStatusEvent = () => {

if (testStatusReportFreq) {
finishTestRunStatusEvent();
testRunStatusWork = setTimeout(sendTestRunStatus, testStatusReportFreq);
}
};

export const finishTestRunStatusEvent = () => {

if (testRunStatusWork) {
clearInterval(testRunStatusWork);
testRunStatusWork = null;
}
};

export const startTestRun = (): TestRunStart => {

const testRunId = getTestRunId();
const taskId = Utils.generateId();
const startTimestamp = new Date().getTime();
Expand All @@ -159,7 +155,6 @@ export const startTestRun = (): TestRunStart => {

export const finishTestRun = (): TestRunFinish => {
if (!initialized || !testRunScope) {

ThundraLogger.debug('<ThundraRunnerSupport> Test run not started. TestRunFinish will not send.');
return;
}
Expand Down Expand Up @@ -193,7 +188,6 @@ export const finishTestRun = (): TestRunFinish => {
};

export const clearTestRun = () => {

setInitialized(false);
finishTestRunStatusEvent();
setWrapperContext(null);
Expand Down
5 changes: 0 additions & 5 deletions src/wrappers/foresight/context/ForesightContextProvider.ts
Expand Up @@ -38,7 +38,6 @@ function initAsync(asyncId: number, type: string, triggerAsyncId: number, resour
}

function _setContext(execContext: ExecutionContext) {

if (execContext.isContextCompatibleWith(ContextMode.AsyncMode)) {

currentContextModel = ContextMode.AsyncMode;
Expand Down Expand Up @@ -71,7 +70,6 @@ export function canChangeablebleContext() {
}

export function runWithContext(createExecContext: Function, fn: Function) {

const execContext: ExecutionContext = createExecContext();

if (activeGlobalExecutionContext != null
Expand All @@ -85,7 +83,6 @@ export function runWithContext(createExecContext: Function, fn: Function) {
}

export function get(): any {

let context;

if (currentContextModel === ContextMode.GlobalMode) {
Expand All @@ -98,12 +95,10 @@ export function get(): any {
}

export function set(execContext: ExecutionContext) {

_setContext(execContext);
}

export function init() {

const hook = asyncHooks.createHook({
init: initAsync,
destroy: destroyAsync,
Expand Down
5 changes: 1 addition & 4 deletions src/wrappers/foresight/context/TestCaseExecutionContext.ts
Expand Up @@ -29,7 +29,6 @@ export default class TestCaseExecutionContext extends ExecutionContext {
}

getContextInformation() {

const baseContextInformation = super.getContextInformation();

return {
Expand All @@ -42,7 +41,6 @@ export default class TestCaseExecutionContext extends ExecutionContext {
}

getAdditionalStartTags() {

const testRunScope = TestRunnerSupport.testRunScope;

return {
Expand All @@ -56,7 +54,6 @@ export default class TestCaseExecutionContext extends ExecutionContext {
}

getAdditionalFinishTags() {

return {
[TestRunnerTags.TEST_STATUS]: this.status,
...(TestRunnerSupport.testSuiteExecutionContext && TestRunnerSupport.testSuiteExecutionContext.invocationData
Expand All @@ -70,7 +67,7 @@ export default class TestCaseExecutionContext extends ExecutionContext {
}

protected initContextMode() {

this.compatibleContextModes.push(ContextMode.GlobalMode);
}

}
4 changes: 1 addition & 3 deletions src/wrappers/foresight/context/TestSuiteExecutionContext.ts
Expand Up @@ -58,7 +58,6 @@ export default class TestSuiteExecutionContext extends ExecutionContext {
}

getContextInformation() {

const baseContextInformation = super.getContextInformation();

return {
Expand All @@ -71,7 +70,6 @@ export default class TestSuiteExecutionContext extends ExecutionContext {
}

getAdditionalStartTags() {

const testRunScope = TestRunnerSupport.testRunScope;

return {
Expand All @@ -93,7 +91,7 @@ export default class TestSuiteExecutionContext extends ExecutionContext {
}

protected initContextMode() {

this.compatibleContextModes.push(ContextMode.GlobalMode);
}

}
4 changes: 0 additions & 4 deletions src/wrappers/foresight/environment/EnvironmentSupport.ts
Expand Up @@ -12,15 +12,13 @@ let environmentInfo: EnvironmentInfo;
* Initiate all environment providers and set non empty one
*/
export const init = async () => {

ThundraLogger.debug('<EnvironmentSupport> Environments initilizing ...');

await InfoProvider.init();

Object.values(InfoProvider.environmentInfoProviders).forEach((environmentInfoProvider) => {
const ei: EnvironmentInfo = environmentInfoProvider.getEnvironmentInfo();
if (ei != null) {

ThundraLogger.debug(`<EnvironmentSupport> Environment loaded. ${ei.environment}`);
environmentInfo = ei;
return;
Expand All @@ -40,7 +38,6 @@ export const getEnvironmentInfo = () => {
* @param invocationData invocationData
*/
export const tagInvocation = (invocationData: InvocationData) => {

if (environmentInfo) {
invocationData.tags[TestRunnerTags.TEST_ENVIRONMENT] = environmentInfo.environment;
invocationData.tags[TestRunnerTags.SOURCE_CODE_REPO_URL] = environmentInfo.repoURL;
Expand All @@ -56,7 +53,6 @@ export const tagInvocation = (invocationData: InvocationData) => {
* @param span span
*/
export const tagSpan = (span: ThundraSpan) => {

if (environmentInfo) {
span.tags[TestRunnerTags.TEST_ENVIRONMENT] = environmentInfo.environment;
span.tags[TestRunnerTags.SOURCE_CODE_REPO_URL] = environmentInfo.repoURL;
Expand Down
6 changes: 1 addition & 5 deletions src/wrappers/foresight/environment/bitbucket/index.ts
Expand Up @@ -11,7 +11,6 @@ export const ENVIRONMENT = 'BitBucket';
let environmentInfo: EnvironmentInfo;

const getTestRunId = (repoURL: string, commitHash: string) => {

const testRunId = ConfigProvider.get<string>(ConfigNames.THUNDRA_AGENT_TEST_RUN_ID);
if (testRunId) {
return testRunId;
Expand All @@ -36,7 +35,6 @@ const isBitBucketEnvironment = () => {
* Get environment info
*/
export const getEnvironmentInfo = () => {

return environmentInfo;
};

Expand All @@ -46,11 +44,9 @@ export const getEnvironmentInfo = () => {
export const init = async (): Promise<void> => {
try {
if (environmentInfo == null && isBitBucketEnvironment()) {

let repoURL = process.env[ConfigNames.BITBUCKET_GIT_HTTP_ORIGIN_ENV_VAR_NAME]
|| process.env[ConfigNames.BITBUCKET_GIT_HTTP_ORIGIN_ENV_VAR_NAME.toLowerCase()];
if (!repoURL) {

repoURL = process.env[ConfigNames.BITBUCKET_GIT_SSH_ORIGIN_ENV_VAR_NAME]
|| process.env[ConfigNames.BITBUCKET_GIT_SSH_ORIGIN_ENV_VAR_NAME.toLowerCase()];
}
Expand Down Expand Up @@ -84,6 +80,6 @@ export const init = async (): Promise<void> => {
}
} catch (e) {
ThundraLogger.error(
`<GithubEnvironmentInfoProvider> Unable to build environment info`);
'<GithubEnvironmentInfoProvider> Unable to build environment info');
}
};

0 comments on commit c68a9d7

Please sign in to comment.