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

Support for TypeScript 3.7.2 #1299

Closed
ChrisLahaye opened this issue Nov 25, 2019 · 13 comments
Closed

Support for TypeScript 3.7.2 #1299

ChrisLahaye opened this issue Nov 25, 2019 · 13 comments

Comments

@ChrisLahaye
Copy link

Hello!

ts-jest currently does not work with TypeScript 3.7.2. Import statements are not transformed in the transformed code of imports in test files.

src/db/seeds/1-users/users.ts:1
import { ObjectId } from '../../../app/helpers/db';
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:703:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Module.require (internal/modules/cjs/loader.js:666:19)
    at require (internal/modules/cjs/helpers.js:16:16)
@kulshekhar
Copy link
Owner

This doesn't look like a typescript version issue. The import format has been the same for a while. Is the rest of the config correct?

@ChrisLahaye
Copy link
Author

Yes, it broke for me after upgrading the typescript package.

tsconfig.json:

{
  "compilerOptions": {
    /* Redirect output structure to the directory. */
    "outDir": "dist",
    /* Specify ECMAScript target version */
    "target": "esnext",
    /* Specify module code generation */
    "module": "commonjs",
    /* Determine how modules get resolved */
    "moduleResolution": "node",
    /* Skip type checking of all declaration files (*.d.ts). */
    "skipLibCheck": true,
    /* Emit design-type metadata for decorated declarations in source. */
    "emitDecoratorMetadata": true,
    /* Enables experimental support for ES decorators. */
    "experimentalDecorators": true,
    /* Emit a single file with source maps instead of having a separate file. */
    "inlineSourceMap": true,
    /* Emit the source alongside the sourcemaps within a single file. */
    "inlineSources": true,
    /* Enable all strict type checking options. */
    "strict": true,
  },
  "include": [
    "src"
  ]
}

jest.config.js

module.exports = {
  /* A list of paths to directories that Jest should use to search for files in */
  roots: ['src'],
  /* The test environment that will be used for testing */
  testEnvironment: 'node',
  /* A map from regular expressions to paths to transformers */
  transform: { '^.+\\.tsx?$': 'ts-jest' },
  /* Allows the use of a custom global setup module which exports an async function that is triggered once before all test suites */
  // globalSetup: './src/lib/jest/global-setup.ts',
  /* Allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites */
  // globalTeardown: './src/lib/jest/global-teardown.ts',
  /* A list of paths to modules that run some code to configure or set up the testing environment. */
  // setupFiles: ['core-js/features/array/flat', './src/lib/jest/setup.ts'],
};

@threesquared
Copy link

I think I have this same issue but with Typescript version 3.4.5 and ts-jest version 24.2.0

/Users/ben/project/src/lib/hibob.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { getUserEmailFromGithubUsername } from './hibob';
                                                                                                    ^

    SyntaxError: Unexpected token {

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)

@ChrisLahaye
Copy link
Author

@kulshekhar do you have any idea what may cause this?

@kulshekhar
Copy link
Owner

@ChrisLahaye not really. I tried it out and it worked fine for me. I checked the package-lock.json for good measure and it has the 3.7.2 version of typescript (all tests are passing with 3.7.2)

@kulshekhar
Copy link
Owner

kulshekhar commented Dec 2, 2019 via email

@ahnpnl
Copy link
Collaborator

ahnpnl commented Dec 2, 2019

Related to #1288

@ChrisLahaye
Copy link
Author

For me the problem was that I was using target: esnext which tells the compiler that your runtime will be able to handle esnext features, i.e. supports the nullish coalescing and optional chaining operator. I am using node 12 which only supports up until es2018. For people encountering this problem, check what runtime you are using and which features (e.g. operators and functions) are supported (see https://node.green/), and then set your target appropriately.

@squalsoft
Copy link

Yes, "target": "ES2019" resolves this issue!

@frck006
Copy link

frck006 commented Jan 14, 2020

@squalsoft , Hi, I've always the problem, can you share your tsconfig, please ? and jest.config ?

@squalsoft
Copy link

squalsoft commented Jan 15, 2020

@frck006
tsconfig

{
  "compilerOptions": {
    "lib": [
      "es5",
      "es6"
    ],
    "target": "ES2019",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./dist",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true
  },
  "exclude": [
    "node_modules",
  ]
}

jest.config

module.exports = {
  "transform": {
    "^.+\\.(ts|tsx)$": "ts-jest"
  },
  testEnvironment: 'node'
}

budlion pushed a commit to antvis/Graphin that referenced this issue Apr 22, 2020
ESNEXT will not tranform optional chaining which is not supported by node yet
ref
kulshekhar/ts-jest#1299 (comment)
@jamiehaywood
Copy link

I'd just like to add
My tsconfig.json was "target": "ESNEXT" but for some reason that's not supported.
I changed to "target": "ES2019" and all is dandy!

@ahnpnl
Copy link
Collaborator

ahnpnl commented May 27, 2020

#1678 will add a warning message for this scenario

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

7 participants