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(init): don't fail when there are no scripts #99

Merged
merged 1 commit into from
Feb 9, 2017
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
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',
})
})