question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question]Const enum still not working

See original GitHub issue

Issue :

Const enum still not working As stated on the documentation: https://kulshekhar.github.io/ts-jest/user/babel7-or-ts#no-const-enum, there is still some problems with const enum in TS. In issue #281 there was a reference to PR #505 as a possible solution and #697 as a release that featured those changes. I tried setting the useExperimentalLanguageServer flag to true in globals, and though it seems to not fail in the enums, it has problems resolving imported modules, both in relative and in aliased paths, throwing: TSC language server encountered errors while transpiling. Errors: Cannot find module If I set the flag to false, it just fails on const enums, which of course I can work around with the suggested let Enum: Enum = 1 as Enum or similar approaches and everything works fine, but I was really hoping to give full support to the code without working around them. So, my main question or reason for reporting this, is that I am not sure of how the TSC language server works and why is it messing with module resolution when turned on.

My settings:

jest.conf.js

module.exports = {
    moduleFileExtensions: ["ts", "js", "json", "vue"],
    testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)+(spec|test).(ts|tsx|js)'],
    transform: {
        "^.+\\.(ts)$": "<rootDir>/node_modules/ts-jest/preprocessor",
        ".*\\.(vue)$": "<rootDir>/node_modules/vue-jest",
        "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest"
    },
    verbose: true,
    moduleDirectories: [
        ".",
        "src",
        "node_modules"
    ],
    moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1"
    },
    globals: {
        'ts-jest': {
            // to give support to const enum. Not working, conflicting with module resolution
            useExperimentalLanguageServer: false
        }
    }
}; 

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "isolatedModules": false,
    "experimentalDecorators": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": [
    "./src/**/*.ts"
  ],
  "compileOnSave": false
}

babelrc

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": ["transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", {  
        "targets": {  
           "node":"current"
        }
     }, "stage-2"],
      "plugins": ["istanbul"]
    }
  }
}

And just in case, error with routes using aliases: TSC language server encountered errors while transpiling. Errors: Cannot find module '@/components/player/player.vue'.

Without aliases: TSC language server encountered errors while transpiling. Errors: Cannot find module './player/player.vue'.

Everything working with the flag set to false and work around for const enum:

  PASS  src/components/player/player.spec.ts
  Component: Player - unit tests
    ✓ Should update skipAds property (21ms)

Expected behavior :

As stated in the title and description, I am pretty sure this is not an issue, since the repo specifically says this is a TS issue that they are not willing to fix/address, but just to be sure, I wanted to see if there was anything else at play. You can contact me in the ts-jest slack, I sent this same inquire in the general channel (Pedro/Peter is my handle and show name).

Thanks for your time. Have a great day.

Debug log:

log file content
# content of ts-jest.log :

Minimal repo :

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

18reactions
ahnpnlcommented, Dec 31, 2019

ts-jest has e2e test on const enum as well as enum, the tests are successful. If you encounter any issues with const enum or enum, please create another issue. I will close this.

5reactions
ristoncommented, Jan 21, 2019

Encountered exact same issue with enums, also with the latest “ts-jest” package. The work around for this was to put enum in separate file after that it was correctly transpiled.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get a const enum from value in typescript [duplicate]
I've tried the approaches that relate to non const enum and they only work for non const enum. If enum is defined as...
Read more >
The trouble with TypeScript enums - Thoughtbot
In TypeScript, enums have a few surprising limitations. In particular, it can be challenging to check whether or not a value is in...
Read more >
Object.keys(const enum) problem : r/typescript - Reddit
Hello, I'm using React with Typescript and I got a really weird problem. I got a graphql typings generated by GraphQLZeus, and I...
Read more >
How To Use Enums in TypeScript - DigitalOcean
Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can...
Read more >
enum in Java - GeeksforGeeks
It is not necessary that the set of constants in an enum type stay fixed for all time. A Java enumeration is a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found