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 getOptions support for Windows machines #215

Merged
merged 7 commits into from Jun 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 17 additions & 10 deletions __tests__/getOptions.test.js
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
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