kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.

Home Page:https://kulshekhar.github.io/ts-jest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Constant enums are not compiled

pablobirukov opened this issue · comments

Hi guys. Imagine I have tsconfig.json like

{
    "compilerOptions": {"...whatever"},
    "includes": [
        "src/**/*"
    ]
}

and i have constant enum in src/data.d.ts

declare const enum Foo {
    Bar = 1,
}

then simply src/index.ts

export const Bar = Foo.Bar;

tsc's output for index.ts is exports.Bar = 1 /* Bar */; but in indexSpec.tsx

import { Bar } from "./";
describe("Bar", () => {
    it("is defined", () => {
        expect(Bar).toBeDefined();
    });
});

I've got ReferenceError: Foo is not defined. Is it possible to make this work?

Would it be possible for you to create a small repo that can reproduce this issue?

Please check tsc and npm t in the repo above @kulshekhar

I just browsed through the linked repo. Isn't this more of a Typescript issue than something that's ts-jest specific?

Can be. I'll take a closer look during this week. Let's keep it open

@kulshekhar you right, this is default behaviour. So It shouldn't substitute constant enums even it it's <reference />d or defined in the same file

It's not clear whether there was a workaround for this or just SOL?

I'm consuming a third party library that exports constant enums, same result TypeError: Cannot read property 'X' of undefined because they are not inlined and no runtime code for them exists.

@jayphelps in the linked repo, if the enum is exported and imported, it works fine. Is that possible in your case? A tiny repo with your issue would be the best way to see if we can get this working

@R00GER can this get added to the readme? It's non obvious behaviour and it seems a lot of people spend time searching for the cause.

@rikkit I'm a bit hesitant because this doesn't seem to be a ts-jest issue. On the other hand, I see the point in adding a note if there are a lot of people running into this issue.

Any PRs which update the README accordingly are welcome