Skip to content

Commit

Permalink
fix: print cli errors to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc committed Sep 14, 2023
1 parent 565474c commit 3190048
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/cliCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function printNetworkError(config) {
* @returns {void}
*/
function printCliResultError(error) {
console.log(`\n\n${'not ok'.red} -- ${error || 'Unknown error'}\n`);
console.error(`\n\n${'not ok'.red} -- ${error || 'Unknown error'}\n`);

if (error && Array.isArray(error.messages)) {
for (const item of error.messages) {
Expand Down
22 changes: 12 additions & 10 deletions lib/cliCommon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const { printCliResultError, messages } = require('./cliCommon');
describe('cliCommon', () => {
describe('printCliResultError', () => {
let consoleLogStub;
let consoleErrorStub;

beforeAll(() => {
consoleLogStub = jest.spyOn(console, 'log').mockImplementation(jest.fn());
consoleErrorStub = jest.spyOn(console, 'error').mockImplementation(jest.fn());
});

afterEach(() => {
Expand All @@ -21,8 +23,8 @@ describe('cliCommon', () => {
it('should log "Unknown error" and general recommendations if input is empty', () => {
printCliResultError(null);

expect(consoleLogStub).toHaveBeenCalledTimes(3);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching('Unknown error'));
expect(consoleLogStub).toHaveBeenCalledTimes(2);
expect(consoleErrorStub).toHaveBeenCalledWith(expect.stringMatching('Unknown error'));
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue);
});
Expand All @@ -32,8 +34,8 @@ describe('cliCommon', () => {

printCliResultError(err);

expect(consoleLogStub).toHaveBeenCalledTimes(3);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledTimes(2);
expect(consoleErrorStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue);
});
Expand All @@ -43,8 +45,8 @@ describe('cliCommon', () => {

printCliResultError(errStr);

expect(consoleLogStub).toHaveBeenCalledTimes(3);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(errStr));
expect(consoleLogStub).toHaveBeenCalledTimes(2);
expect(consoleErrorStub).toHaveBeenCalledWith(expect.stringMatching(errStr));
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue);
});
Expand All @@ -55,8 +57,8 @@ describe('cliCommon', () => {

printCliResultError(err);

expect(consoleLogStub).toHaveBeenCalledTimes(5);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledTimes(4);
expect(consoleErrorStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledWith(`${err.messages[0].message.red}\n`);
expect(consoleLogStub).toHaveBeenCalledWith(`${err.messages[1].message.red}\n`);
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
Expand All @@ -77,8 +79,8 @@ describe('cliCommon', () => {

printCliResultError(err);

expect(consoleLogStub).toHaveBeenCalledTimes(5);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledTimes(4);
expect(consoleErrorStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledWith(`${'first_error'.red}\n`);
expect(consoleLogStub).toHaveBeenCalledWith(`${'2nd_error'.red}\n`);
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
Expand Down

0 comments on commit 3190048

Please sign in to comment.