diff --git a/src/runner/__tests__/runESLint.test.js b/src/runner/__tests__/runESLint.test.js index 26fbdbf..1e16d21 100644 --- a/src/runner/__tests__/runESLint.test.js +++ b/src/runner/__tests__/runESLint.test.js @@ -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 { @@ -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 () => { @@ -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(); +}); diff --git a/src/runner/runESLint.js b/src/runner/runESLint.js index 144bb25..173d951 100644 --- a/src/runner/runESLint.js +++ b/src/runner/runESLint.js @@ -56,6 +56,7 @@ const mkTestResults = ({ numPassingTests, testPath, assertionResults, + cliOptions, }) => { const startTime = new Date(start).getTime(); const endTime = new Date(end).getTime(); @@ -97,6 +98,7 @@ const mkTestResults = ({ status: result.status, title: result.title, })), + cliOptions, }; }; @@ -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; @@ -162,7 +170,7 @@ const getCachedValues = async (config, extraOptions) => { const cliOptions = { ...baseCliOptions, fix: getComputedFixValue(baseCliOptions), - ...extraOptions, + ...removeUndefinedFromObject(extraOptions), }; const ESLintConstructor = await getESLintConstructor(); @@ -238,6 +246,7 @@ const runESLint = async ({ testPath, config, extraOptions }) => { numFailingTests: report[0].errorCount, numPassingTests: 0, assertionResults: mkAssertionResults(testPath, report), + cliOptions, }); } @@ -253,6 +262,7 @@ const runESLint = async ({ testPath, config, extraOptions }) => { numFailingTests: 1, numPassingTests: 0, assertionResults: mkAssertionResults(testPath, report), + cliOptions, }); } @@ -268,6 +278,7 @@ const runESLint = async ({ testPath, config, extraOptions }) => { status: 'passed', }, ], + cliOptions, }); if (!cliOptions.quiet && report[0]?.warningCount > 0) {