未捕获的ReferenceError:因果报应启动karma.conf.js上未定义require [英] Uncaught ReferenceError: require is not defined on karma start karma.conf.js

查看:104
本文介绍了未捕获的ReferenceError:因果报应启动karma.conf.js上未定义require的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Karma和Jasmine在Rails应用程序的前端进行单元测试.看来我已经尽力解决此错误,并且在package.json中还剩下一百万个依赖项.这是我的Karma.conf.js:

Using Karma and Jasmine for unit testing on the angular front-end of a rails app. It appears I've done everything known to man to get through this error and I'm left with a million dependencies in my package.json. Here is my Karma.conf.js:

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

// list of files / patterns to load in the browser
files: [
   //angular mocks
  'bower_components/angular/angular.js',
  'bower_components/angular-mocs/angular-mocks.js',
  'bower_components/angular-resource/angular-resource.js',

  //load modules
  'public/app/app.js',

  //test file locations
  'app/**/*.js',
  'spec/**/*.js',
  'public/**/*.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 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', 'Firefox'],


// 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,

plugins : [
  'karma-requirejs',
  'karma-jasmine',
  'karma-chrome-launcher',
  'karma-firefox-launcher',
  'karma-browserify'
],

frameworks: ['jasmine', 'browserify']

  })
}

在这里我做错了什么吗?我希望在那里,好像我已经在这里中途实施了几个解决方案,而不完全知道发生了什么.谢谢!

Is there something obvious I'm doing wrong here? I'm hoping there is, it looks like I've halfway implemented a couple solutions here without exactly knowing what is going on. Thanks!

第一行出现'require',我的app.js文件出现错误

I'm getting the error on my app.js file on the first line with 'require'

推荐答案

浏览器不了解require,因此您需要在将文件提供给浏览器之前对其进行预处理.您可以将Webpack配置为karma.config,这样karma可以在测试之前使用webpack预处理文件.您还需要安装karma webpack,

Browser don't understand require so you need to pre-process your files before you serve them to the browser.You can config webpack into karma.config so karma can use webpack to preprocess your files before testing. You also need karma webpack install with,

npm我--save-dev karma-webpack

npm i --save-dev karma-webpack

有很多方法可以做到这一点,我就是这样做的.

There are many ways to do this, i did this way.

var path = require('path');
var webpackConfig = require('./webpack.config');
var entry = path.resolve(webpackConfig.context, webpackConfig.entry);
var preprocessors = {};
preprocessors[entry] = ['webpack'];
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: ['chai','mocha'],


    // list of files / patterns to load in the browser
    files: [entry],
    webpack:webpackConfig,

    // 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:preprocessors,


    // 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 simultanous
    concurrency: Infinity,
    plugins:[
      require('karma-webpack'),
      ('karma-chai'),
      ('karma-mocha'),
      ('karma-chrome-launcher')
    ]
  })
}

这是我与业力合作的种子, webpack angularjs .

here is a seed i worked on with karma, webpack, angularjs.

看看,祝你好运.

这篇关于未捕获的ReferenceError:因果报应启动karma.conf.js上未定义require的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆