Skip to content

Commit

Permalink
fix: use newest versions
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Dec 1, 2023
1 parent 3651248 commit 031bcb9
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 112 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json"
"project": "tsconfig.json",
"ecmaVersion": 2021
},
"plugins": [
"no-only-tests"
Expand Down
2 changes: 1 addition & 1 deletion e2e/jest-recorder.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const JEST_ENVIRONMENT = process.env.JEST_ENVIRONMENT || 'node';
if (CI) {
const logsDir = path.join(artifactsDir, PRESET, 'logs');
fs.mkdirSync(logsDir, { recursive: true });
process.env.JEST_METADATA_DEBUG = logsDir;
process.env.JEST_BUNYAMIN_DIR = logsDir;
}

const mixins = {
Expand Down
5 changes: 0 additions & 5 deletions e2e/reporters/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ class E2eRecorderReporter extends JestMetadataReporter {
fs.mkdirSync(path.dirname(fixturePath), { recursive: true });
fs.writeFileSync(fixturePath, contents + '\n');
}

if (debugUtils.isEnabled()) {
await sleep(1000);
await debugUtils.aggregateLogs();
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@
"homepage": "https://github.com/wix-incubator/jest-metadata#readme",
"dependencies": {
"bunyamin": "^1.5.0",
"bunyan": "^2.0.5",
"bunyan-debug-stream": "^3.1.0",
"funpermaproxy": "^1.1.0",
"jest-environment-emit": "^1.0.0-alpha.8",
"jest-environment-emit": "^1.0.1",
"lodash.merge": "^4.6.2",
"node-ipc": "9.2.1",
"strip-ansi": "^6.0.0",
Expand Down
6 changes: 0 additions & 6 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { ParentProcessRealm, realm } from './realms';

export { aggregateLogs } from './utils';

export { Shallow } from './jest-reporter';

export function isEnabled() {
return !!process.env.JEST_METADATA_DEBUG;
}

export function isFallback() {
return realm.type === 'parent_process'
? (realm as ParentProcessRealm).fallbackAPI.enabled
Expand Down
7 changes: 7 additions & 0 deletions src/jest-reporter/ReporterServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-empty-function,unicorn/no-for-loop */
import type { TestCaseResult, TestResult } from '@jest/reporters';
// eslint-disable-next-line import/no-internal-modules
import { aggregateLogs } from 'jest-environment-emit/debug';
import type { IPCServer } from '../ipc';
import { logger, memoizeArg1, memoizeLast, optimizeTracing } from '../utils';
import type { AssociateMetadata } from './AssociateMetadata';
Expand Down Expand Up @@ -97,5 +99,10 @@ export class ReporterServer {

async onRunComplete(): Promise<void> {
await this.#ipc.stop();

if (process.env.JEST_BUNYAMIN_DIR) {
await new Promise((resolve) => setTimeout(resolve, 1000));
await aggregateLogs();
}
}
}
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './emitters';
export * as jestUtils from './jestUtils';
export { getVersion } from './getVersion';
export { aggregateLogs, logger, optimizeTracing } from './logger';
export { logger, optimizeTracing } from './logger';
export { get } from './get';
export { set } from './set';
export { makeDeferred, Deferred } from './makeDeferred';
Expand Down
100 changes: 6 additions & 94 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import fs from 'fs';
import path from 'path';
import { bunyamin, threadGroups, traceEventStream, uniteTraceEventsToFile } from 'bunyamin';
import { createLogger } from 'bunyan';
import createDebugStream from 'bunyan-debug-stream';
import { bunyamin, isDebug, threadGroups } from 'bunyamin';
import { noop } from './noop';

const logsDirectory = process.env.JEST_METADATA_DEBUG;

bunyamin.useLogger(createBunyanImpl(isTraceEnabled()), 1);

threadGroups.add({ id: 'ipc-server', displayName: 'IPC Server (jest-metadata)' });
threadGroups.add({ id: 'ipc-client', displayName: 'IPC Client (jest-metadata)' });
threadGroups.add({ id: 'emitter-core', displayName: 'Core emitter (jest-metadata)' });
Expand All @@ -18,93 +10,13 @@ threadGroups.add({ id: 'environment', displayName: 'Test Environment (jest-metad
threadGroups.add({ id: 'metadata', displayName: 'Metadata (jest-metadata)' });
threadGroups.add({ id: 'reporter', displayName: 'Reporter (jest-metadata)' });

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const optimizeTracing: <F>(f: F) => F = isTraceEnabled() ? (f) => f : ((() => noop) as any);

function isTraceEnabled(): boolean {
return !!logsDirectory;
}

function createLogFilePath() {
const suffix = process.env.JEST_WORKER_ID ? `-${process.env.JEST_WORKER_ID}` : '';
let counter = 0;
let filePath = '';

do {
// when jest-metadata test environment is not configured,
// we don't have realm reuse in the sandboxed environment,
// so erroneously the logger tries to create a new file
// with the same name. This is a workaround for that.
filePath = path.join(
process.env.JEST_METADATA_DEBUG!,
`jest-metadata.${process.pid}${suffix}${counter-- || ''}.log`,
);
} while (fs.existsSync(filePath));

return filePath;
return isDebug('jest-metadata');
}

function createBunyanImpl(traceEnabled: boolean) {
const label = process.env.JEST_WORKER_ID ? `Worker ${process.env.JEST_WORKER_ID}` : 'Main';
const bunyan = createLogger({
name: `Jest Metadata (${label})`,
streams: [
{
level: 'warn' as const,
stream: createDebugStream({
out: process.stderr,
showMetadata: false,
showDate: false,
showPid: false,
showProcess: false,
showLoggerName: false,
showLevel: false,
prefixers: {
cat: () => 'jest-metadata',
},
}),
},
...(traceEnabled
? [
{
level: 'trace' as const,
stream: traceEventStream({
filePath: createLogFilePath(),
threadGroups: bunyamin.threadGroups,
}),
},
]
: []),
],
});

return bunyan;
}

const LOG_PATTERN = /^jest-metadata\..*\.log$/;

export async function aggregateLogs() {
const root = logsDirectory;
if (!root) {
return;
}

const unitedLogPath = path.join(root, 'jest-metadata.log');
if (fs.existsSync(unitedLogPath)) {
fs.rmSync(unitedLogPath);
}

const logs = fs
.readdirSync(root)
.filter((x) => LOG_PATTERN.test(x))
.map((x) => path.join(root, x));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const optimizeTracing: <F>(f: F) => F = isTraceEnabled() ? (f) => f : ((() => noop) as any);

if (logs.length > 1) {
await uniteTraceEventsToFile(logs, unitedLogPath);
for (const x of logs) fs.rmSync(x);
} else {
fs.renameSync(logs[0], unitedLogPath);
}
}
export const logger = bunyamin.child({ cat: 'jest-metadata' });

export { bunyamin as logger, nobunyamin as nologger } from 'bunyamin';
export { nobunyamin as nologger } from 'bunyamin';
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2022",
"module": "commonjs",
"module": "node16",
"declaration": true,
"sourceMap": true,
"outDir": "./dist",
Expand Down

0 comments on commit 031bcb9

Please sign in to comment.