2017-02-20 12 views
1

ngrxに基づいたAngular2プロジェクトがあり、@angular/cli: 1.0.0-beta.32.3で構築されています。 アプリケーション自体は、公式にサポートされているngrxアプリケーションの例とよく似ています。https://github.com/ngrx/example-appAngular-CLI "ng e2e":ts-nodeを使った分度器テストでlocalStorageが見つからない

私は、https://github.com/btroncone/ngrx-store-localstorageパッケージを使用してのlocalStorageと店舗の一部を同期:私が持っている

const developmentReducer: ActionReducer<State> = compose(
    storeFreeze, 
    combineReducers 
)(reducers); 

const productionReducer: ActionReducer<State> = combineReducers(reducers); 

:オリジナルの代わりに私のreducers/index.tsファイル(元https://github.com/ngrx/example-app/blob/master/src/app/reducers/index.tsのように見える)で

わずかに変更されたバージョン:

const developmentReducer: ActionReducer<State> = compose(
    storeFreeze, 
    localStorageSync(['auth'], true), 
    combineReducers 
)(reducers); 

const productionReducer: ActionReducer<State> = compose(
    localStorageSync(['auth'], true), 
    combineReducers 
)(reducers); 

それだけで正常に動作し、無問題ですべて。

事ははこの設定で分度器e2eテストを実行できません。このlocalStorageSyncは、ブラウザのlocalStorageをデフォルト引数として受け取ります。しかし、protractor.conf.jsに述べたようにangular-cli's分度器がts-nodeを使用しています。ここで使用可能な

[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

beforeLaunch: function() { 
    require('ts-node').register({ 
    project: 'e2e' 
    }); 
}, 

ng e2eはエラーで終了します。

私はこのプロジェクトをサーバー側のレンダリングに使用するのではなく、ブラウザ環境だけにしたいので、node-localstorageのようなものを使いこなすことを避けたいと思います。

この問題を回避するにはどうすればよいですか?

答えて

2

この理由は、予想通り、かなりばかげていることが判明しました。

e2eテストファイルの1つに、アプリケーションのルーティングモジュールからのパス名のインポートがありました。これにより、ルーティングモジュールにインポートされたコンポーネントを含むインポートチェーンが発生しました。

e2eテストファイルはブラウザコンテキストではなくts-nodeで実行されるため、コンパイラがlocalStorageに遭遇するとすぐにエラーがスローされました発生。

e2e仕様ファイル内で問題のあるインポートを削除すると、問題が解決しました。

私の結論は次のとおりです.e2eモジュール内のアプリから何かをインポートする際には注意が必要です。

関連する問題