【问题标题】:Angular2 + karma test + redux - process is not definedAngular2 + karma test + redux - 未定义进程
【发布时间】:2016-10-23 11:31:19
【问题描述】:

我正在尝试使用基于 angular2-redux 的 Ionic2 rc1(使用 angular 2.0.0)应用程序来实现业力单元测试的可能性。 业力设置基本上是从以下回购中采用的: https://github.com/lathonez/clicker 并且工作正常。我有一个简单的 counter redux 实现也可以正常工作。当我尝试为我的测试设置 angular2-redux 时,我收到以下错误:

Uncaught ReferenceError: 未定义进程 在 webpack:///Users/luki/Applications/ionic/ionic2-redux-example/~/redux/es/index.js:14:0

我知道进程未在浏览器中定义,但仅适用于服务器上的节点。我需要如何调整我的业力配置才能使用 angular2-redux ?

调用 createAppStoreFactory 函数以在 TestBed.configureTestingModule 中提供我的 AppStore 会导致此错误。

我的测试设置可以在以下 repo 中找到: https://github.com/lujakob/ionic2-redux-example/blob/master/src/test.ts

任何帮助表示赞赏!

(npm angular2-redux 有一个演示应用和一个示例测试设置。但这依赖于

【问题讨论】:

  • 我知道它已经过时了,但是……您找到了解决方案吗?我也遇到了 karma + redux 的这个错误......
  • 对不起,它非常具体且已有多年历史。完全不记得了。

标签: unit-testing angular redux karma-runner ionic2


【解决方案1】:

即使这是一个差不多三年前的问题,对于其他面临这个问题的人来说,这是我的解决方案:

由于process 仅在 NodeJS 中定义而不在浏览器中定义,我们必须告诉 karma 它应该“伪造”它。

创建一个 javascript 文件并为其命名(例如 global-variables.js),内容如下:

const process = {
  env: {
    NODE_ENV :'production'
  }
};

在您的karma.conf.js 中,将此文件作为files[] 数组中的FIRST import 导入:

(...)
files: [
    './test/global-variables.js',
    { pattern: config.grep ? config.grep : 'test/**/*.test.ts', type: 'module' }
],
(...)

这至少应该修复ReferenceError: process is not defined

【讨论】:

  • 感谢您的回答,我一直在寻找解决此问题的方法。这是第一个可行的解决方案。赞!!谢谢
猜你喜欢
  • 2018-10-01
  • 2017-07-18
  • 2021-10-08
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
相关资源
最近更新 更多