Skip to content

Commit 9f9c51d

Browse files
authored
fix: Ensure afterAllSetup is called when using addIntegration() (#10372)
Noticed that this is not really correct right now!
1 parent dd77951 commit 9f9c51d

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

packages/core/src/baseclient.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,14 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
368368
* @inheritDoc
369369
*/
370370
public addIntegration(integration: Integration): void {
371+
const isAlreadyInstalled = this._integrations[integration.name];
372+
373+
// This hook takes care of only installing if not already installed
371374
setupIntegration(this, integration, this._integrations);
375+
// Here we need to check manually to make sure to not run this multiple times
376+
if (!isAlreadyInstalled) {
377+
afterSetupIntegrations(this, [integration]);
378+
}
372379
}
373380

374381
/**

packages/core/test/lib/integration.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,63 @@ describe('addIntegration', () => {
646646
expect(warnings).toHaveBeenCalledTimes(1);
647647
expect(warnings).toHaveBeenCalledWith('Cannot add integration "test" because no SDK Client is available.');
648648
});
649+
650+
it('triggers all hooks', () => {
651+
const setup = jest.fn();
652+
const setupOnce = jest.fn();
653+
const setupAfterAll = jest.fn();
654+
655+
class CustomIntegration implements Integration {
656+
name = 'test';
657+
setupOnce = setupOnce;
658+
setup = setup;
659+
afterAllSetup = setupAfterAll;
660+
}
661+
662+
const client = getTestClient();
663+
const hub = new Hub(client);
664+
// eslint-disable-next-line deprecation/deprecation
665+
makeMain(hub);
666+
667+
const integration = new CustomIntegration();
668+
addIntegration(integration);
669+
670+
expect(setupOnce).toHaveBeenCalledTimes(1);
671+
expect(setup).toHaveBeenCalledTimes(1);
672+
expect(setupAfterAll).toHaveBeenCalledTimes(1);
673+
});
674+
675+
it('does not trigger hooks if already installed', () => {
676+
const logs = jest.spyOn(logger, 'log');
677+
678+
class CustomIntegration implements Integration {
679+
name = 'test';
680+
setupOnce = jest.fn();
681+
setup = jest.fn();
682+
afterAllSetup = jest.fn();
683+
}
684+
685+
const client = getTestClient();
686+
const hub = new Hub(client);
687+
// eslint-disable-next-line deprecation/deprecation
688+
makeMain(hub);
689+
690+
const integration1 = new CustomIntegration();
691+
const integration2 = new CustomIntegration();
692+
addIntegration(integration1);
693+
694+
expect(integration1.setupOnce).toHaveBeenCalledTimes(1);
695+
expect(integration1.setup).toHaveBeenCalledTimes(1);
696+
expect(integration1.afterAllSetup).toHaveBeenCalledTimes(1);
697+
698+
addIntegration(integration2);
699+
700+
expect(integration2.setupOnce).toHaveBeenCalledTimes(0);
701+
expect(integration2.setup).toHaveBeenCalledTimes(0);
702+
expect(integration2.afterAllSetup).toHaveBeenCalledTimes(0);
703+
704+
expect(logs).toHaveBeenCalledWith('Integration skipped because it was already installed: test');
705+
});
649706
});
650707

651708
describe('convertIntegrationFnToClass', () => {

0 commit comments

Comments
 (0)