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

861 Views Asked by At

I am writing a sample ASP .NET MVC application, which has karma and requirejs to test the scripts. I see below error Uncaught (in promise) Error: No tests were run.

I see below error in command prompt Chrome 77.0.3865 (Windows 10.0.0): Executed 0 of 0 ERROR (0.012 secs / 0 secs)

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"
  }
}

My 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

}) }

My test-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
});

my test.specs.js

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

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

There are 2 best solutions below

1
On BEST ANSWER

Use the debug set up to open devtools on the browser: http://karma-runner.github.io/4.0/intro/troubleshooting.html

0
On

I was able to run these test cases. Looks whenever their is error in your specs file No tests were run message will come. You need make sure you specs file syntax is correct