您好,我正在尝试在d3应用程序上进行一些茉莉花/业力测试,但是我无法克服此错误:ReferenceError:找不到变量:d3 ...

at getSvg (C:/Users/test/Desktop/bob/angular-force-directed-graph/src/app/d3mapping.spec.js:9)
            at C:/Users/test/Desktop/bob/angular-force-directed-graph/src/app/d3mapping.spec.js:9
        ReferenceError: Can't find variable: d3


我知道这与直到window.load之前未定义d3有关,但是我不确定如何正确定义d3及其使用的方法。

// Karma configuration
// Generated on Thu Oct 29 2015 14:09:24 GMT-0400 (Eastern Daylight Time)

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

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


        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['jasmine'],


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

            'home/*.js',
            'common/*.js',
            'home/home.module.js',


            'app.js',
            '**/*.module.js',
            '**/*.controller.js',
            '**/*.service.js',

            '**/*.directive.js',
            '**/*.routes.js',

            '**/*.spec.js'
        ],


        // list of files to exclude
        exclude: [
            'bower_components/**/!(angular*|angular-mocks|angular-route*).js'
        ],


        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors: {
            '!(bower_components)/**/!(*spec).js': 'coverage',
            '*.js': 'coverage'
        },

        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress', 'coverage'],

        coverageReporter: {
            dir: '../coverage/',
            subdir: 'report'
        },

        captureTimeout: 30000,

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


        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: true,

        // Concurrency level
        // how many browser should be started simultanous
        concurrency: Infinity
    })
};


javascript - ReferenceError:找不到变量:d3-LMLPHP

最佳答案

将d3添加到您的karma.conf.js文件中

如果您的项目结构如下所示

|--app
| |--app.js
| |--...
|
|--test
| |--karma.conf.js
|
|--bower_components


您必须将basePath设置为“ ../”,因为karma.conf.js文件位于测试文件夹中,但是您想通过“ app / app.js”或“ bower_components / ...”来寻址这些文件

config.set({
    basePath: '../' // dont forget the right basePath!
    files:[
        'path/to/d3'
    ],
    ...
})

10-05 20:48
查看更多