gpt4 book ai didi

unit-testing - Angular2 + karma test + redux - 进程未定义

转载 作者:行者123 更新时间:2023-12-04 05:00:59 25 4
gpt4 key购买 nike

我正在尝试使用基于 angular2-redux 的 Ionic2 rc1(使用 angular 2.0.0)应用程序实现 karma 单元测试的可能性。
karma 设置基本上来自以下 repo:
https://github.com/lathonez/clicker并且工作正常。我有一个简单的计数器 redux 实现也可以正常工作。当我尝试为我的测试设置 angular2-redux 时,出现以下错误:

未捕获的 ReferenceError:未定义进程
在 webpack:///Users/luki/Applications/ionic/ionic2-redux-example/~/redux/es/index.js:14:0 <- src/test.ts:65041

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

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

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

任何帮助表示赞赏!

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

最佳答案

即使这几乎是一个三年前的问题,对于面临这个问题的其他人,这是我的解决方案:

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

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

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

在您的 karma.conf.js , 将此文件导入为 第一次进口 files[]大批:
(...)
files: [
'./test/global-variables.js',
{ pattern: config.grep ? config.grep : 'test/**/*.test.ts', type: 'module' }
],
(...)

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

关于unit-testing - Angular2 + karma test + redux - 进程未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com