Ts-jest: Parsing TypeScript namespace token

Created on 14 Jul 2018  Â·  2Comments  Â·  Source: kulshekhar/ts-jest

  • Issue

Running jest using ts-jest produces an error when it tries to parse a TypeScript namespace.

  • Expected behavior

Jest should be able to parse the file normally without any error.

  • Output from your debug log
      Jest encountered an unexpected token
      This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
      By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
      Here's what you can do:
       • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
       • If you need a custom transformation specify a "transform" option in your config.
       • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
      You'll find more details and examples of these config options in the docs:
      https://facebook.github.io/jest/docs/en/configuration.html
      Details:
        153 | }
        154 |
      > 155 | export namespace QueryResolvers {
            |                  ^
        156 |   export interface Resolvers {
        157 |     user?: UserResolver;
        158 |     countries?: CountriesResolver;

Did I miss something in the Jest setup or is this not a support feature? I've looked elsewhere but was unable to find any similar discussions about this particular issue. Happy to provide a repo to reproduce if needed but I think it will be reproduced by importing from any TypeScript file which uses a namespace when using ts-jest.

Most helpful comment

I've managed to figure this out. The culprit was several configuration issues. Notes to anyone in the future (for reference this is within a TypeScript React Native project with Expo):

From jest.config.js:

module.exports = {
    preset: "jest-expo",
    globals: {
        "ts-jest": {
            "useBabelrc": true,
        }
    },
    transform: {
        "^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
        "^.+\\.tsx?$": "ts-jest",
        "\\.(gql|graphql)$": "jest-transform-graphql",
    },
    // ... other config
}

I also had to enable this option in tsconfig.json:

"esModuleInterop": true,

All 2 comments

Please yes provide a minimal repo, because your issue looks like a jest config issue where you'd forget the ts in moduleFileExtensions for example.

I've managed to figure this out. The culprit was several configuration issues. Notes to anyone in the future (for reference this is within a TypeScript React Native project with Expo):

From jest.config.js:

module.exports = {
    preset: "jest-expo",
    globals: {
        "ts-jest": {
            "useBabelrc": true,
        }
    },
    transform: {
        "^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
        "^.+\\.tsx?$": "ts-jest",
        "\\.(gql|graphql)$": "jest-transform-graphql",
    },
    // ... other config
}

I also had to enable this option in tsconfig.json:

"esModuleInterop": true,
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahnpnl picture ahnpnl  Â·  3Comments

jbreckmckye picture jbreckmckye  Â·  3Comments

stephenotalora picture stephenotalora  Â·  3Comments

Slessi picture Slessi  Â·  3Comments

Vinnl picture Vinnl  Â·  3Comments