用玩笑测试角度8中的amcharts 4

时间:2020-01-27 14:51:03

标签: testing jestjs angular8 amcharts4

尝试使用Jest测试Angular 8组件,该组件上使用amcharts图形。我发现的唯一解决方案是特定于React (React Solution)的。当Jest运行它使用节点模块时,这似乎失败了,因此在我认为可以为相关导入实现模拟之前。

有什么想法吗?

错误消息:

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.

export { System, system } from "./.internal/core/System";
    ^^^^^^

    SyntaxError: Unexpected token 'export'

       8 |   OnChanges,
       9 | } from '@angular/core';
    > 10 | import * as am4core from '@amcharts/amcharts4/core';
         | ^
      11 | import * as am4charts from '@amcharts/amcharts4/charts';
      12 | import am4themes_animated from '@amcharts/amcharts4/themes/animated';
      13 | import { Moment } from 'moment';

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)

1 个答案:

答案 0 :(得分:0)

我在React中遇到了同样的问题。我发现您需要使用transformIgnorePatterns来解决此问题。有时与babel结合使用可能会有些棘手。另请参见https://github.com/amcharts/amcharts4/issues/3131#event-4045290142

transformIgnorePatterns: ['node_modules/(?!@amcharts/amcharts4/)'],

这对我有用。