Skip to content

Commit

Permalink
fix: fix parser bug, add test. (#183)
Browse files Browse the repository at this point in the history
* fix: fix parser bug, add test

* Skip condition path in testing
  • Loading branch information
Hypercubed authored and Kent C. Dodds committed Jun 23, 2018
1 parent e52b136 commit ddca423
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/bin-utils/__tests__/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jest.mock('../../bin-utils', () => {
return defaultPsConfig
}),
initialize: jest.fn(() => {
if (mockFindUp.mock.syncFail) {
return undefined
}
return {
packageScriptsPath: '/path/to/package-scripts.js',
packageJsonPath: '/path/to/package.json',
Expand Down Expand Up @@ -196,6 +199,22 @@ test('init without an existing config will initialize package-scripts.js', () =>
delete mockFindUp.mock.syncReturn
})

test('init without an existing package.json will fail', () => {
mockFindUp.mock.syncFail = true
mockFindUp.mock.syncReturn = undefined

const result = parse('init')
expect(result).toBe(undefined)
expect(mockReadLine.keyInYN).toHaveBeenCalledTimes(0)
expect(mockGetLogger.mock.error).toHaveBeenCalledWith(
expect.stringMatching(/Unable/),
)

delete mockFindUp.mock.syncFail
delete mockReadLine.mock.keyInYNReturn
delete mockFindUp.mock.syncReturn
})

test('if there is a help script in the psConfig, does not show the help', () => {
mockBinUtils.mock.psConfig = {scripts: {help: 'hi'}, options: {}}
expect(parse('help')).not.toBe(undefined)
Expand Down
1 change: 1 addition & 0 deletions src/bin-utils/initialize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const CORE_SCRIPTS = [
function initialize(configType = 'js') {
/* eslint global-require:0,import/no-dynamic-require:0 */
const packageJsonPath = findUpSync('package.json')
/* istanbul ignore next */
if (packageJsonPath === null) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin-utils/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function parse(rawArgv) {
}
}
const init = initialize(initArgv.type)
if (!packageScriptsPath) {
if (!init) {
log.error(chalk.red('Unable to to find an existing package.json'))
return
}
Expand Down

0 comments on commit ddca423

Please sign in to comment.