使用 Jasmine 和 Karma 在 ASP.NET MVC 中进行单元测试 JavaScript

Unit Testing JavaScript in ASP.NET MVC with Jasmine and Karma

我正在编写示例 ASP .NET MVC 应用程序,它具有 karma 和 requirejs 来测试脚本。我看到下面的错误 Uncaught (in promise) Error: No tests were 运行.

我在命令提示符中看到以下错误 Chrome 77.0.3865 (Windows 10.0.0):执行 0 次,共 0 次错误(0.012 秒/0 秒)

Package.json

{
  "name": "karmawebapp",
  "version": "1.0.0",
  "description": "test application for karma tests",
  "main": "index.js",
  "scripts": {
    "test": "karma start"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-preset-es2015": "^6.24.1",
    "karma": "^4.3.0",
    "karma-chrome-launcher": "^3.1.0",
    "karma-qunit": "^4.0.0",
    "karma-requirejs": "^1.1.0",
    "protractor": "^5.4.2",
    "qunit": "^2.9.3",
    "requirejs": "^2.3.6"
  }
}

我的karma.conf.js

// Karma configuration
// Generated on Tue Oct 15 2019 13:51:47 GMT-0500 (Central Daylight 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: ['requirejs', 'qunit'],


    // list of files / patterns to load in the browser
    files: [
      'test-main.js',
      { pattern: './Scripts/tests/*.specs.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

}) }

我的测试-main.js

var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;

// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(file) {
    if (TEST_REGEXP.test(file)) {
        // Normalize paths to RequireJS module names.
        // If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
        // then do not normalize the paths
        var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
        allTestFiles.push(normalizedTestModule)
    };
});

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

    // dynamically load all test files
    deps: allTestFiles,

    path: {
        qunit: 'qunit',
        jquery: 'jquery.3.3.1'
    },

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

我的test.specs.js

define(['qunit'], function (qunit) {

    qunit.test("Second test", function (assert) {
        assert.ok(true, "Passed!");
    });
});

使用调试设置在浏览器上打开devtools: http://karma-runner.github.io/4.0/intro/troubleshooting.html

我能够 运行 这些测试用例。每当它们在您的规格文件中出现错误时,就会查看 No tests were 运行 消息。您需要确保您的规格文件语法正确