Skip to content

Commit

Permalink
Merge pull request #215 from mastrzyz/mastrzyz/fixGetOptions
Browse files Browse the repository at this point in the history
Fix `getOptions` support for Windows machines
  • Loading branch information
palmerj3 committed Jun 25, 2022
2 parents 48a6c13 + d739358 commit 27b4389
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
27 changes: 17 additions & 10 deletions __tests__/getOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{},
Expand All @@ -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'])
});
});

Expand Down
15 changes: 12 additions & 3 deletions utils/getOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down

0 comments on commit 27b4389

Please sign in to comment.