Skip to content

Commit 7b0a5b1

Browse files
authored
fix: remove noisy implicit configuration warning from service locator (#3837)
Fixes #3582
1 parent c6f3256 commit 7b0a5b1

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

packages/basic-crawler/src/internals/basic-crawler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,9 @@ export class BasicCrawler<
823823

824824
this.#log = serviceLocator.getLogger().child({ prefix: this.constructor.name });
825825

826+
// Initialize the Configuration instance to avoid lazy loading in the components
827+
serviceLocator.getConfiguration();
828+
826829
// Store whether the user explicitly provided an ID
827830
this.hasExplicitId = id !== undefined;
828831
// Store the user-provided ID, or generate a unique one for tracking purposes (not for state key)

packages/core/test/core/service_locator.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ describe('ServiceLocator', () => {
128128
serviceLocator.setEventManager(customEventManager);
129129
}).toThrow(/EventManager is already in use/);
130130
});
131+
132+
test('warns about implicit configuration when configuration was not set beforehand', () => {
133+
const warningSpy = vi.fn();
134+
serviceLocator.setLogger(makeMockLogger({ warning: warningSpy }));
135+
136+
serviceLocator.getEventManager();
137+
138+
expect(warningSpy).toHaveBeenCalledWith(expect.stringMatching(/implicitly set configuration/));
139+
});
140+
141+
test('does not warn about implicit configuration when configuration was already set', () => {
142+
const warningSpy = vi.fn();
143+
serviceLocator.setLogger(makeMockLogger({ warning: warningSpy }));
144+
serviceLocator.setConfiguration(new Configuration());
145+
146+
serviceLocator.getEventManager();
147+
148+
expect(warningSpy).not.toHaveBeenCalled();
149+
});
131150
});
132151

133152
describe('StorageBackend', () => {
@@ -168,6 +187,25 @@ describe('ServiceLocator', () => {
168187
serviceLocator.setStorageBackend(customStorageBackend);
169188
}).toThrow(/StorageBackend is already in use/);
170189
});
190+
191+
test('warns about implicit configuration when configuration was not set beforehand', () => {
192+
const warningSpy = vi.fn();
193+
serviceLocator.setLogger(makeMockLogger({ warning: warningSpy }));
194+
195+
serviceLocator.getStorageBackend();
196+
197+
expect(warningSpy).toHaveBeenCalledWith(expect.stringMatching(/implicitly set configuration/));
198+
});
199+
200+
test('does not warn about implicit configuration when configuration was already set', () => {
201+
const warningSpy = vi.fn();
202+
serviceLocator.setLogger(makeMockLogger({ warning: warningSpy }));
203+
serviceLocator.setConfiguration(new Configuration());
204+
205+
serviceLocator.getStorageBackend();
206+
207+
expect(warningSpy).not.toHaveBeenCalled();
208+
});
171209
});
172210

173211
describe('Logger', () => {

test/core/crawlers/basic_crawler.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ describe('BasicCrawler', () => {
7777
server.close();
7878
});
7979

80+
test('constructor eagerly resolves the configuration, avoiding a later implicit-configuration warning', () => {
81+
serviceLocator.reset();
82+
const warningSpy = vitest.spyOn(serviceLocator.getLogger(), 'warning');
83+
84+
new BasicCrawler({ requestHandler: async () => {} });
85+
serviceLocator.getStorageBackend();
86+
87+
expect(warningSpy).not.toHaveBeenCalledWith(expect.stringMatching(/implicitly set configuration/));
88+
89+
// restore the shared storage backend expected by the other tests' beforeEach
90+
serviceLocator.reset();
91+
serviceLocator.setStorageBackend(new MemoryStorageBackend());
92+
});
93+
8094
test('does not leak sigint events', async () => {
8195
let count = 0;
8296

0 commit comments

Comments
 (0)