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: stop spinners on caught error #407

Merged
merged 2 commits into from
Oct 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ export abstract class SfCommand<T> extends Command {

// eslint-disable-next-line @typescript-eslint/require-await
protected async catch(error: Error | SfError | SfCommand.Error): Promise<SfCommand.Error> {
// stop any spinners to prevent it from unintentionally swallowing output.
// If there is an active spinner, it'll say "Error" instead of "Done"
this.spinner.stop(StandardColors.error('Error'));
// transform an unknown error into one that conforms to the interface

// @ts-expect-error because exitCode is not on Error
Expand Down
48 changes: 45 additions & 3 deletions test/unit/sfCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { Flags } from '@oclif/core';
import { Lifecycle } from '@salesforce/core';
import { TestContext } from '@salesforce/core/lib/testSetup';
import { stubMethod } from '@salesforce/ts-sinon';
import { expect } from 'chai';
import { assert, expect } from 'chai';
import { SfError } from '@salesforce/core';
import { Config } from '@oclif/core/lib/interfaces';
import { SfCommand } from '../../src/sfCommand';

import { SfCommand, StandardColors } from '../../src/sfCommand';
import { stubSfCommandUx, stubSpinner } from '../../src/stubUx';
class TestCommand extends SfCommand<void> {
public static readonly flags = {
actions: Flags.boolean({ char: 'a', description: 'show actions' }),
Expand Down Expand Up @@ -49,6 +49,7 @@ class NonJsonCommand extends SfCommand<void> {
await this.parse(TestCommand);
}
}

describe('jsonEnabled', () => {
beforeEach(() => {
delete process.env.SF_CONTENT_TYPE;
Expand Down Expand Up @@ -169,3 +170,44 @@ describe('warning messages', () => {
.and.to.include('action');
});
});

describe('spinner stops on errors', () => {
const $$ = new TestContext();

class SpinnerThrow extends SfCommand<void> {
// public static enableJsonFlag = true;
public static flags = {
throw: Flags.boolean(),
};
public async run(): Promise<void> {
const { flags } = await this.parse(SpinnerThrow);
this.spinner.start('go');
if (flags.throw) {
throw new Error('boo');
}
}
}

it("spinner stops but stop isn't called", async () => {
const spinnerStub = stubSpinner($$.SANDBOX);
stubSfCommandUx($$.SANDBOX);
try {
await SpinnerThrow.run(['--throw']);
throw new Error('should have thrown');
} catch (e) {
assert(e instanceof Error);
expect(e.message).to.equal('boo');
expect(spinnerStub.start.callCount).to.equal(1);
expect(spinnerStub.stop.callCount).to.equal(1);
expect(spinnerStub.stop.firstCall.firstArg).to.equal(StandardColors.error('Error'));
}
});
it('spinner not stopped when no throw', async () => {
const spinnerStub = stubSpinner($$.SANDBOX);
stubSfCommandUx($$.SANDBOX);
await SpinnerThrow.run([]);

expect(spinnerStub.start.callCount).to.equal(1);
expect(spinnerStub.stop.callCount).to.equal(0);
});
});
Loading