jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.28k stars 6.47k forks source link

[Bug]: ESM, typescript: importing attributes from package.json causes failing to start test suites #15353

Open yarick123 opened 3 weeks ago

yarick123 commented 3 weeks ago

Version

29.7.0

Steps to reproduce

use ESM module, use the latest typescript version - 5.6.3, set "resolveJsonModule": true in the tsconfig.json, in an source file with the tested functionality put something like this:

import { name } from '../package.json' with { type: 'json' };

console.log(name);

start a test suit, which tests functionality in the file changed before

Expected behavior

tests from the test suit are started

Actual behavior

● Test suite failed to run

SyntaxError: The requested module '../package.json' does not provide an export named 'name'

  at Runtime.linkAndEvaluateModule (../../../../node_modules/jest-runtime/build/index.js:708:5)

Additional context

workaround: instead of the named import, import whole json object:

import pkgJson from '../package.json' with { type: 'json' };

console.log(pkgJson.name);

Environment

System:
  OS: Ubuntu 24.04.1 LTS
  CPU: (16) AMD Ryzen 7 PRO 6850U
Binaries:
  Node: 20.16.0 - ~/.nvm/versions/node/v20.16.0/bin/node
  npm: 10.8.1 - ~/.nvm/versions/node/v20.16.0/bin/npm
npmPackages:
  jest: 29.7.0