diff --git a/__tests__/getOptions.test.js b/__tests__/getOptions.test.js index be44020..4b215af 100644 --- a/__tests__/getOptions.test.js +++ b/__tests__/getOptions.test.js @@ -3,6 +3,18 @@ const path = require('path'); const getOptions = require('../utils/getOptions.js'); + +const rootPath = path.parse(process.cwd()).root +const rootPackageJsonMock = path.join(rootPath, "package.json"); + +const mockPackageJson = { + name: 'foo', + version: '1.0.0', + 'jest-junit': { + suiteName: 'test suite' + } +} + jest.mock('fs', () => { return Object.assign( {}, @@ -15,19 +27,14 @@ jest.mock('fs', () => { // Mock return of require('/package.json') // Virtual because it doesn't actually exist -jest.mock('/package.json', () => { - return { - name: 'foo', - version: '1.0.0', - 'jest-junit': { - suiteName: 'test suite' - } - } +jest.doMock(rootPackageJsonMock, () => { + return mockPackageJson }, {virtual: true}); describe('getOptions', () => { - it ('should support package.json in /', () => { - + it ('should support package.json in root directory', () => { + const options = getOptions.getAppOptions(rootPath) + expect(options).toBe(mockPackageJson['jest-junit']) }); }); diff --git a/utils/getOptions.js b/utils/getOptions.js index 98da615..b7e5a5a 100644 --- a/utils/getOptions.js +++ b/utils/getOptions.js @@ -23,14 +23,23 @@ function getEnvOptions() { function getAppOptions(pathToResolve) { let traversing = true; - // Find nearest package.json by traversing up directories until / + // Get the root dir to detect when we reached the end to our search + const rootDir = path.parse(pathToResolve).root + + // Find nearest package.json by traversing up directories until root while(traversing) { - traversing = pathToResolve !== path.sep; + traversing = pathToResolve !== rootDir; const pkgpath = path.join(pathToResolve, 'package.json'); if (fs.existsSync(pkgpath)) { - let options = (require(pkgpath) || {})['jest-junit']; + let options; + + try { + options = (require(pkgpath) || {})['jest-junit']; + } catch (error) { + console.warn(`Unable to import package.json to get app Options : ${error}`) + } if (Object.prototype.toString.call(options) !== '[object Object]') { options = {};