Skip to content

Commit ddca423

Browse files
HypercubedKent C. Dodds
authored and
Kent C. Dodds
committed
fix: fix parser bug, add test. (#183)
* fix: fix parser bug, add test * Skip condition path in testing
1 parent e52b136 commit ddca423

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/bin-utils/__tests__/parser.js

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ jest.mock('../../bin-utils', () => {
2121
return defaultPsConfig
2222
}),
2323
initialize: jest.fn(() => {
24+
if (mockFindUp.mock.syncFail) {
25+
return undefined
26+
}
2427
return {
2528
packageScriptsPath: '/path/to/package-scripts.js',
2629
packageJsonPath: '/path/to/package.json',
@@ -196,6 +199,22 @@ test('init without an existing config will initialize package-scripts.js', () =>
196199
delete mockFindUp.mock.syncReturn
197200
})
198201

202+
test('init without an existing package.json will fail', () => {
203+
mockFindUp.mock.syncFail = true
204+
mockFindUp.mock.syncReturn = undefined
205+
206+
const result = parse('init')
207+
expect(result).toBe(undefined)
208+
expect(mockReadLine.keyInYN).toHaveBeenCalledTimes(0)
209+
expect(mockGetLogger.mock.error).toHaveBeenCalledWith(
210+
expect.stringMatching(/Unable/),
211+
)
212+
213+
delete mockFindUp.mock.syncFail
214+
delete mockReadLine.mock.keyInYNReturn
215+
delete mockFindUp.mock.syncReturn
216+
})
217+
199218
test('if there is a help script in the psConfig, does not show the help', () => {
200219
mockBinUtils.mock.psConfig = {scripts: {help: 'hi'}, options: {}}
201220
expect(parse('help')).not.toBe(undefined)

src/bin-utils/initialize/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const CORE_SCRIPTS = [
6060
function initialize(configType = 'js') {
6161
/* eslint global-require:0,import/no-dynamic-require:0 */
6262
const packageJsonPath = findUpSync('package.json')
63+
/* istanbul ignore next */
6364
if (packageJsonPath === null) {
6465
return
6566
}

src/bin-utils/parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function parse(rawArgv) {
186186
}
187187
}
188188
const init = initialize(initArgv.type)
189-
if (!packageScriptsPath) {
189+
if (!init) {
190190
log.error(chalk.red('Unable to to find an existing package.json'))
191191
return
192192
}

0 commit comments

Comments
 (0)