Skip to content

Commit 00bef3e

Browse files
committed
refactor: wip tests
1 parent 9b6719c commit 00bef3e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/utils/src/lib/profiler/profiler.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ export class Profiler<T extends ActionTrackConfigs> {
8383
* @param options.track - Default track name for measurements
8484
* @param options.trackGroup - Default track group for organization
8585
* @param options.color - Default color for track entries
86-
* @param options.enabled - Whether profiling is enabled (defaults to false)
86+
* @param options.enabled - Whether profiling is enabled (defaults to CP_PROFILING env var)
8787
*
8888
*/
8989
constructor(options: ProfilerOptions<T>) {
90-
const { tracks, prefix, enabled = false, ...defaults } = options;
90+
const { tracks, prefix, enabled, ...defaults } = options;
9191
const dataType = 'track-entry';
9292

93-
this.#enabled = enabled;
93+
this.#enabled = enabled ?? isEnvVarEnabled(PROFILER_ENABLED_ENV_VAR);
9494
this.#defaults = { ...defaults, dataType };
9595
this.tracks = tracks
9696
? setupTracks({ ...defaults, dataType }, tracks)
@@ -105,11 +105,13 @@ export class Profiler<T extends ActionTrackConfigs> {
105105
/**
106106
* Sets enabled state for this profiler.
107107
*
108+
* Also sets the `CP_PROFILING` environment variable.
108109
* This means any future {@link Profiler} instantiations (including child processes) will use the same enabled state.
109110
*
110111
* @param enabled - Whether profiling should be enabled
111112
*/
112113
setEnabled(enabled: boolean): void {
114+
process.env[PROFILER_ENABLED_ENV_VAR] = `${enabled}`;
113115
this.#enabled = enabled;
114116
}
115117

packages/utils/src/lib/profiler/profiler.unit.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ describe('Profiler', () => {
127127
expect(profiler.isEnabled()).toBe(false);
128128
});
129129

130+
it('setEnabled should set environment variable and future instances should use it', () => {
131+
vi.stubEnv('CP_PROFILING', 'false');
132+
const profiler1 = getProfiler();
133+
134+
profiler1.setEnabled(true);
135+
136+
expect(profiler1.isEnabled()).toBeTrue();
137+
expect(process.env['CP_PROFILING']).toBe('true');
138+
expect(
139+
new Profiler({ prefix: 'cp', track: 'test-track' }).isEnabled(),
140+
).toBeTrue();
141+
});
142+
130143
it('marker should execute without error when enabled', () => {
131144
const enabledProfiler = getProfiler({ enabled: true });
132145
expect(() => {

0 commit comments

Comments
 (0)