Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
smack0007 opened this issue Jan 19, 2023 · 5 comments · Fixed by #4002
Closed

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

smack0007 opened this issue Jan 19, 2023 · 5 comments · Fixed by #4002
Labels
🚀 Feature Request new suggested feature

Comments

@smack0007
Copy link
Contributor

🚀 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?$/;
@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 20, 2023

What you are doing in your codebase is a valid solution. The thing is that ts-jest will transform files which are given by Jest. To be able to load those file extensions, it’s in Jest scope because it’s related to module resolution and module loader, but not transformer area.

You should request this to Jest team about loading files.

about transforming more file extensions, you will need to adjust your transform config to have the regex to include the extensions you need. This belongs to Jest config and ts-jest cannot interfere with that process.

@michael42
Copy link

michael42 commented Jan 23, 2023

I don't think I get what you're saying, here's a reproduction repo, maybe that helps.

There, you can easily see, that jest supports .cjs just fine. One can import .cjs and use that within a test:

x.cjs:

exports.x = ".cjs";

x.spec.js:

const {x} = require('./x.cjs');

it('should work', () => {
    expect(x).toBe('.cjs');
});

But when doing the same, but with TypeScript:
y.cts:

export const y = '.cts' as const

y.spec.js:

const {y} = require('./y.cts');

it('should work', () => {
    expect(y).toBe('.cts');
});

If ts-jest is properly listed in jest.config.js (transform: {"\.cts$": "ts-jest" }), ts-jest complains that one should remove it there:

ts-jest[ts-jest-transformer] (WARN) Got a unknown file type to compile (file: .../ts-jest-cts/ts/y.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.

Which is confusing instead of helpful, because when not processing TypeScript, jest obviously does not support the syntax and throws the usual Jest encountered an unexpected token error.

edit: forgot the repo link

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2023

You should check this to understand more about Jest infra https://jestjs.io/docs/code-transformation

@michael42
Copy link

What are you suggesting exactly? When jest is configured to call ts-jest, to transform the source (.cts) to JavaScript, but ts-jest only prints a warning and does not transform the code, how does reading docs help?

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 23, 2023

Ah I know what it is now, because of this

export const TS_TSX_REGEX = /\.m?tsx?$/

The issue is about: transforming file, not loading file :) PR is welcome

@ahnpnl ahnpnl reopened this Jan 23, 2023
Vylpes pushed a commit to Vylpes/card-drop that referenced this issue Jun 3, 2024
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-jest](https://kulshekhar.github.io/ts-jest) ([source](https://github.com/kulshekhar/ts-jest)) | dependencies | patch | [`29.1.3` -> `29.1.4`](https://renovatebot.com/diffs/npm/ts-jest/29.1.3/29.1.4) |

---

### Release Notes

<details>
<summary>kulshekhar/ts-jest (ts-jest)</summary>

### [`v29.1.4`](https://github.com/kulshekhar/ts-jest/blob/HEAD/CHANGELOG.md#2914-2024-05-28)

[Compare Source](kulshekhar/ts-jest@v29.1.3...v29.1.4)

##### Bug Fixes

-   fix(transformer): allow transforming of .cts/.mts extensions. ([#&#8203;3996](kulshekhar/ts-jest#3996)) ([b8f6eaa](kulshekhar/ts-jest@b8f6eaa)), closes [#&#8203;3996](kulshekhar/ts-jest#3996)

##### Features

-   feat: make cli generate esm config based on `type: "module"` ([#&#8203;4210](kulshekhar/ts-jest#4210)) ([81a5f64](kulshekhar/ts-jest@81a5f64)), closes [#&#8203;4210](kulshekhar/ts-jest#4210) [#&#8203;4012](kulshekhar/ts-jest#4012)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: https://git.vylpes.xyz/External/card-drop/pulls/255
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
Vylpes pushed a commit to Vylpes/vylbot-app that referenced this issue Jun 25, 2024
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-jest](https://kulshekhar.github.io/ts-jest) ([source](https://github.com/kulshekhar/ts-jest)) | dependencies | patch | [`29.1.2` -> `29.1.5`](https://renovatebot.com/diffs/npm/ts-jest/29.1.2/29.1.5) |

---

### Release Notes

<details>
<summary>kulshekhar/ts-jest (ts-jest)</summary>

### [`v29.1.5`](https://github.com/kulshekhar/ts-jest/blob/HEAD/CHANGELOG.md#2915-2024-06-16)

[Compare Source](kulshekhar/ts-jest@v29.1.4...v29.1.5)

##### Bug Fixes

-   build(deps-dev): bump braces ([5560334](kulshekhar/ts-jest@5560334)), ([59026b4](kulshekhar/ts-jest@59026b4)), ([0d9e359](kulshekhar/ts-jest@0d9e359))

### [`v29.1.4`](https://github.com/kulshekhar/ts-jest/blob/HEAD/CHANGELOG.md#2914-2024-05-28)

[Compare Source](kulshekhar/ts-jest@v29.1.3...v29.1.4)

##### Bug Fixes

-   fix(transformer): allow transforming of .cts/.mts extensions. ([b8f6eaa](kulshekhar/ts-jest@b8f6eaa)), closes [#&#8203;3996](kulshekhar/ts-jest#3996)

##### Features

-   feat: make cli generate esm config based on `type: "module"` ([81a5f64](kulshekhar/ts-jest@81a5f64)), closes [#&#8203;4210](kulshekhar/ts-jest#4210) [#&#8203;4012](kulshekhar/ts-jest#4012)

### [`v29.1.3`](https://github.com/kulshekhar/ts-jest/blob/HEAD/CHANGELOG.md#2913-2024-05-21)

[Compare Source](kulshekhar/ts-jest@v29.1.2...v29.1.3)

##### Bug Fixes

-   add `@jest/transform` as an optional peer dependency ([0ba7f86](kulshekhar/ts-jest@0ba7f86))
-   bring back Node 14 support ([eda56a7](kulshekhar/ts-jest@eda56a7))

##### Performance Improvements

-   remove ts resolved module cache file ([4c88da5](kulshekhar/ts-jest@4c88da5))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=-->

Reviewed-on: https://git.vylpes.xyz/RabbitLabs/vylbot-app/pulls/438
Reviewed-by: Vylpes <ethan@vylpes.com>
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 Feature Request new suggested feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants