Akheloes
Akheloes

Reputation: 1372

Syntaxe error on executing typescript test with Jest

Trying to run a test written in typescript 2.7.1 with Jest, it fails on a syntax error for this exact code:

module X.Y.Z {

}

With an error message like so:

SyntaxError: C:\Users\jest-typescript\__tests__\example.spec.ts: Unexpected token, expected ";" (1:7)

    > 1 | module X.Y.Z {
        |        ^
      2 | 
      3 | }
      4 | 

      at Parser.raise (node_modules/@babel/parser/lib/index.js:6325:17)

I am running the tests using the following CLI command line:

jest -t --config=jest.config.js

It seems then the babel parser is having an issue with the syntax. My problem is that I cannot change the typescript syntax (a legacy code base), since I am not seeing an option of the jest.config.js which would allow me to specify a version of typescript.

My jest.config.js is as follows:

module.exports = {
  coverageDirectory: "coverage",
  testEnvironment: "node",
  testMatch: [
    "**/__tests__/**/*.[jt]s?(x)"
  ]
};

Any ideas how I can adress these syntax erros aside from refactoring the code ?

Upvotes: 0

Views: 282

Answers (1)

Akheloes
Akheloes

Reputation: 1372

Alright, a possiblity would be to install ts-jest and add the following option to the jest.config.js:

"transform": {
    "^.+\\.tsx?$": "ts-jest"
  },

More infos on this link.

Upvotes: 1

Related Questions