Skip to content

Commit d633c8b

Browse files
B4nanclaude
andauthored
refactor: add Configuration.reset() static method (#3649)
## Summary - Adds `Configuration.reset()` as a discoverable static method on the new v4 `Configuration` class. Delegates to `serviceLocator.reset()` so the cached configuration, event manager, storage client, and logger are all dropped together. - Adds a test verifying that after `Configuration.reset()`, the next `Configuration.getGlobalConfig()` re-reads `process.env`. ## Motivation v4 `Configuration` resolves env vars eagerly at construction, so tests that mutate `process.env` after the global singleton has been resolved need a way to drop the cached instance. Today that's either `serviceLocator.reset()` (works, but the name suggests "service tree" rather than "the config I just changed env vars for") or reaching into the private `Configuration.globalConfig` via type assertions (currently happening across multiple test files in `apify-sdk-js`). A class-side `Configuration.reset()` makes the intent obvious from the call site and lives next to `Configuration.getGlobalConfig()` in autocomplete. The implementation just delegates to `serviceLocator.reset()` rather than resetting only the configuration — resetting only the config would leave a stale `EventManager` / `StorageClient` / logger holding the old config, which is the wrong contract for callers wanting to "start fresh". ## Test plan - [x] `vitest run packages/core/test/core/configuration.test.ts -t "reset"` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 20b320a commit d633c8b

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

packages/core/src/configuration.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ export class Configuration {
205205
return serviceLocator.getConfiguration();
206206
}
207207

208+
/**
209+
* Resets the global configuration (and the rest of the global service locator) so that the
210+
* next `Configuration.getGlobalConfig()` call constructs a fresh instance from the current
211+
* environment. Intended mainly for tests that mutate `process.env` at runtime — values are
212+
* resolved eagerly at construction, so changing env vars after the singleton has been
213+
* cached has no effect until the singleton is dropped.
214+
*
215+
* Delegates to {@link ServiceLocator.reset} since the global configuration is owned by the
216+
* service locator; resetting only the configuration would leave a stale `EventManager` /
217+
* `StorageClient` / logger holding the old config.
218+
*/
219+
static reset(): void {
220+
serviceLocator.reset();
221+
}
222+
208223
/**
209224
* Resolves all field values once using the priority chain:
210225
* constructor options > env vars > crawlee.json > schema defaults.

packages/core/test/core/configuration.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,18 @@ describe('Configuration', () => {
317317
expect((config2 as any).customFlag).toBe(true);
318318
});
319319
});
320+
321+
describe('reset()', () => {
322+
it('drops the cached global instance so the next `getGlobalConfig()` re-reads env vars', () => {
323+
setEnv('CRAWLEE_DEFAULT_DATASET_ID', 'first');
324+
expect(Configuration.getGlobalConfig().defaultDatasetId).toBe('first');
325+
326+
// Without resetting, the singleton keeps the value resolved at first access.
327+
setEnv('CRAWLEE_DEFAULT_DATASET_ID', 'second');
328+
expect(Configuration.getGlobalConfig().defaultDatasetId).toBe('first');
329+
330+
Configuration.reset();
331+
expect(Configuration.getGlobalConfig().defaultDatasetId).toBe('second');
332+
});
333+
});
320334
});

0 commit comments

Comments
 (0)