Skip to content

Commit 1e34f35

Browse files
committed
fix: remove event listeners in finally
1 parent 24c60c5 commit 1e34f35

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/shared/sandboxCommandBase.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,20 @@ export abstract class SandboxCommandBase<T> extends SfCommand<T> {
225225
}
226226
}
227227

228+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
229+
protected async finally(_: Error | undefined): Promise<any> {
230+
const lifecycle = Lifecycle.getInstance();
231+
lifecycle.removeAllListeners('POLLING_TIME_OUT');
232+
lifecycle.removeAllListeners(SandboxEvents.EVENT_RESUME);
233+
lifecycle.removeAllListeners(SandboxEvents.EVENT_ASYNC_RESULT);
234+
lifecycle.removeAllListeners(SandboxEvents.EVENT_STATUS);
235+
lifecycle.removeAllListeners(SandboxEvents.EVENT_AUTH);
236+
lifecycle.removeAllListeners(SandboxEvents.EVENT_RESULT);
237+
lifecycle.removeAllListeners(SandboxEvents.EVENT_MULTIPLE_SBX_PROCESSES);
238+
239+
return super.finally(_);
240+
}
241+
228242
private removeSandboxProgressConfig(): void {
229243
if (this.latestSandboxProgressObj?.SandboxName) {
230244
this.sandboxRequestConfig.unset(this.latestSandboxProgressObj.SandboxName);

test/unit/org/createSandbox.test.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ const fakeOrg: AuthFields = {
3737

3838
describe('org:create:sandbox', () => {
3939
beforeEach(() => {
40-
// stubMethod(sandbox, OrgAccessor.prototype, 'read').callsFake(async (): Promise<AuthFields> => fakeOrg);
41-
// stubMethod(sandbox, OrgAccessor.prototype, 'write').callsFake(async (): Promise<AuthFields> => fakeOrg);
4240
stubMethod(sandbox, OrgAccessor.prototype, 'read').resolves(fakeOrg);
4341
stubMethod(sandbox, OrgAccessor.prototype, 'write').resolves(fakeOrg);
4442
sfCommandUxStubs = stubSfCommandUx(sandbox);
@@ -53,23 +51,18 @@ describe('org:create:sandbox', () => {
5351
it('will print the correct message for asyncResult lifecycle event', async () => {
5452
stubMethod(sandbox, Org, 'create').resolves(Org.prototype);
5553
stubMethod(sandbox, Org.prototype, 'getUsername').returns('testProdOrg');
56-
const createStub = stubMethod(sandbox, Org.prototype, 'createSandbox').callsFake(async () =>
57-
(async () => {})().catch()
58-
);
54+
const createStub = stubMethod(sandbox, Org.prototype, 'createSandbox').callsFake(async () => {
55+
await Lifecycle.getInstance().emit(SandboxEvents.EVENT_ASYNC_RESULT, sandboxProcessObj);
56+
});
5957

6058
await CreateSandbox.run(['-o', 'testProdOrg', '--name', 'mysandboxx', '--no-prompt']);
6159

62-
expect(createStub.firstCall.args[0].SandboxName).includes('mysandboxx');
63-
expect(createStub.firstCall.args[0].SandboxName.length).equals(10);
64-
65-
Lifecycle.getInstance().on(SandboxEvents.EVENT_ASYNC_RESULT, async (result) => {
66-
expect(result).to.deep.equal(sandboxProcessObj);
67-
expect(sfCommandUxStubs.info.firstCall.firstArg).to.include(sandboxProcessObj.Id);
68-
return Promise.resolve();
60+
expect(createStub.firstCall.firstArg).to.deep.equal({
61+
SandboxName: 'mysandboxx',
62+
LicenseType: 'Developer',
6963
});
70-
71-
await Lifecycle.getInstance().emit(SandboxEvents.EVENT_ASYNC_RESULT, sandboxProcessObj);
72-
Lifecycle.getInstance().removeAllListeners(SandboxEvents.EVENT_ASYNC_RESULT);
64+
const expectedInfoMsg = `org resume sandbox --job-id ${sandboxProcessObj.Id} -o testProdOrg`;
65+
expect(sfCommandUxStubs.info.firstCall.firstArg).to.include(expectedInfoMsg);
7366
});
7467
});
7568

0 commit comments

Comments
 (0)