使用karma,jasmine 2.0,requirejs和angularjs设置测试框架的问题

时间:2014-06-10 03:03:39

标签: angularjs requirejs jasmine karma-runner

我正在尝试使用karma,jasmine 2.0,requirejs和angular设置测试框架。但是当我运行'karma start'时,我得到'未捕获的错误:模块名称“shelljs”尚未加载上下文:_。使用require([])'。我不知道为什么会这样。有人可以帮帮我吗?感谢。

我的折叠结构:

$ tree
.
|-- index.html
|-- karma.conf.js
|-- app
|   |-- bower_components
        |-- angular
        |-- requirejs
        |-- angular-mocks
        |-- jasmine
        |-- ...
    |-- scripts
        |-- model
            |-- User.js
        |-- main.js
        |-- ...
-- test
    |-- spec
    |-- model
        |-- userSpec.js
    |-- test-main.js

karma.conf.js:

// Karma configuration
// Generated on Mon Jun 02 2014 09:54:34 GMT+1000 (AUS Eastern Standard Time)

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', 'requirejs'],

            // list of files / patterns to load in the browser
            files: [
                {pattern: 'app/bower_components/**/*.js', included: false},
                {pattern: 'app/scripts/*.js', included: false},
                {pattern: 'app/scripts/**/*.js', included: false},
                {pattern: 'test/**/*Spec.js', included: false},
                {pattern: 'test/test-main.js', included: true},

            ],


            // list of files to exclude
            exclude: [
                'app/bower_components/angular-bootstrap/ui-bootstrap.js',
                'app/bower_components/**/main.js',
                'app/scripts/main.js',
                'app/scripts/*min.js',
                'app/scripts/**/*min.js'
            ],


            // 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'],
            /*browsers: ['Chrome', 'IE', 'PhantomJS', 'Safari', 'Firefox'],*/

            plugins: [
                'karma-chrome-launcher',
                'karma-firefox-launcher',
                'karma-IE-launcher',
                'karma-PhantomJS-launcher',
                'karma-script-launcher',
                'karma-jasmine',
                'karma-requirejs'
            ],

            // Continuous Integration mode
            // if true, Karma captures browsers, runs the tests and exits
            singleRun: false`enter code here`
        }
    )
    ;
    console.log('karma config done--------------------------------');
}

测试main.js:

var allTestFiles = [];
/*var TEST_REGEXP = /(spec|test)\.js$/i;*/
//Replace this line with something more specific to your project and make sure it does not match files in your libraries:
var TEST_REGEXP = /spec\.js$/i;

var pathToModule = function (path) {
    return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function (file) {
    /*
     console.log(file);
     */
    if (TEST_REGEXP.test(file)) {
        // Normalize paths to RequireJS module names.
        allTestFiles.push(file);
        //allTestFiles.push(pathToModule(file));


    }
});

require.config({
    // Karma serves files under /base, which is the basePath from your config file
    baseUrl: '/base/app/scripts',

    paths: {
        /*'require': '../bower_components/requirejs/require',*/
        'domReady': '../bower_components/requirejs-domready/domReady',
        'jquery': '../bower_components/jquery/dist/jquery',
        'angular': '../bower_components/angular/angular',
        'angularResource': '../bower_components/angular-resource/angular-resource',
        'angularTranslate': '../bower_components/angular-translate/angular-translate',
        'uiBootstrap': '../bower_components/angular-bootstrap/ui-bootstrap-tpls',
        'uiRouter': '../bower_components/angular-ui-router/release/angular-ui-router',
        'angularGrid': '../bower_components/angular-grid/build/ng-grid',
        'stacktrace': '../bower_components/stacktrace/stacktrace',
        'angularMocks': '../bower_components/angular-mocks/angular-mocks',
        'jasmine': '../bower_components//jasmine/lib/jasmine-core/jasmine'
    },

    // angular does not support AMD out of the box, put it in a shim
    shim: {
        'angular': { deps: ['jquery'], exports: 'angular'},
        'angularResource': { deps: ['angular'] },
        'angularTranslate': { deps: ['angular'] },
        'uiBootstrap': { deps: ['angular'] },
        'uiRouter': { deps: ['angular'] },
        'angularGrid': { deps: ['angular'] },
        'angularMocks': {deps: ['angular'], 'exports': 'angularMocks'}
    },
    // dynamically load all test files
    deps: allTestFiles,

    // we have to kickoff jasmine, as it is asynchronous
    callback: window.__karma__.start


});

的package.json:

{
  "name": "example",
  "version": "0.0.0",
  "dependencies": {
    "grunt-contrib-compress": "0.7.0"
  },
  "devDependencies": {
    "grunt": "0.4.1",
    "grunt-autoprefixer": "0.4.0",
    "grunt-concurrent": "0.4.1",
    "grunt-contrib-clean": "0.5.0",
    "grunt-contrib-coffee": "0.7.0",
    "grunt-contrib-compass": "0.6.0",
    "grunt-contrib-concat": "0.3.0",
    "grunt-contrib-connect": "0.5.0",
    "grunt-contrib-copy": "0.4.1",
    "grunt-contrib-cssmin": "0.7.0",
    "grunt-contrib-htmlmin": "0.1.3",
    "grunt-contrib-imagemin": "0.3.0",
    "grunt-contrib-jshint": "0.7.1",
    "grunt-contrib-uglify": "0.2.0",
    "grunt-contrib-watch": "0.5.2",
    "grunt-google-cdn": "0.2.0",
    "grunt-newer": "0.5.4",
    "grunt-ngmin": "0.0.2",
    "grunt-rev": "0.1.0",
    "grunt-svgmin": "0.2.0",
    "grunt-usemin": "2.0.0",
    "jshint-stylish": "0.1.3",
    "load-grunt-tasks": "0.3.0",
    "time-grunt": "0.2.1",
    "karma-ng-scenario": "0.1.0",
    "requirejs": "2.1.9",
    "karma-requirejs": "^0.2.2",
    "karma-coffee-preprocessor": "0.1.1",
    "karma-phantomjs-launcher": "0.1.1",
    "karma-ng-html2js-preprocessor": "0.1.0",
    "grunt-bower-install": "0.7.0",
    "yo": "1.1.2",
    "bower": "1.2.8",
    "generator-karma": "0.6.0",
    "generator-angular": "0.7.1",
    "generator-generator": "0.4.1",
    "grunt-contrib-requirejs": "0.4.1",
    "grunt-karma": "^0.8.3",
    "karma-firefox-launcher": "^0.1.3",
    "karma-chrome-launcher": "^0.1.4",
    "karma-ie-launcher": "^0.1.5",
    "karma-script-launcher": "^0.1.0",
    "karma-html2js-preprocessor": "^0.1.0",
    "glob": "^4.0.2",
    "karma": "^0.12.0",
    "karma-jasmine": "^0.2.0"
  },
  "engines": {
    "node": ">=0.8.0"
  },
  "scripts": {
    "test": "grunt test"
  }
}

bower.json:

{
  "name": "myEmail",
  "version": "1.0.0",
  "dependencies": {
    "angular": "1.2.14",
    "json3": "3.3.0",
    "es5-shim": "2.3.0",
    "angular-resource": "1.2.14",
    "angular-cookies": "1.2.14",
    "angular-sanitize": "1.2.14",
    "angular-route": "1.2.14",
    "bootstrap": "3.1.1",
    "respond": "1.4.2",
    "requirejs": "2.1.11",
    "requirejs-domready": "2.0.1",
    "angular-ui-router": "0.2.10",
    "angular-translate": "2.0.1",
    "angular-http-auth": "1.2.1",
    "angular-grid": "2.0.7",
    "jquery": "1.11.0",
    "angular-bootstrap": "0.10.0",
    "stacktrace": "0.6.0"
  },
  "devDependencies": {
    "angular-mocks": "1.2.14",
    "jasmine": "~2.0.0"
  }
}

2 个答案:

答案 0 :(得分:0)

当我在karma.conf.js中的TEST_REGEXP不够具体并且拉入&my; my_client / app / bower_components / jasmine / src / core / Spec.js:1'时,我看到了这个错误。作为要执行的spec文件。一旦我确定TEST_REGEXP只捕获我的规格,这个错误就消失了。 (据推测,问题是任何未实际定义为AMD的规范都会产生依赖性问题。)

所以我的规格的一般模式是" original_file_name.spec.js"。切换

var TEST_REGEXP = /spec\.js$/i; 

var TEST_REGEXP = /\.spec\.js$/i;

正确捕捉到这种模式。 :)

答案 1 :(得分:0)

你应该在你的package.json文件中插入“shelljs”:“^ 0.2.6”