kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.94k stars 452 forks source link

SyntaxError in declaration merging typescript test #1171

Closed Akheloes closed 4 years ago

Akheloes commented 5 years ago

I have a jest project where I'd like to execute this test-file (in project/ tests /example.spec.ts):


module X.Y.Z {
        let r = X.Y.Z.C;
        describe("A", ()=>{
            it("Does A stuff", ()=>{
                expect(r).toEqual("someContent");
            });
        })
    }

The source code (in project/src/source.ts) :


module X.Y.Z {
        export const C = "someContent";
    }

My jest configuration:


module.exports = {
      globals: {
        'ts-jest': {
          "tsConfig": "<rootDir>/tsconfig.test.json"
        }
      },
      coverageDirectory: "coverage",
      testEnvironment: "jsdom",
      testMatch: [
          '<rootDir>/**/__tests__/**/*.ts'
      ]
    };

The tsConfig.test.json file :


{
      "compileOnSave": false,
      "compilerOptions": {
        "allowJs": true,
        "moduleResolution": "classic",
        "inlineSources": true,
        "declaration": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es5",
        "typeRoots": [
          "node_modules/@types",
          "./node_modules/typescript/lib/lib.d.ts"
        ]
      },
      "files": [
         "./src/source.ts"
      ]
}

Test's execution fails on a syntaxError:

 FAIL  __tests__/example.spec.ts
  ● Test suite failed to run

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

    > 1 | module X.Y.Z {
        |        ^
      2 | 
      3 |     let r = X.Y.Z.C;
      4 | 

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

Anybody knows how I may be able to bring jest to execute this test ? Thanks !

ahnpnl commented 5 years ago

Tbh I never write test in the way you did in the example. As far as I know about TS, I only use declaration merging for typings. Maybe if you write tests in a traditional way, it should solve your issue ?

What do you try to achieve in your example with declaration merging ?

Looking at your error, look like babel tries to do something with your test file, not sure if the error is caused by babel.

ahnpnl commented 4 years ago

I think this error is related to how to write tests with jest, it's not in the scope of ts-jest. You can ask this question on StackOverflow or jest repository.

kenobi-io commented 6 months ago

Same issue:

● Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • 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://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

node_modules/@types/vscode/index.d.ts:12 declare module "vscode" { ^^^^^^

SyntaxError: Unexpected identifier
> 4 | import { window } from "vscode";