SyntaxError: Unexpected token export when testing typescript code with ES2015 module imports with jest











up vote
0
down vote

favorite












Im struggeling to get some code I work with tested.



In all my projects I use jest for testing. it works fine as long as I have javascript files but I do work with some packages containing typescript code. The code contains dependencies with ES2015 module imports and exports and when built its bundled with webpack where the target code still contain ES2015 module exports and imports



Jest runs on node and can´t handle ES2015 modules (yet) and I can not figure out how to configure either typescript babel or jest to transpile the dependency code into commonjs modules when I want to run my tests



I have spent an insane amount of time trying to figure this out so I appreciate all help I can get.



in this example I have cloned the jest example project for Typescript and modified it to contain a dependency containing ES2015 modules to illustrate the problem



tsconfig.json



{
"compilerOptions": {
"allowJs": true,
"module": "commonjs"
}
}


package.json



{
"devDependencies": {
"@types/jest": "^23.1.1",
"@types/node": "^10.12.3",
"jest": "*",
"typescript": "*",
"ts-jest": "*"
},

"dependencies": {
"lodash-es": "^4.17.11" //has es2015 exports
},
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\.(ts|tsx)$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.json"
}
},
"testMatch": [
"**/__tests__/*.+(ts|tsx|js)">
]
}
}


wrap.ts



import {map} from "lodash-es";
export function wrap(a: number, b: number): number {
return map([a,b],i=>({i}));
}


wrap.test.ts



import {wrap} from "../sum"
it('wrap ', ()=> {
expect(wrap(1, 2)).toBe([{i:1},{i:1}]);
});


when running jest i get



 export { default as add } from './add.js';
^^^^^^

SyntaxError: Unexpected token export


from a file in the dependency



any ideas on how to solve this?










share|improve this question




























    up vote
    0
    down vote

    favorite












    Im struggeling to get some code I work with tested.



    In all my projects I use jest for testing. it works fine as long as I have javascript files but I do work with some packages containing typescript code. The code contains dependencies with ES2015 module imports and exports and when built its bundled with webpack where the target code still contain ES2015 module exports and imports



    Jest runs on node and can´t handle ES2015 modules (yet) and I can not figure out how to configure either typescript babel or jest to transpile the dependency code into commonjs modules when I want to run my tests



    I have spent an insane amount of time trying to figure this out so I appreciate all help I can get.



    in this example I have cloned the jest example project for Typescript and modified it to contain a dependency containing ES2015 modules to illustrate the problem



    tsconfig.json



    {
    "compilerOptions": {
    "allowJs": true,
    "module": "commonjs"
    }
    }


    package.json



    {
    "devDependencies": {
    "@types/jest": "^23.1.1",
    "@types/node": "^10.12.3",
    "jest": "*",
    "typescript": "*",
    "ts-jest": "*"
    },

    "dependencies": {
    "lodash-es": "^4.17.11" //has es2015 exports
    },
    "jest": {
    "moduleFileExtensions": [
    "ts",
    "tsx",
    "js"
    ],
    "transform": {
    "^.+\.(ts|tsx)$": "ts-jest"
    },
    "globals": {
    "ts-jest": {
    "tsConfigFile": "tsconfig.json"
    }
    },
    "testMatch": [
    "**/__tests__/*.+(ts|tsx|js)">
    ]
    }
    }


    wrap.ts



    import {map} from "lodash-es";
    export function wrap(a: number, b: number): number {
    return map([a,b],i=>({i}));
    }


    wrap.test.ts



    import {wrap} from "../sum"
    it('wrap ', ()=> {
    expect(wrap(1, 2)).toBe([{i:1},{i:1}]);
    });


    when running jest i get



     export { default as add } from './add.js';
    ^^^^^^

    SyntaxError: Unexpected token export


    from a file in the dependency



    any ideas on how to solve this?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Im struggeling to get some code I work with tested.



      In all my projects I use jest for testing. it works fine as long as I have javascript files but I do work with some packages containing typescript code. The code contains dependencies with ES2015 module imports and exports and when built its bundled with webpack where the target code still contain ES2015 module exports and imports



      Jest runs on node and can´t handle ES2015 modules (yet) and I can not figure out how to configure either typescript babel or jest to transpile the dependency code into commonjs modules when I want to run my tests



      I have spent an insane amount of time trying to figure this out so I appreciate all help I can get.



      in this example I have cloned the jest example project for Typescript and modified it to contain a dependency containing ES2015 modules to illustrate the problem



      tsconfig.json



      {
      "compilerOptions": {
      "allowJs": true,
      "module": "commonjs"
      }
      }


      package.json



      {
      "devDependencies": {
      "@types/jest": "^23.1.1",
      "@types/node": "^10.12.3",
      "jest": "*",
      "typescript": "*",
      "ts-jest": "*"
      },

      "dependencies": {
      "lodash-es": "^4.17.11" //has es2015 exports
      },
      "jest": {
      "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
      ],
      "transform": {
      "^.+\.(ts|tsx)$": "ts-jest"
      },
      "globals": {
      "ts-jest": {
      "tsConfigFile": "tsconfig.json"
      }
      },
      "testMatch": [
      "**/__tests__/*.+(ts|tsx|js)">
      ]
      }
      }


      wrap.ts



      import {map} from "lodash-es";
      export function wrap(a: number, b: number): number {
      return map([a,b],i=>({i}));
      }


      wrap.test.ts



      import {wrap} from "../sum"
      it('wrap ', ()=> {
      expect(wrap(1, 2)).toBe([{i:1},{i:1}]);
      });


      when running jest i get



       export { default as add } from './add.js';
      ^^^^^^

      SyntaxError: Unexpected token export


      from a file in the dependency



      any ideas on how to solve this?










      share|improve this question















      Im struggeling to get some code I work with tested.



      In all my projects I use jest for testing. it works fine as long as I have javascript files but I do work with some packages containing typescript code. The code contains dependencies with ES2015 module imports and exports and when built its bundled with webpack where the target code still contain ES2015 module exports and imports



      Jest runs on node and can´t handle ES2015 modules (yet) and I can not figure out how to configure either typescript babel or jest to transpile the dependency code into commonjs modules when I want to run my tests



      I have spent an insane amount of time trying to figure this out so I appreciate all help I can get.



      in this example I have cloned the jest example project for Typescript and modified it to contain a dependency containing ES2015 modules to illustrate the problem



      tsconfig.json



      {
      "compilerOptions": {
      "allowJs": true,
      "module": "commonjs"
      }
      }


      package.json



      {
      "devDependencies": {
      "@types/jest": "^23.1.1",
      "@types/node": "^10.12.3",
      "jest": "*",
      "typescript": "*",
      "ts-jest": "*"
      },

      "dependencies": {
      "lodash-es": "^4.17.11" //has es2015 exports
      },
      "jest": {
      "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
      ],
      "transform": {
      "^.+\.(ts|tsx)$": "ts-jest"
      },
      "globals": {
      "ts-jest": {
      "tsConfigFile": "tsconfig.json"
      }
      },
      "testMatch": [
      "**/__tests__/*.+(ts|tsx|js)">
      ]
      }
      }


      wrap.ts



      import {map} from "lodash-es";
      export function wrap(a: number, b: number): number {
      return map([a,b],i=>({i}));
      }


      wrap.test.ts



      import {wrap} from "../sum"
      it('wrap ', ()=> {
      expect(wrap(1, 2)).toBe([{i:1},{i:1}]);
      });


      when running jest i get



       export { default as add } from './add.js';
      ^^^^^^

      SyntaxError: Unexpected token export


      from a file in the dependency



      any ideas on how to solve this?







      typescript jestjs es6-modules






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 8:25









      skyboyer

      3,07611028




      3,07611028










      asked Nov 7 at 23:36









      Magnus Siverbrant

      5817




      5817





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199535%2fsyntaxerror-unexpected-token-export-when-testing-typescript-code-with-es2015-mo%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53199535%2fsyntaxerror-unexpected-token-export-when-testing-typescript-code-with-es2015-mo%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings