【问题标题】:Karma Jasmine AngularJs define is not definedKarma Jasmine AngularJs 定义未定义
【发布时间】:2018-03-16 23:32:31
【问题描述】:

我正在使用 karma Jasmine 为 angularjs 应用程序编写单元测试。当我尝试运行命令 karma start 时,它会引发类似 Uncaught ReferenceError: define is not defined 的错误。基本上我正在尝试加载一个单独的模块(其文件以 define 关键字开头。)它在我的应用程序模块中使用 karma 配置文件在文件部分具有依赖关系。

我不知道为什么会这样,任何帮助将不胜感激。

【问题讨论】:

    标签: javascript angularjs karma-runner karma-jasmine


    【解决方案1】:

    我遇到了类似的问题,但找到解决方案并不容易。 下次提供一些代码,在这种情况下是你的 Karma 和 Jasmine 配置文件,因为它们负责这类问题。 你可能没有使用或没有为 Karma 配置过 RequireJS。 这个链接应该很有帮助。 Karma - RequireJS documentation

    只需在控制台中输入“karma init”,为 Require.js 选择“yes”。更多详情见链接。

    非常重要的是要有正确的文件夹结构并在你有 karma.conf.jstest-main.js(RequireJS 配置文件)的地方播放。

    经过数小时的研究和反复试验,我决定使用的示例结构。

    .
    - karma
    ├── karma.conf.js
    ├── build
    |   ├── js
    |   └── tests
    |        ├── test-main.js
    |        ├── tes
    

    karma.conf.js

    // Karma configuration
    // Generated on Fri Mar 16 2018 09:14:49 GMT+0100 (CET)
    
    module.exports = function(config) {
      config.set({
    
        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '',
    
    
        // frameworks to use
        frameworks: ['jasmine', 'requirejs'],
    
    
        // list of files / patterns to load in the browser
        files: [
            'build/tests/test-main.js',
            {pattern: 'build/**/*.js', included: false},
            {pattern: 'documents/exampleData/**/*.js', included: false},
            {pattern: 'node_modules/**/*-min.js', included: false},
            {pattern: 'node_modules/d3/build/d3.js', included: false},
            {pattern: 'node_modules/jquery/dist/jquery.min.js', included: false},
            {pattern: 'build/tests/**/*.js', included: false},
        ],
    
    
        // list of files / patterns 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'],
    
    
        // 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
      })
    }
    

    test-main.js

    var tests = [];
    var TEST_REGEXP = /(spec|test)\.js$/i;
    var BASE_URL = '/base/build/js';
    var BASE_URL_REGEXP = /^\/base\/build\/js\/|\.js$/g;
    
    // Get a list of all the test files to include
    Object.keys(window.__karma__.files).forEach(function (file) {
        if (TEST_REGEXP.test(file)) {
          var normalizedTestModule = file.replace(BASE_URL_REGEXP, '')
            tests.push(normalizedTestModule)
        }
    })
    
    require.config({
      // Karma serves files under /base, which is the basePath from your config file
        baseUrl: BASE_URL,
        paths: {
            'env': 'env',
            'Utils': './utils/utils',
            'exampledata' : '../../documents/exampleData',
            'jquery': '../../node_modules/jquery/dist/jquery.min',
            'underscore': '../../node_modules/underscore/underscore-min',
            'backbone': '../../node_modules/backbone/backbone-min',
            'backbone.touch': '../../node_modules/backbone.touch/dist/backbone.touch.min',
            'd3' : '../../node_modules/d3/build/d3'
        },
        shim: {
            'underscore': {
                exports: '_'
            }
        },
    
        deps: tests,
    
      // we have to kickoff jasmine, as it is asynchronous
      callback: window.__karma__.start
    })
    

    这个解决方案目前可能并不完美,但它确实有效。如果我在研究开始时能找到类似的东西,我会非常满意。 当我对测试框架配置的最佳实践有点熟悉时,我会更新它。

    【讨论】:

    • 嗨@Jakub 每次我运行 karma start 我都会得到:WARN [web-server]: 404: /.tmp/text.js。什么是 text.js?是生成的吗?我在打字稿中创建我的测试并将其转换为 .tmp 文件夹我将我的 BASE_URL 设置为 /.tmp/' 你能帮帮我吗?
    • 06/2019 - 这是正确的答案,我的 karma.conf.js 文件配置错误,我的 frameworks 属性缺少 requirejs 以及其他 jasmine 方法访问。通过再次初始化我的业力配置,我解决了与 Nilesh 类似的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2015-09-03
    • 2018-10-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多