node.js - Jest가 jest.config 파일(스펙 파일이 아닌)을 ESM으로 읽게 하려면 어떻게 하나요?
저는 Jest로 제 ESM .spec 파일들을 실행할 수 있습니다. 제 질문은 .spec.ts 파일들이 아니라 jest.config.ts 파일 자체를 CommonJS 대신 ESM으로 불러오는 것에 관한 것입니다.
파일 jest.config.ts은 strip-json-comments와 같은 ESM 모듈을 가져옵니다(버전 > 4.0은 ESM만 지원합니다).
jest.config.ts:
import strip from 'strip-json-comments'
export default function config(){
// call the esm function
strip(...);
}
이로 인해 다음 오류가 발생합니다:
Error: Jest: Failed to parse the TypeScript config file jest.config.ts
Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/.pnpm/[email /node_modules/strip-json-comments/index.js from jest.config.ts not supported.
Instead change the require of index.js in jest.config.ts to a dynamic import() which is available in all CommonJS modules.
ESM 사양 파일은 정상적으로 실행될 수 있다는 점에 유의하세요. 즉, jest.config.ts 안에서 ESM 모듈을 가져오지 않으면 이 오류가 발생하지 않습니다.
저는 package.json에 "type": "module"을 추가하고 이렇게 Jest를 실행했습니다:
node --loader ts-node/esm --experimental-specifier-resolution=node --experimental-import-meta-resolve --experimental-vm-modules node_modules/jest/bin/jest.js
최소 재현