jest icon indicating copy to clipboard operation
jest copied to clipboard

Include ".ts" in `extensionsToTreatAsEsm` for TypeScript projects with "type": "module"

Open RA80533 opened this issue 4 years ago • 2 comments

🚀 Feature Proposal

Include ".ts" in the default values of extensionsToTreatAsEsm for TypeScript projects with "type": "module".

Motivation

The TypeScript compiler treats .ts files as their would-be .js counterparts for interactions relating to the TS-to-JS transpilation effort. The purpose of this behavior is to better represent the runtime shape and structure of the output.

The underlying behavior of requiring ".ts" in extensionsToTreatAsEsm breaks this abstraction, at least in terms of its name. It's not TypeScript that should be treated as ES Modules but rather the corresponding JavaScript it represents. Requiring manual configuration on the part of the user pushes this misunderstanding onto others.

Not even the TypeScript compiler allows TypeScript files to be treated as ES Modules:

$ tsc --build
src/index.ts:1:25 - error TS2691: An import path cannot end with a '.ts' extension. Consider importing './version.js' instead.

1 import { version } from "./version.ts";
                          ~~~~~~~~~~~~~~

Note that, if the import syntax were changed to that of CommonJS (in other words, require), there would be no error.

In leu of requesting a deeper change in the way Jest manages anything really internal to stamp out this break in abstraction entirely, the easiest solution to alleviate this issue is to include ".ts" to extensionsToTreatAsEsm by default.

Pitch

  • Nearly every TypeScript project that uses Jest would be able to take advantage of this change immediately.
  • There would be no requirement for new users to create a configuration file to specifically enable this behavior.
  • Jest would work with no configuration whatsoever for TypeScript projects.

RA80533 avatar Jul 25 '21 10:07 RA80533

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jul 25 '22 12:07 github-actions[bot]

Not stale

RA80533 avatar Aug 05 '22 19:08 RA80533

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Aug 05 '23 20:08 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Sep 04 '23 20:09 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Oct 05 '23 00:10 github-actions[bot]