Skip to content

Commit

Permalink
Fix issue #58 cliOptions.fix is ignored (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
hassoubeat authored Aug 16, 2023
1 parent 251a6e3 commit 2d734a2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/runner/__tests__/runESLint.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* eslint-disable class-methods-use-this, global-require */
const path = require('path');

const runESLintRunnerWithMockedEngine = ({ mockOptions, runESLintOptions }) => {
const runESLintRunnerWithMockedEngine = ({
mockOptions,
runESLintOptions,
extraOptions,
}) => {
jest.resetModules();
jest.doMock('eslint', () => ({
ESLint: class {
Expand All @@ -18,11 +22,14 @@ const runESLintRunnerWithMockedEngine = ({ mockOptions, runESLintOptions }) => {
loadFormatter() {
return Promise.resolve({ format() {} });
}

// eslint-disable-next-line no-unused-vars
static outputFixes(report) {}
},
}));
const runESLint = require('../runESLint');

return runESLint({ extraOptions: {}, ...runESLintOptions });
return runESLint({ extraOptions: extraOptions || {}, ...runESLintOptions });
};

it('Requires the config setupTestFrameworkScriptFile when specified', async () => {
Expand Down Expand Up @@ -144,3 +151,21 @@ it('Returns "fail" when the test failed', async () => {
numPendingTests: 0,
});
});

it('Not to be override by undefined in extraOptions', async () => {
const result = await runESLintRunnerWithMockedEngine({
mockOptions: {
ignoredFiles: [],
errorCount: 0,
},
runESLintOptions: {
testPath: '/path/to/file.test.js',
config: {},
},
extraOptions: {
fix: true,
},
});

expect(result.cliOptions.fix).toBeTruthy();
});
13 changes: 12 additions & 1 deletion src/runner/runESLint.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const mkTestResults = ({
numPassingTests,
testPath,
assertionResults,
cliOptions,
}) => {
const startTime = new Date(start).getTime();
const endTime = new Date(end).getTime();
Expand Down Expand Up @@ -97,6 +98,7 @@ const mkTestResults = ({
status: result.status,
title: result.title,
})),
cliOptions,
};
};

Expand All @@ -122,6 +124,12 @@ const getComputedFixValue = ({ fix, quiet, fixDryRun }) => {
return undefined;
};

function removeUndefinedFromObject(object) {
return Object.fromEntries(
Object.entries(object).filter(([, value]) => typeof value !== 'undefined'),
);
}

const getESLintConstructor = async () => {
if (await shouldUseFlatConfig()) {
return FlatESLint;
Expand Down Expand Up @@ -162,7 +170,7 @@ const getCachedValues = async (config, extraOptions) => {
const cliOptions = {
...baseCliOptions,
fix: getComputedFixValue(baseCliOptions),
...extraOptions,
...removeUndefinedFromObject(extraOptions),
};

const ESLintConstructor = await getESLintConstructor();
Expand Down Expand Up @@ -238,6 +246,7 @@ const runESLint = async ({ testPath, config, extraOptions }) => {
numFailingTests: report[0].errorCount,
numPassingTests: 0,
assertionResults: mkAssertionResults(testPath, report),
cliOptions,
});
}

Expand All @@ -253,6 +262,7 @@ const runESLint = async ({ testPath, config, extraOptions }) => {
numFailingTests: 1,
numPassingTests: 0,
assertionResults: mkAssertionResults(testPath, report),
cliOptions,
});
}

Expand All @@ -268,6 +278,7 @@ const runESLint = async ({ testPath, config, extraOptions }) => {
status: 'passed',
},
],
cliOptions,
});

if (!cliOptions.quiet && report[0]?.warningCount > 0) {
Expand Down

0 comments on commit 2d734a2

Please sign in to comment.