afterAll ReferenceError:模块未定义,nodejs中的Karma / Jasmine测试错误,引发错误

时间:2019-08-17 06:26:27

标签: javascript jasmine karma-jasmine karma-coverage

我正在尝试使用karma和jasmine作为框架为m个nodejs简单应用程序编写测试。我将karma-coverage用于预处理程序。 这是项目的结构: enter image description here

package.json

{
  "name": "tests",
  "version": "1.0.0",
  "description": "tests with karma in jasmine framework",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "karma start karma.conf.js"
  },
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "jasmine-core": "^3.4.0",
    "karma": "^4.2.0",
    "karma-chrome-launcher": "^3.1.0",
    "karma-coverage": "^1.1.2",
    "karma-jasmine": "^2.0.1"
  }
}

karma.conf.js

// Karma configuration
// Generated on Sat Aug 17 2019 09:37:53 GMT+0500 (Uzbekistan 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'],


        // list of files / patterns to load in the browser
        files: [
            'js/*.js',
            'test/*.test.js'
        ],


        // 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/*.test.js': ['coverage']
        },


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


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

        // optionally, configure the reporter
        coverageReporter: {
            type: 'html',
            dir: 'coverage/'
        }
    })
}

每当我运行npm test时,都会出现以下错误:

enter image description here

custom-lodash.js

class CustomLodash {

    compact(array) {
        let newArray = [];
        for (let i = 0; i < array.length; i++) {
            if (!array[i] || array[i] === undefined || array[i] === null) {
                continue;
            }

            this.push(array[i])(newArray);
        }
        return newArray;
    }


}

let _ = new CustomLodash;

module.exports = {
    compact: _.compact
};

custom-lodash.test.js

describe("CustomLodash", function () {

    let utils;
    //This will be called before running each spec
    beforeEach(function () {
        console.log('before each');
        utils = new CustomLodash();
    });


    describe("when calc is used to peform basic math operations", function () {
        it("creates an array with all falsey values removed", function () {
            // expect(utils.compact([0, 1, false, 2, '', 3])).toBe([1, 2, 3]);
            let compact = utils.compact([0, 1, false, 2, '', 3]);
            console.log('before each in it is: ', compact);
            expect(compact).toEqual([1, 2, 3]);
           //console.log('is defined ', expect(compact).toEqual([1, 2, 3]));
        });
    })


});

如您所见,console.log正在提供输出,这意味着正在读取文件。但是,当我调用toBe()toEqual()时,我可以看到输出未定义(也在console.log中进行了检查)。 真的赞赏。我搜索了许多答案和实际问题,但无法解决。

1 个答案:

答案 0 :(得分:0)

我想与我分享这种情况的解决方案。

添加browserify对我有用。另外,我添加了watchify来监视文件的增量编译。

  

Browserify的诞生是为了在浏览器中创建您的Node代码。截止日期   它仅支持通用的节点风格(包括JSON支持)   并为许多节点核心模块提供内置垫片。一切   否则是其他程序包。

在添加软件包以在浏览器和Node中运行我的ES6代码之后,我的package.json如下所示:

"dependencies": {
    "browserify": "^16.2.3",
    "jasmine-core": "^3.2.1",
    "karma": "^4.2.0",
    "karma-chrome-launcher": "^3.1.0",
    "karma-browserify": "^5.3.0",
    "karma-commonjs": "^1.0.0",
    "karma-coverage": "^1.1.2",
    "karma-jasmine": "^2.0.1"
    "watchify": "^3.11.0"
  }

不要忘记在您的karma.config.js配置中添加browserify,如下所示:

  • 将其添加到您的框架列表中

  • 将其添加到您的预处理器列表中。

示例:

preprocessors: {
      'src/**/*.js': ['coverage'],
      'js/**/*.js': ['browserify'],
      'test/**/*.[sS]pec.js': ['browserify']
    }

一个人可以通过构建webpack来解决ES6的这种问题,但是由于browserify在最小的配置下更有可能工作,因此我选择在这种情况下使用它作为解决方案。希望它能对某人有所帮助。