0
votes

I have a small set of Jasmine tests that I'm attempting to run with Karma. Any help is hugely appreciated.

I can run the tests just fine if I run the command "jasmine". However, when I try to use Karma, (karma start --singleRun=true), the browser opens but nothing further happens until Karma eventually times out with this:

26 06 2019 14:40:24.431:INFO [karma]: Delaying execution, these browsers are not ready: Chrome 75.0.3770 (Windows 10.0.0)
26 06 2019 14:42:01.326:WARN [Chrome 75.0.3770 (Windows 10.0.0)]: Disconnected (0 times), because no message in 160000 ms.
26 06 2019 14:42:01.327:DEBUG [Chrome 75.0.3770 (Windows 10.0.0)]: CONFIGURING -> DISCONNECTED
Chrome 75.0.3770 (Windows 10.0.0) ERROR
  Disconnected, because no message in 160000 ms.
26 06 2019 14:42:01.330:DEBUG [launcher]: CAPTURED -> BEING_KILLED
26 06 2019 14:42:01.332:DEBUG [launcher]: BEING_KILLED -> BEING_FORCE_KILLED
26 06 2019 14:42:01.333:WARN [karma]: No captured browser, open http://localhost:9876/

Chrome 75.0.3770 (Windows 10.0.0): Executed 0 of 0 DISCONNECTED (2 mins 40.011 secs / 0 secs)

If I instead run with --singleRun=false and set breakpoints in my test, then run "karma run karma.conf.js" from a new command prompt, I noticed that breakpoints inside the implementation callbacks for my tests never get hit. Other breakpoints get hit just fine.

E.g. in the test below:

  describe(">String Utils", function() {
      it("should be able to lower case a string",function() {
          expect(utils.toLowerCase).toBeDefined();
          expect(utils.toLowerCase("HELLO WORLD")).toEqual("hello world");
      });

A breakpoint on the "describe" or "it" functions will get hit. But a breakpoint on either "expect" never gets hit.

I've stepped into the "it" functions to see if there's ever an error trying to setup the jasmine spec, but everything seems fine.

However, Karma never calls the implementation callbacks. And the browser continually stays "idle"

Here is my karma.conf.js. Below it is my spec-bundle.js, and below that is my webpack.

If there's anything else you'd like to see, I can post it or you can view it on my github repo: https://github.com/webgirlwonder/karma-jasmine-test

// Karma configuration
// Generated on Fri May 17 2019 10:37:04 GMT-0500 (Central Daylight Time)

var webpackConfig = require('./webpack.config.js');

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','requirejs'],

    // list of files / patterns to load in the browser
    files: [
      'spec-bundle.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: {
      // '*.js': [ 'webpack' ],
      // 'spec/*Spec.js': ['webpack' ]
    // },

    preprocessors: {
      'spec-bundle.js': ['webpack' ]
    },


    webpack: webpackConfig,


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


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


    /* // 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: true, */

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity,
    captureTimeout: 160000,
    browserNoActivityTimeout: 160000,
  })
}

spec-bundle.js

/*
 * Create a context for all tests files below the src folder and all sub-folders.
 */
const context = require.context('./spec/', true, /\Spec\.js$/);

/*
 * For each file, call the context function that will require the file and load it up here.
 */
context.keys().forEach(context);

webpack

const config =  {
    "mode": "development",
    "entry": "./spec/MyJSUtilities.spec.js",
    "target": "node",
    "output": {
        "path": __dirname+'/static',
        "filename": "[name].[chunkhash:8].js"
    }
}

module.exports = config;
1
Raised a pull request.Sreeram Padmanabhan

1 Answers

0
votes

Here is a configuration that works for me:

.babelrc

{
    "presets": ["@babel/preset-env" ]
}

karma.conf.js

const webpackConfig = require('./webpack.config');

module.exports = function (config) {
  config.set({
    basePath: '.',
    autoWatch: false,
    singleRun: true,
    browsers: [ 'Chrome' ],
    frameworks: [ 'jasmine' ],
    files: [
      './src/test.js',
    ],
    preprocessors: {
      './src/**/*.js': [ 'webpack' ],
    },
    webpack: webpackConfig,
    webpackMiddleware: {
      noInfo: true,
      stats: 'error-only',
    },
  });
};

webpack.config.js

module.exports = {
    mode: 'development',
    devtool: 'none',
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
          },
        },
      ],
    },
    externals: {
      request: {
        commonjs: 'request',
        commonjs2: 'request',
      },
      os: {
        commonjs: 'os',
        commonjs2: 'os',
      },
      process: 'process',
    },
  };

./src/test.js

const testsContext = require.context(".", true, /spec$/);
testsContext.keys().forEach(testsContext)