[英]Angular-CLI “ng e2e”: missing localStorage in protractor tests with ts-node
我有一个基于ngrx并使用@angular/cli: 1.0.0-beta.32.3
构建的Angular2项目@angular/cli: 1.0.0-beta.32.3
。 该应用程序本身与官方支持的示例ngrx应用程序有很多共同点: https : //github.com/ngrx/example-app 。
我使用https://github.com/btroncone/ngrx-store-localstorage软件包将商店的某些部分与localStorage同步,因此:
在我的reducers/index.ts
文件中(看起来像原始的https://github.com/ngrx/example-app/blob/master/src/app/reducers/index.ts )代替原始文件:
const developmentReducer: ActionReducer<State> = compose(
storeFreeze,
combineReducers
)(reducers);
const productionReducer: ActionReducer<State> = combineReducers(reducers);
我有稍微修改的版本:
const developmentReducer: ActionReducer<State> = compose(
storeFreeze,
localStorageSync(['auth'], true),
combineReducers
)(reducers);
const productionReducer: ActionReducer<State> = compose(
localStorageSync(['auth'], true),
combineReducers
)(reducers);
它工作正常,没有任何问题。
问题是我无法使用此设置运行量角器e2e测试 。 此localStorageSync
将浏览器的localStorage
作为默认参数。 然而, angular-cli's
量角器使用ts-node
中陈述protractor.conf.js
:
beforeLaunch: function() {
require('ts-node').register({
project: 'e2e'
});
},
ng e2e
以错误结束:
[19:25:07] I/direct - Using ChromeDriver directly...
[19:25:09] E/launcher - Error: ReferenceError: localStorage is not defined
at Object.exports.localStorageSync (/home/myfolder/e2e-storage-test/node_modules/ngrx-store-localstorage/src/index.ts:118:77)
at Object.<anonymous> (/home/myfolder/e2e-storage-test/src/app/reducers/index.ts:32:3)
at Module._compile (module.js:570:32)
at Module.m._compile (/home/myfolder/e2e-storage-test/node_modules/ts-node/src/index.ts:406:23)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/myfolder/e2e-storage-test/node_modules/ts-node/src/index.ts:409:12)
可在此处找到完整的追溯: https : //gist.github.com/radoslawroszkowiak/17e53b9043264b6d813df2374e8d4dc8 。
我想避免弄乱节点本地存储之类的事情,因为我不希望该项目仅用于浏览器环境,而用于服务器端渲染。
如何解决此问题?
正如人们所料想的那样,这个原因真是愚蠢。
在一个e2e测试文件中,从应用程序的路由模块中导入了路径名。 这导致了导入链,包括在路由模块中导入的组件等。
由于e2e测试文件是使用ts-node执行的,而不是在浏览器上下文中执行的,因此,一旦编译器遇到localStorage
,就会引发该错误。
删除e2e规范文件中有问题的导入可解决此问题。
对我来说,结论是:在e2e模块中从您的应用程序导入任何内容时要小心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.