diff --git a/lib/read-dependencies.js b/lib/read-dependencies.js index e962985..e30b6c4 100644 --- a/lib/read-dependencies.js +++ b/lib/read-dependencies.js @@ -7,9 +7,13 @@ const readDependencies = (options = []) => { options.forEach(option => args.push(`--${option}`)); - const result = childProcess.spawnSync('npm', args); + const result = childProcess.spawnSync('npm', args, { shell: true }); const tree = JSON.parse(result.stdout); + if (!tree) { + throw new Error('Failed to read dependencies'); + } + // mark root level -> we want to exclude this level from analysis tree.isRoot = true; diff --git a/lib/read-dependencies.test.js b/lib/read-dependencies.test.js index 6501f36..6f2a17b 100644 --- a/lib/read-dependencies.test.js +++ b/lib/read-dependencies.test.js @@ -41,7 +41,7 @@ describe('readDependencies', () => { it('calls child_process.spawnSync "npm ls --json"', () => { readDependencies(); - expect(childProcess.spawnSync).toHaveBeenCalledWith('npm', [ 'ls', '--json' ]); + expect(childProcess.spawnSync).toHaveBeenCalledWith('npm', [ 'ls', '--json' ], { shell: true }); }); it('returns parsed json returned by child process', () => { @@ -59,7 +59,7 @@ describe('readDependencies', () => { it('calls child_process.spawnSync "npm ls --json --production"', () => { readDependencies([ 'production' ]); - expect(childProcess.spawnSync).toHaveBeenCalledWith('npm', [ 'ls', '--json', '--production' ]); + expect(childProcess.spawnSync).toHaveBeenCalledWith('npm', [ 'ls', '--json', '--production' ], { shell: true }); }); it('returns parsed json returned by child process', () => { @@ -77,7 +77,7 @@ describe('readDependencies', () => { it('calls child_process.spawnSync "npm ls --json --development"', () => { readDependencies([ 'development' ]); - expect(childProcess.spawnSync).toHaveBeenCalledWith('npm', [ 'ls', '--json', '--development' ]); + expect(childProcess.spawnSync).toHaveBeenCalledWith('npm', [ 'ls', '--json', '--development' ], { shell: true }); }); it('returns parsed json returned by child process', () => { @@ -91,6 +91,12 @@ describe('readDependencies', () => { }); }); + it('throws when parsed tree is null', () => { + childProcess.spawnSync.mockImplementation(() => ({ stdout: null })); + + expect(readDependencies).toThrow('Failed to read dependencies'); + }); + it('returns problems found by npm ls', () => { childProcess.spawnSync.mockImplementation(() => ({ stdout: stringifiedTree, stderr: 'missing peer dependencies' }));