Skip to content

Commit

Permalink
feat: avoid side effects at initiation
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Oct 30, 2023
1 parent e265d36 commit 074e361
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
13 changes: 1 addition & 12 deletions src/Roarr.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createLogger } from './factories/createLogger';
import { createMockLogger } from './factories/createMockLogger';
import { createRoarrInitialGlobalState } from './factories/createRoarrInitialGlobalState';
import { type MessageSerializer, type RoarrGlobalState } from './types';
import { isTruthy } from './utilities/isTruthy';
import fastJson from 'fast-json-stringify';
import safeStringify from 'safe-stable-stringify';

Expand Down Expand Up @@ -30,15 +28,6 @@ const ROARR = createRoarrInitialGlobalState(

globalThis.ROARR = ROARR;

let logFactory = createLogger;

// eslint-disable-next-line node/no-process-env
const enabled = isTruthy(process.env.ROARR_LOG ?? '');

if (!enabled) {
logFactory = createMockLogger;
}

const serializeMessage: MessageSerializer = (message) => {
return (
'{"context":' +
Expand All @@ -48,7 +37,7 @@ const serializeMessage: MessageSerializer = (message) => {
);
};

const Roarr = logFactory((message) => {
const Roarr = createLogger((message) => {
if (ROARR.write) {
// Stringify message as soon as it is received to prevent
// properties of the context from being modified by reference.
Expand Down
12 changes: 12 additions & 0 deletions src/factories/createLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
type TransformMessageFunction,
} from '../types';
import { hasOwnProperty } from '../utilities';
import { isBrowser } from '../utilities/isBrowser';
import { isTruthy } from '../utilities/isTruthy';
import { createMockLogger } from './createMockLogger';
import { printf } from 'fast-printf';
import safeStringify from 'safe-stable-stringify';

Expand Down Expand Up @@ -121,6 +124,15 @@ export const createLogger = (
parentMessageContext: MessageContext = {},
transforms: ReadonlyArray<TransformMessageFunction<MessageContext>> = [],
): Logger => {
if (!isBrowser()) {
// eslint-disable-next-line node/no-process-env
const enabled = isTruthy(process.env.ROARR_LOG ?? '');

if (!enabled) {
return createMockLogger(onMessage, parentMessageContext);
}
}

const log = (
a: any,
b: any,
Expand Down
1 change: 1 addition & 0 deletions src/utilities/isBrowser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isBrowser = () => typeof window !== 'undefined';
4 changes: 1 addition & 3 deletions test/roarr/integrations/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Integration Tests

Tests in `./integrations` simulate `ROARR_LOG=0` (`noLog.ts`) and `ROARR_LOG=1` (`log.ts`) states.

The reason we cannot just sandbox this logic is because [the factory logic is initiated at the time when script is loaded](https://github.com/gajus/roarr/blob/v7.0.3/src/Roarr.ts#L37-L48), i.e. there is no way to reset logic once the script has been imported. Furthermore, each test must be run in a separate process to avoid global pollution (e.g., `ROARR.sequence`).
Tests in `./integrations` simulate `ROARR_LOG=0` (`noLog.ts`) and `ROARR_LOG=1` (`log.ts`) states.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* eslint-disable max-nested-callbacks */
/* eslint-disable ava/use-test */

import { createLogger } from '../../src/factories/createLogger';
import { createRoarrInitialGlobalState } from '../../src/factories/createRoarrInitialGlobalState';
import { type Logger, type Message } from '../../src/types';
import test from 'ava';
import { createLogger } from '../../../src/factories/createLogger';
import { createRoarrInitialGlobalState } from '../../../src/factories/createRoarrInitialGlobalState';
import { type Logger, type Message } from '../../../src/types';
import { createIntegrationTest } from '../../helpers/createIntegrationTest';
import { setTimeout } from 'node:timers/promises';

const test = createIntegrationTest({
writeLogs: true,
});

const time = -1;
const version = '2.0.0';

Expand Down
12 changes: 8 additions & 4 deletions test/roarr/roarr.ts → test/roarr/integrations/roarr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { createLogger } from '../../src/factories/createLogger';
import { createRoarrInitialGlobalState } from '../../src/factories/createRoarrInitialGlobalState';
import { type Logger, type Message } from '../../src/types';
import test from 'ava';
import { createLogger } from '../../../src/factories/createLogger';
import { createRoarrInitialGlobalState } from '../../../src/factories/createRoarrInitialGlobalState';
import { type Logger, type Message } from '../../../src/types';
import { createIntegrationTest } from '../../helpers/createIntegrationTest';

const test = createIntegrationTest({
writeLogs: true,
});

const time = -1;
const version = '2.0.0';
Expand Down

0 comments on commit 074e361

Please sign in to comment.