Skip to content

Commit

Permalink
fix: trace logging (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Feb 10, 2024
1 parent 8376963 commit d6f38c8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions e2e/globalTeardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('jest-environment-emit/debug').aggregateLogs;
1 change: 1 addition & 0 deletions e2e/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
...base,

rootDir: '..',
globalTeardown: './e2e/globalTeardown',
testEnvironment: './e2e/testEnvironment.js',
testEnvironmentOptions: {
eventListeners: [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"homepage": "https://github.com/wix-incubator/jest-environment-emit#readme",
"dependencies": {
"bunyamin": "^1.5.0",
"bunyamin": "^1.5.2",
"bunyan": "^2.0.5",
"bunyan-debug-stream": "^3.1.0",
"funpermaproxy": "^1.1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/emitters/ReadonlyEmitterBase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/ban-types */
import { logger, optimizeTracing, iterateSorted } from '../utils';
import { debugLogger, optimizeTracing, iterateSorted } from '../utils';
import type { ReadonlyEmitter } from './Emitter';

//#region Optimized event helpers
Expand All @@ -15,13 +15,13 @@ const __LISTENERS = optimizeTracing((listener: unknown) => ({
const ONCE: unique symbol = Symbol('ONCE');

export abstract class ReadonlyEmitterBase<EventMap> implements ReadonlyEmitter<EventMap> {
protected readonly _log: typeof logger;
protected readonly _log: typeof debugLogger;
protected readonly _listeners: Map<keyof EventMap | '*', [Function, number][]> = new Map();

#listenersCounter = 0;

constructor(name: string) {
this._log = logger.child({
this._log = debugLogger.child({
cat: `emitter`,
tid: [name, {}],
});
Expand Down
9 changes: 6 additions & 3 deletions src/emitters/syncEmitterCommons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ const CATEGORIES = {
INVOKE: ['invoke'],
};

export const __ENQUEUE = optimizeTracing((event: unknown) => ({
export const __ENQUEUE = optimizeTracing((_event: unknown) => ({
cat: CATEGORIES.ENQUEUE,
event,
}));
export const __EMIT = optimizeTracing((event: unknown) => ({ cat: CATEGORIES.EMIT, event }));

export const __EMIT = optimizeTracing((_event: unknown) => ({
cat: CATEGORIES.EMIT,
}));

export const __INVOKE = optimizeTracing((listener: unknown, type?: '*') => ({
cat: CATEGORIES.INVOKE,
fn: `${listener}`,
Expand Down
13 changes: 11 additions & 2 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
TestEnvironmentSyncEventMap,
EnvironmentEventEmitter,
} from './types';
import { getHierarchy } from './utils';
import { debugLogger, getHierarchy } from './utils';

type EnvironmentEventEmitterImpl = SemiAsyncEmitter<
TestEnvironmentAsyncEventMap,
Expand Down Expand Up @@ -42,9 +42,18 @@ export function onTestEnvironmentCreate(
'error',
]);

const environmentConfig = normalizeJestEnvironmentConfig(jestEnvironmentConfig);
debugLogger.trace(
{
testPath: environmentContext.testPath,
environmentConfig,
},
'test_environment_create',
);

contexts.set(jestEnvironment, {
testEvents,
environmentConfig: normalizeJestEnvironmentConfig(jestEnvironmentConfig),
environmentConfig,
environmentContext,
});
}
Expand Down
11 changes: 7 additions & 4 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import {
bunyamin,
nobunyamin,
isDebug,
threadGroups,
traceEventStream,
Expand All @@ -13,7 +14,7 @@ import { noop } from './noop';

const logsDirectory = process.env.JEST_BUNYAMIN_DIR;
const LOG_PATTERN = /^jest-bunyamin\..*\.log$/;
const PACKAGE_NAME = 'jest-enviroment-emit' as const;
const PACKAGE_NAME = 'jest-environment-emit' as const;

function isTraceEnabled(): boolean {
return !!logsDirectory;
Expand Down Expand Up @@ -111,6 +112,8 @@ export const logger = bunyamin.child({
cat: PACKAGE_NAME,
});

export const optimizeTracing: <F>(f: F) => F = isDebug(PACKAGE_NAME)
? (f) => f
: ((() => noop) as any);
const isDebugMode = isDebug(PACKAGE_NAME);

export const debugLogger = isDebugMode ? logger : nobunyamin;

export const optimizeTracing: <F>(f: F) => F = isDebugMode ? (f) => f : ((() => noop) as any);

0 comments on commit d6f38c8

Please sign in to comment.