代码之家  ›  专栏  ›  技术社区  ›  user776686

Jest遇到意外的令牌-导入(6)

  •  1
  • user776686  · 技术社区  · 6 年前

    在Mac上,我在安装jest和使用angular/cli(v6)应用程序时遇到问题。不管我用什么方法来开玩笑(如下所列),我都得到了一个错误:

    Test suite failed to run
    
        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://jestjs.io/docs/en/configuration.html
    
        Details:
    
        /Users/owner/Code/testjest/src/setup-jest.ts:1
        ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import 'jest-preset-angular';
                                                                                                 ^^^^^^
    
        SyntaxError: Unexpected token import
    
          at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
    

    奥托,当我克隆的时候 https://github.com/blove/ngrx-testing/tree/master ,repo运行jest很好-我认为,这排除了全球安装jest包的问题

    1 回复  |  直到 6 年前
        1
  •  2
  •   Tumit Watcharapol    6 年前

    • 清除笑话缓存 jest --clearCache
    • 添加 "module": "commonjs" 在tsconfig.spec.json中

    裁判: https://github.com/thymikee/jest-preset-angular/issues/194