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 issue #58 cliOptions.fix is ignored #200

Merged
merged 6 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 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,16 @@ const runESLintRunnerWithMockedEngine = ({ mockOptions, runESLintOptions }) => {
loadFormatter() {
return Promise.resolve({ format() {} });
}

// eslint-disable-next-line no-unused-vars
static outputFixes(report) {
return Promise.resolve();
}
hassoubeat marked this conversation as resolved.
Show resolved Hide resolved
},
}));
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 +153,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