Skip to content

[Feature]: Support loading of ".cjs" and ".cts" files. #3996

@smack0007

Description

@smack0007

🚀 Feature Proposal

Support loading and transforming of files with the ".cts" file extension.

Motivation

In our code base we import all our files using the ".js" file extension even though we are writing in TypeScript. We then use a custom jest resolver to load files when running the tests in jest as it will not recognize the files otherwise. We also have situations where we differentiate between CommonJS and ESM implementations using ".cts" and ".mts" files extensions which means the imports then have the extensions ".cjs" and ".mjs" respectively. Using ".cjs" imports in a test leads to the following warning:

ts-jest[ts-jest-transformer] (WARN) Got a unknown file type to compile (file: ./shared/src/file.cts). To fix this, in your Jest config change the `transform` key which value is `ts-jest` so that it does not match this kind of files anymore.

And the file is not transformed at all and so the test fails. In our custom resolver we have inserted the hack displayed in the example section which allows our ".cts" files to loaded without any problems.

Example

// HACK: ts-jest does not consider .cts files as TypeScript
/** @type { { TS_TSX_REGEX: RegExp, JS_JSX_REGEX: RegExp }  } */
const constants = require('ts-jest/dist/constants');
constants.TS_TSX_REGEX = /\.[cm]?tsx?$/;
constants.JS_JSX_REGEX = /\.[cm]?jsx?$/;

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions