-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move printErrorMessages() from theme-api-client.js to cliCo…
…mmon.js
- Loading branch information
MaxGenash
committed
Sep 21, 2020
1 parent
6dcdd9b
commit d2c259b
Showing
7 changed files
with
121 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const messages = { | ||
visitTroubleshootingPage: 'Please visit the troubleshooting page https://developer.bigcommerce.com/stencil-docs/deploying-a-theme/troubleshooting-theme-uploads.', | ||
submitGithubIssue: 'If this error persists, please visit https:/bigcommerce/stencil-cli/issues and submit an issue.', | ||
}; | ||
|
||
/** | ||
* @param {Error} error | ||
* @param {Array<{message: string}>} [error.messages] | ||
* @returns {void} | ||
*/ | ||
function printCliResultError (error) { | ||
console.log('\n\n' + 'not ok'.red + ` -- ` + (error || 'Unknown error') + '\n'); | ||
|
||
if (error && Array.isArray(error.messages)) { | ||
for (let item of error.messages) { | ||
if (item && item.message) { | ||
console.log(item.message.red + '\n'); | ||
} | ||
} | ||
} | ||
|
||
console.log(messages.visitTroubleshootingPage); | ||
|
||
console.log(messages.submitGithubIssue); | ||
} | ||
|
||
module.exports = { | ||
printCliResultError, | ||
messages, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
require('colors'); | ||
|
||
const { printCliResultError, messages } = require('./cliCommon'); | ||
|
||
describe('cliCommon', () => { | ||
describe('printCliResultError', () => { | ||
let consoleLogStub; | ||
|
||
beforeAll(() => { | ||
consoleLogStub = jest.spyOn(console, 'log').mockImplementation(jest.fn()); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
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).toHaveBeenCalledWith(messages.visitTroubleshootingPage); | ||
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue); | ||
}); | ||
|
||
it('should log the passed error and general recommendations if input is a plain Error object with no extra messages', () => { | ||
const err = new Error('test error'); | ||
|
||
printCliResultError(err); | ||
|
||
expect(consoleLogStub).toHaveBeenCalledTimes(3); | ||
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(err.toString())); | ||
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage); | ||
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue); | ||
}); | ||
|
||
it('should log the passed message and general recommendations if input is a string', () => { | ||
const errStr = 'test error message'; | ||
|
||
printCliResultError(errStr); | ||
|
||
expect(consoleLogStub).toHaveBeenCalledTimes(3); | ||
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(errStr)); | ||
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage); | ||
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue); | ||
}); | ||
|
||
|
||
it('should log the error, each field in error.messages and general recommendations if input is an object with error.messages field', () => { | ||
const err = new Error('test error'); | ||
err.messages = [{'message': 'first_error'}, {'message': '2nd_error'}]; | ||
|
||
printCliResultError(err); | ||
|
||
expect(consoleLogStub).toHaveBeenCalledTimes(5); | ||
expect(consoleLogStub).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); | ||
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue); | ||
}); | ||
|
||
it('should skip non object elements in the error.message array', () => { | ||
const err = new Error('test error'); | ||
err.messages = [{'message': 'first_error'}, 'string', {'message': '2nd_error'}, undefined, null, 228, true]; | ||
|
||
printCliResultError(err); | ||
|
||
expect(consoleLogStub).toHaveBeenCalledTimes(5); | ||
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(err.toString())); | ||
expect(consoleLogStub).toHaveBeenCalledWith('first_error'.red + '\n'); | ||
expect(consoleLogStub).toHaveBeenCalledWith('2nd_error'.red + '\n'); | ||
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage); | ||
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.