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: print cli errors to stderr #1133

Merged
merged 1 commit into from
Sep 14, 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
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
Loading