bug: ESLint cannot be imported in Jest 27
nickserv opened this issue · comments
kcd-scripts
version: 11.2.0node
version: 16.9.1npm
(oryarn
) version: 7.23.0jest
version: 27.2.0
Relevant code or config
import 'eslint'
// or
require('eslint')
What you did: kcd-scripts test
What happened:
[test] ● Test suite failed to run
[test]
[test] TypeError: Ajv is not a constructor
[test]
[test] at Object.<anonymous>.module.exports (node_modules/@eslint/eslintrc/lib/shared/ajv.js:19:17)
Reproduction repository:
Alternatively to reproduce without kcd-scripts
, use the following Jest config:
// jest.config.js
module.exports = {
moduleDirectories: ['node_modules', 'shared'],
}
Problem description: kcd-scripts
adds 'shared'
to moduleDirectories
(I assume some projects might rely on this for absolute imports. Unfortunately, there is a file in ESLint in a directory called shared
which has the same name as a package, and when it tries to import that package, the config causes it to import itself, and ESLint breaks with a runtime error because the script does not have the same API as the package. This blocked us from upgrading to Jest 27 in eslint-plugin-jest-dom
(testing-library/eslint-plugin-jest-dom#185), which has tests relying on the ESLint rule tester.
Suggested solution: It seems like the most straightforward solution is to remove 'shared'
from 'moduleDirectories'
. However I'm concerned this could break back compatibility with projects that use a shared
directory internally. Ideally we should figure out a way to configure Jest's module resolution so both ESLint imports and shared
imports work, but I don't know how to do that myself, and reordering moduleDirectories
doesn't seem to help either.
The release is available on:
npm package (@latest dist-tag)
- GitHub release
Your semantic-release bot