2017-02-20 108 views
1

我有一个基于ngrx的并且使用@angular/cli: 1.0.0-beta.32.3构建的Angular2项目。 该应用程序本身与官方支持的示例ngrx应用程序有很多相同之处:https://github.com/ngrx/example-appAngular-CLI“ng e2e”:在使用ts-node的量角器测试中缺少localStorage

我使用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-nodeprotractor.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

我想避免像node-localstorage这样的东西搞乱,因为我不希望这个项目用于服务器端渲染,只有浏览器环境。

如何解决此问题?

回答

2

原因竟然非常愚蠢,正如人们所预料的那样。

在其中一个e2e测试文件中,从应用程序的路由模块导入了路径名。这导致进口链,包括在路由模块中导入的组件等。

由于e2e测试文件是在ts-node中执行的,而不是在浏览器上下文中,所以只要编译器遇到错误就会引发localStorage发生。

删除e2e spec文件中有问题的导入解决了问题。

结论对我来说是:在从e2e模块内的应用程序导入任何东西时要小心。

相关问题