2017-03-07 4 views
1

カルマでジャスミンテストを実行しようとしていますが、ロードするモジュールの一部がロードされていないと思われます。アイデア。KarmaでWebpackを使用すると、ノードモジュールの読み込みエラーが発生する

karma.conf.js

var webpackConfig = require('./webpack.config.js'); 
webpackConfig.entry = {}; 

module.exports = function(config) { 
    config.set({ 
    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 
    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 
    // list of files/patterns to load in the browser 
    files: [ 
     { pattern: 'test-context.js'} 
    ], 
    // list of files to exclude 
    exclude: [ 
    ], 
    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
     'test-context.js': ['webpack'] 
    }, 
    webpack: webpackConfig, 
    webpackMiddleware: { 
     noInfo: true 
    }, 
    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 
    // web server port 
    port: 9876, 
    // enable/disable colors in the output (reporters and logs) 
    colors: true, 
    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 
    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 
    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Chrome'], 
    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false, 
    // Concurrency level 
    // how many browser should be started simultaneous 
    concurrency: Infinity, 
    debug: true 
    }) 
} 

webpack.config.js

var path = require('path'); 
var webpack = require('webpack'); 

module.exports = { 
    entry: [ 
    'babel-polyfill', 
    './index.js' 
    ], 
    output: { 
     publicPath: '/', 
     filename: 'main.js' 
    }, 
    devtool: 'source-map', 
    resolve: { 
    extensions: ['', '.js', '.jsx'] 
    }, 
    module: { 
    rules: [ 
     { 
     test: /\.json$/, 
     use: 'json-loader' 
     } 
    ], 
    loaders: [ 
     { 
     test: /\.js$/, 
     loader: 'babel-loader', 
     exclude: /node_modules/ 
     }, 
     { 
     test: /\.json$/, 
     loader: 'json-loader' 
     } 
    ] 
    }, 
    node: { 
    net: 'empty', 
    tls: 'empty', 
    //dns: 'empty', 
    fs: 'empty' 
    }, 
    externals: { 
    //'crypto': 'crypto' 
    }, 
    debug: true 
}; 

package.json

{ 
    "name": "Web-Application", 
    "version": "0.0.1", 
    "description": "A web application", 
    "engines": { 
    "node": "5.9.1" 
    }, 
    "main": "index.js", 
    "scripts": { 
    "start": "node index.js", 
    "tests": "karma start" 
    }, 
    "dependencies": { 
    "babel-preset-es2015": "^6.22.0", 
    "babel-preset-react": "^6.23.0", 
    "babel-register": "^6.23.0", 
    "box-node-sdk": "^1.3.0", 
    "ejs": "2.4.1", 
    "express": "4.13.3", 
    "node-libs-browser": "^2.0.0", 
    "crypto-browserify": "^3.11.0" 
    }, 
    "repository": { 
    "type": "git", 
    "url": "https://github.com/heroku/node-js-getting-started" 
    }, 
    "keywords": [ 
    "node", 
    "heroku", 
    "express" 
    ], 
    "license": "MIT", 
    "devDependencies": { 
    "babel-cli": "^6.23.0", 
    "babel-core": "^6.23.1", 
    "babel-loader": "^v6.4.0", 
    "babel-preset-env": "^1.2.0", 
    "babel-preset-es2015": "^6.22.0", 
    "express": "4.13.3", 
    "jasmine-core": "^2.5.2", 
    "json-loader": "^0.5.4", 
    "karma": "^1.5.0", 
    "karma-babel-preprocessor": "^6.0.1", 
    "karma-chrome-launcher": "^2.0.0", 
    "karma-jasmine": "^1.1.0", 
    "karma-phantomjs2-launcher": "^0.5.0", 
    "karma-webpack": "^2.0.2", 
    "webpack": "^1.3.0", 
    "webpack-dev-server": "1.10.1" 
    } 
} 

私は、以前の依存関係の問題を取り除くためにコードに

node: { 
    net: 'empty', 
    tls: 'empty', 
    //dns: 'empty', 
    fs: 'empty' 
    }, 

を追加しましたが、それは単にサービス層を固定している可能性があり、多分深く依存関係の問題があります。

スタックトレース:

enter image description here

enter image description here

Gitのソース

https://github.com/noobiehacker/revaBoxWeb

すべてのヘルプやヒントが理解されます> <

答えて

0

この例ではfs又はnet等Node.jsの組み込みモジュールを必要expressを使用Node.jsのアプリケーションです。しかし、webpack(デフォルトのターゲットはweb)を使ってWebアプリケーションを構築しようとしています。ブラウザではこれらのモジュールは利用できません。 Webpackはモジュールを解決できないと言いましたが、モジュールを空にすることに決めました。これによりランタイムエラーが発生します。

ウェブパックでexpressを使用するには、targetnodeを設定する必要があります。したがって、webpackは組み込みモジュールに触れません。

target: 'node' 

また、前に追加したnode設定を削除する必要があります。前述したように

I am trying to just run my Jasmine Test with Karma

、あなたはNode.jsのアプリを構築しているが、Karmaは、ブラウザのテストツールです。あなたのコードを実行することはできません。あなたのコードはノードによって実行され、ブラウザではなくノードによってテストされます。

+0

あなたのご意見ありがとうございました。私はあなたのソリューションを試しましたが、今、このエラーが発生しています: "Uncaught ReferenceError:requireが定義されていません"、おそらく私はブラウザにライブラリを読み込む方法カルマをまったく使ってもいいですか? – noobiehacker

+0

ブラウザにそれらをロードすることはできません.Webpackは、 'require( 'fs')'や他の組み込みモジュールを手作業で触れないようにして、ノードで直接使用できるようにします。 'require'はブラウザにも存在しません。ブラウザのファイルシステムにアクセスすることはできませんでした。エクスプレスアプリをテストするためにカルマを使用することはできません。その場合は何も必要ありませんが、ジャスミンはブラウザに依存しないほど十分です。 –

+0

あなたはchromeを使用せず、phantonjsをブラウザオプションとして使用することをお勧めしますか? – noobiehacker

関連する問題