Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure afterAllSetup is called when using addIntegration() #10372

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,14 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
* @inheritDoc
*/
public addIntegration(integration: Integration): void {
const isAlreadyInstalled = this._integrations[integration.name];

// This hook takes care of only installing if not already installed
setupIntegration(this, integration, this._integrations);
// Here we need to check manually to make sure to not run this multiple times
if (!isAlreadyInstalled) {
afterSetupIntegrations(this, [integration]);
}
}

/**
Expand Down
57 changes: 57 additions & 0 deletions packages/core/test/lib/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,63 @@ describe('addIntegration', () => {
expect(warnings).toHaveBeenCalledTimes(1);
expect(warnings).toHaveBeenCalledWith('Cannot add integration "test" because no SDK Client is available.');
});

it('triggers all hooks', () => {
const setup = jest.fn();
const setupOnce = jest.fn();
const setupAfterAll = jest.fn();

class CustomIntegration implements Integration {
name = 'test';
setupOnce = setupOnce;
setup = setup;
afterAllSetup = setupAfterAll;
}

const client = getTestClient();
const hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);

const integration = new CustomIntegration();
addIntegration(integration);

expect(setupOnce).toHaveBeenCalledTimes(1);
expect(setup).toHaveBeenCalledTimes(1);
expect(setupAfterAll).toHaveBeenCalledTimes(1);
});

it('does not trigger hooks if already installed', () => {
const logs = jest.spyOn(logger, 'log');

class CustomIntegration implements Integration {
name = 'test';
setupOnce = jest.fn();
setup = jest.fn();
afterAllSetup = jest.fn();
}

const client = getTestClient();
const hub = new Hub(client);
// eslint-disable-next-line deprecation/deprecation
makeMain(hub);

const integration1 = new CustomIntegration();
const integration2 = new CustomIntegration();
addIntegration(integration1);

expect(integration1.setupOnce).toHaveBeenCalledTimes(1);
expect(integration1.setup).toHaveBeenCalledTimes(1);
expect(integration1.afterAllSetup).toHaveBeenCalledTimes(1);

addIntegration(integration2);

expect(integration2.setupOnce).toHaveBeenCalledTimes(0);
expect(integration2.setup).toHaveBeenCalledTimes(0);
expect(integration2.afterAllSetup).toHaveBeenCalledTimes(0);

expect(logs).toHaveBeenCalledWith('Integration skipped because it was already installed: test');
});
});

describe('convertIntegrationFnToClass', () => {
Expand Down