Skip to content

Commit

Permalink
fix(init): don't fail when there are no scripts (#99)
Browse files Browse the repository at this point in the history
**What**: Closes #89

**Why**: Because we want to be able to init even if you have no
`scripts` in your `package.json`

**How**: ES6 magic
  • Loading branch information
Kent C. Dodds authored Feb 9, 2017
1 parent 033bbda commit b80aa2b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/bin-utils/initialize/fixtures/_package-no-scripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 1 addition & 1 deletion src/bin-utils/initialize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function initialize(configType = 'js') {
/* eslint global-require:0,import/no-dynamic-require:0 */
const packageJsonPath = findUpSync('package.json')
const packageJson = require(packageJsonPath)
const {scripts} = packageJson
const {scripts = {}} = packageJson
packageJson.scripts = {
start: 'nps',
test: scripts.test ? 'nps test' : undefined,
Expand Down
23 changes: 23 additions & 0 deletions src/bin-utils/initialize/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,26 @@ test('initialize without a test script should not add a test to the package.json
start: 'nps',
})
})

test('initialize without any scripts should successfully create an empty package-scripts.js file', () => {
const packageJsonDestination = resolve('./src/bin-utils/initialize/fixtures/_package-no-scripts.json')
const mockWriteFileSync = spy()
const mockFindUpSync = spy(file => {
if (file === 'package.json') {
return packageJsonDestination
}
throw new Error('Should not look for anything but package.json')
})
jest.resetModules()
jest.mock('find-up', () => ({sync: mockFindUpSync}))
jest.mock('fs', () => ({writeFileSync: mockWriteFileSync}))
const initialize = require('./index').default

initialize()
const [, packageJsonStringResult] = mockWriteFileSync.firstCall.args
const {scripts: packageJsonScripts} = JSON.parse(packageJsonStringResult)

expect(packageJsonScripts).toEqual({
start: 'nps',
})
})

0 comments on commit b80aa2b

Please sign in to comment.