diff --git a/jest.config.js b/jest.config.js index b7568c554..878a1825a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,11 @@ const config = { collectCoverage: true, - collectCoverageFrom: ['lib/**/*.js'], + collectCoverageFrom: [ + 'lib/**/*.js', + // Avoid ESM import.meta parse error. + // (Can't measure coverage anyway, it's always mocked) + '!lib/resolveConfig.js', + ], moduleDirectories: ['node_modules'], setupFiles: ['./testSetup.js'], snapshotSerializers: ['jest-snapshot-serializer-ansi'], diff --git a/lib/loadConfig.js b/lib/loadConfig.js index 76493447d..626a6da02 100644 --- a/lib/loadConfig.js +++ b/lib/loadConfig.js @@ -6,6 +6,8 @@ import debug from 'debug' import { lilconfig } from 'lilconfig' import YAML from 'yaml' +import { resolveConfig } from './resolveConfig' + const debugLog = debug('lint-staged:loadConfig') /** @@ -49,14 +51,6 @@ const loaders = { noExt: yamlParse, } -const resolveConfig = (configPath) => { - try { - return require.resolve(configPath) - } catch { - return configPath - } -} - /** * @param {object} options * @param {string} [options.configPath] - Explicit path to a config file diff --git a/lib/resolveConfig.js b/lib/resolveConfig.js new file mode 100644 index 000000000..0b9f357c1 --- /dev/null +++ b/lib/resolveConfig.js @@ -0,0 +1,15 @@ +import { createRequire } from 'module' + +/** + * require() does not exist for ESM, so we must create it to use require.resolve(). + * @see https://nodejs.org/api/module.html#modulecreaterequirefilename + */ +const require = createRequire(import.meta.url) + +export function resolveConfig(configPath) { + try { + return require.resolve(configPath) + } catch { + return configPath + } +} diff --git a/test/index.spec.js b/test/index.spec.js index 004ef7444..81bd5193d 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -28,6 +28,16 @@ jest.mock('url', () => ({ jest.mock('../lib/getStagedFiles') jest.mock('../lib/gitWorkflow') +jest.mock('../lib/resolveConfig', () => ({ + /** Unfortunately necessary due to non-ESM tests. */ + resolveConfig: (configPath) => { + try { + return require.resolve(configPath) + } catch { + return configPath + } + }, +})) jest.mock('../lib/validateOptions', () => ({ validateOptions: jest.fn().mockImplementation(async () => {}), })) diff --git a/test/index2.spec.js b/test/index2.spec.js index 8712feff5..41de135f3 100644 --- a/test/index2.spec.js +++ b/test/index2.spec.js @@ -4,6 +4,17 @@ import { Listr } from 'listr2' import makeConsoleMock from 'consolemock' jest.mock('listr2') +jest.mock('../lib/resolveConfig', () => ({ + /** Unfortunately necessary due to non-ESM tests. */ + resolveConfig: (configPath) => { + try { + return require.resolve(configPath) + } catch { + return configPath + } + }, +})) + jest.mock('../lib/resolveGitRepo') import lintStaged from '../lib/index' diff --git a/test/integration.test.js b/test/integration.test.js index cc9c2da11..1048f8148 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -7,6 +7,16 @@ import normalize from 'normalize-path' jest.unmock('lilconfig') jest.unmock('execa') +jest.mock('../lib/resolveConfig', () => ({ + /** Unfortunately necessary due to non-ESM tests. */ + resolveConfig: (configPath) => { + try { + return require.resolve(configPath) + } catch { + return configPath + } + }, +})) import { execGit as execGitBase } from '../lib/execGit' import lintStaged from '../lib/index' diff --git a/test/loadConfig.spec.js b/test/loadConfig.spec.js index 33272e610..2123012ae 100644 --- a/test/loadConfig.spec.js +++ b/test/loadConfig.spec.js @@ -1,3 +1,14 @@ +jest.mock('../lib/resolveConfig', () => ({ + /** Unfortunately necessary due to non-ESM tests. */ + resolveConfig: (configPath) => { + try { + return require.resolve(configPath) + } catch { + return configPath + } + }, +})) + import { dynamicImport } from '../lib/loadConfig.js' describe('dynamicImport', () => {