2016-04-15 160 views
0

我一直在试图把使用业力和反应,并最终编写测试React非常简单的演示,但我一直坚持过去10个小时和我希望以前有人遇到过这种情况,可以告诉我我错过了什么,或者没有理解什么。karma-webpack无法找到模块“反应”

这里是我的karma.conf.js:

// Karma configuration 
// Generated on Sun Apr 10 2016 10:10:34 GMT-0700 (PDT) 

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 

    resolve: { 
     extensions: ['', '.js', '.jsx','.ts'] 
    }, 
    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['commonjs','jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
     // only specify one entry point 
     // and require all tests in there 
     //'node_modules/babel-polyfill/browser.js', 
     //'node_modules/react/react.js', 
     'test_index.js' 
    ], 

    // list of files to exclude 
    exclude: [ 
    ], 

    preprocessors: { 
     'node_modules/react/react.js':['babel','commonjs'], 
     'src/**/*.js':['babel','commonjs'], 
     'spec/**/*.js':['babel','commonjs'], 
     'test_index.js': ['babel','commonjs','webpack'] 
    }, 

    babelPreprocessor: { 
     options: { 
     presets: ['es2015','react'] 
     } 
    }, 




    webpack: { 
     module : { 
     loaders: [ { 
      loader : 'babel-loader', 
      query: { 
      presets: ['es2015','react'] 
      } 
     } 
     ] 
     } 
    }, 

    webpackMiddleware: { 
     // webpack-dev-middleware configuration 
     // i. e. 
     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: false, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: [], 


    // 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 
    }) 
} 

当我人缘来看,我得到以下错误:

Chrome 49.0.2623 (Mac OS X 10.10.5) ERROR 
    Uncaught Error: Could not find module 'react' from '/Users/chris/web-projects/project-template/src/scripts/app.js' 
    at /Users/chris/web-projects/project-template/node_modules/karma-commonjs/client/commonjs_bridge.js:85 

如果你想看看我的任何其他文件,您可以访问回购的当前分支在这里:

​​

感谢您提供任何帮助! 克里斯

回答

0

更改您的resolve

resolve: { 
    extensions: ['', '.js', '.jsx','.ts'] 
}, 

resolve: { 
    modulesDirectories: [ 
    'node_modules' 
    ], 
    extensions: ['', '.json', '.js'] 
}, 
0

OK,我想我得到了这个工作。我认为我的问题是太多的预处理器,但我其实并不确定。这是我现在karma.conf.js:

// Karma configuration 
// Generated on Sun Apr 10 2016 10:10:34 GMT-0700 (PDT) 

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: [ 
     // only specify one entry point 
     // and require all tests in there 
     //'node_modules/babel-polyfill/browser.js', 
     //'node_modules/react/react.js', 
     'test_index.js' 
    ], 

    // list of files to exclude 
    exclude: [ 
    ], 

    preprocessors: { 
     'src/**/*.js':['babel','webpack'], 
     'spec/**/*.js':['babel','webpack'], 
     'test_index.js': ['babel','webpack'] 
    }, 

    babelPreprocessor: { 
     options: { 
     presets: ['es2015','react'] 
     } 
    }, 

    webpack: { 
     module : { 
     loaders: [ { 
      loader : 'babel-loader', 
      query: { 
      presets: ['es2015','react'] 
      } 
     } 
     ] 
     } 

    }, 

    webpackMiddleware: { 
     // webpack-dev-middleware configuration 
     // i. e. 
     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: false, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: [], 


    // 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 
    }) 
} 

它的模块加载罚款了,但现在它告诉我:

未捕获不变违规:_registerComponent(...):目标容器不是一个DOM元素。

我没有得到这个,当我用webpack正常构建,只是通过业力。