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

Enum breaks ts-jest #1229

Closed
bodinsamuel opened this issue Sep 15, 2019 · 7 comments
Closed

Enum breaks ts-jest #1229

bodinsamuel opened this issue Sep 15, 2019 · 7 comments

Comments

@bodinsamuel
Copy link

May be related to #944 #962

Issue

I struggled to find a good reason why ts-jest was failing. I first thought my aliases were the source of it. But I reproduced the bug, and finally understood. Accessing an ENUM value is breaking ts-jest.

const a = MyEnum['value'];

It throws:

Cannot find module '../types/tet' from
However, Jest was able to find:

Expected behavior

Well, working obviously 🥺
I found many old issues talking about enum, but nothing in the doc mentioning that specifically.
There is a mention of const enum which is not exactly my case here. And passing isolatedModules to false or true does not change the outcome.

Minimal repo

Here is the smallest repo possible, with all explanation in it (install and run basically)
https://github.com/bodinsamuel/ts-jest-not-working-with-enum/tree/master

@bodinsamuel bodinsamuel changed the title Enum break ts-jest Enum breaks ts-jest Sep 15, 2019
@projekt86
Copy link

Downgrading to <23.10.0 works for me

yuqu added a commit to zeplin/cli-connect-react-plugin that referenced this issue Oct 23, 2019
yuqu added a commit to zeplin/cli-connect-react-plugin that referenced this issue Oct 23, 2019
yuqu added a commit to zeplin/cli-connect-react-plugin that referenced this issue Nov 3, 2019
@ahnpnl
Copy link
Collaborator

ahnpnl commented Dec 31, 2019

hi @bodinsamuel , I've checked your repo. You have tet.d.ts but you don't have tet.ts which causes the error. If you put your enum into tet.ts it will work. d.ts extension is type definition file, I wouldn't put enum definition into d.ts to avoid issues. I will close this issue as it's working.

@ahnpnl ahnpnl closed this as completed Dec 31, 2019
@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 31, 2020

Please see the detail of explanation here

@ncuillery
Copy link

@ahnpnl thanks for the explanation, but what are we supposed to do if the enum is defined in a 3rd-party d.ts file? like https://github.com/software-mansion/react-native-gesture-handler/blob/master/react-native-gesture-handler.d.ts
Duplicate it? 😕

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 4, 2020

Hi, I think you can ask jest to skip d.ts from transforming. If jest doesn’t give d.ts to ts-jest, typescript compiler api in ts-jest internal wont throw errors. In general, the input files ts-jest processes depending on jest, whatever jest gives, ts-jest will process.

@ncuillery
Copy link

Oh sorry I was unclear. My use-case is the following:

I'm using this enum from the react-native-gesture-handler definition file: react-native-gesture-handler.d.ts#L32-L39

So in my code, in the file I'd like to test, I have:

import { [....], State } from "react-native-gesture-handler";

[....]

if (state === State.BEGAN) {

[....]

And my test fails on TypeError: Cannot read property 'BEGAN' of undefined. But I have no control on the State enum. The d.ts is needed, the enum is defined in it.

The best workaround I could find is to mock the State enum in the jest setup file with a plain old JavaScript object:

jest.mock("react-native-gesture-handler", () => ({
  PanGestureHandler: () => "PanGestureHandler",
  NativeViewGestureHandler: () => "NativeViewGestureHandler",
  State: {
    UNDETERMINED: 0,
    FAILED: 1,
    BEGAN: 2,
    CANCELLED: 3,
    ACTIVE: 4,
    END: 5
  }
}));

@ahnpnl
Copy link
Collaborator

ahnpnl commented Feb 4, 2020

I don't have any other ways other than your provided workaround :( but I think your workaround looks good. Thanks for sharing. In general, d.ts should be generated from a ts file, that will make it easier to use when distributing a package via npm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants