Skip to content

Commit

Permalink
fix: Resolve config modules with ESM createRequire()
Browse files Browse the repository at this point in the history
ES modules do not have require() available, we must construct it first.
This fixes the CLI when passing `--config my-config-package`, broken in
v12.0.0.

The tests didn't catch this because Jest still doesn't support mocking
ESM, and there's really no way to write a test for this right now.

I wasn't able to get the automatic `test/__mocks__/resolveConfig.js` to
work, probably a race condition between the module parser and the mocker.
  • Loading branch information
evocateur committed Jan 6, 2022
1 parent c7ea359 commit 4d98715
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 9 deletions.
7 changes: 6 additions & 1 deletion 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'],
Expand Down
10 changes: 2 additions & 8 deletions lib/loadConfig.js
Expand Up @@ -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')

/**
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions 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
}
}
10 changes: 10 additions & 0 deletions test/index.spec.js
Expand Up @@ -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 () => {}),
}))
Expand Down
11 changes: 11 additions & 0 deletions test/index2.spec.js
Expand Up @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions test/integration.test.js
Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions 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', () => {
Expand Down

0 comments on commit 4d98715

Please sign in to comment.