kentcdodds / kcd-scripts

CLI toolbox for common scripts for my projects

Home Page:http://npm.im/kcd-scripts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: ESLint cannot be imported in Jest 27

nickserv opened this issue · comments

  • kcd-scripts version: 11.2.0
  • node version: 16.9.1
  • npm (or yarn) version: 7.23.0
  • jest 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:

https://github.com/testing-library/eslint-plugin-jest-dom/tree/5cfb8c97ecc78fb4f61748fe083123bed7dc1f9e

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.

🎉 This issue has been resolved in version 11.2.1 🎉

The release is available on:

Your semantic-release bot 📦🚀