5

I am new to node.js stack like npm, gulp etc. I had wrote some JavaScript unit test cases earlier, and now want to use Karma as test runner. However, after a few attempts I am completely lost now.

To start, I have following project configurations:

  • SystemJS as module loader.
  • Folder structre (some are omitted for brevity):

    project
    |
    |--build //contains all gulp tasks
    |
    |--dist //contains the output (*.html, *.js etc.)
    |
    |--jspm_packages 
    |
    |--node_modules
    |
    |--src //contains the source .html, and *.ts
    |
    |--tests //contains unit tests in *.ts. I also have a gulp task to build tests/**/*.ts to *.js in the same folder.
    |
    |--typings //contains type definitions
    |
    |--config.js //contains SystemJS configuration
    |
    |--karma.conf.js
    
  • karma.conf.js

    // Karma configuration        
    
    module.exports = function (config) {
        config.set({
            basePath: '',
            frameworks: [ 'qunit'],
            files: [
                'jspm_packages/system.js',
                //'config.js',
                'dist/custom/*.js' //,
                //'tests/**/*.js'
            ],
            exclude: [        ],
            preprocessors: {        },
            reporters: ['progress'],
            port: 9876,
            colors: true,
            logLevel: config.LOG_INFO,
            autoWatch: true,
            browsers: ['Chrome'],
            singleRun: false,
            concurrency: Infinity
        });
    }
    

However, whenever I do karma start, I have error Uncaught TypeError: Unexpected anonymous System.register call.. Is there anyway to resolve this?

Other Attempts:

  1. Tried to create a gulp task, according to https://github.com/karma-runner/gulp-karma. But have same problem.
  2. Tried to use karma-systemjs. And the karma.conf.js was different as follows:

    // Karma configuration        
    
    module.exports = function (config) {
        config.set({
            ...
            frameworks: [ 'systemjs', 'qunit'],
            plugins: ['karma-systemjs', 'karma-chrome-launcher', 'karma-firefox-launcher'],
            systemjs: {
                configFile: 'config.js',
                serveFiles: ['jspm_packages/**/*.js'],
                config: {
                    transpiler: 'babel'
                }
            },
            ...
        });
    }
    

    And in this case I got following error, though I have both SystemJs and Babel installed:

    [WARNING] Looking up paths with require.resolve() is deprecated.
    Please add "systemjs" to your SystemJS config paths.
    Cannot find "systemjs".
      Did you forget to install it ?
      npm install systemjs --save-dev
    [WARNING] Looking up paths with require.resolve() is deprecated.
    Please add "babel" to your SystemJS config paths.
    Cannot find "babel-core".
      Did you forget to install it ?
      npm install babel-core --save-dev
    

Any help in this regard will be great.

Sayan Pal
  • 4,768
  • 5
  • 43
  • 82
  • 1
    Perhaps try adding `systemjs` to the `frameworks` property in your first Karma configuration file. If that doesn't work, then add the `systemjs` property from your second karma conf file to the first one. Exclude the `plugins` property for now. – Healforgreen Feb 11 '16 at 15:35
  • 1
    @GenYDeveloper Thank You for your comment. this seems to work as it results in removal of the `SystemJS` error. Additionally asking, now Karma ignores the test cases completely and not running any of them. The message on console is `Executed 0 of 0 ERROR`. Any reason why? – Sayan Pal Feb 17 '16 at 06:34
  • Did you uncomment the test files array value on the `files` property? – Healforgreen Feb 17 '16 at 12:57
  • Yes I have done so and currently testing for unit tests of only one module: `tests/custom/MyTargetModule.js`. – Sayan Pal Feb 17 '16 at 13:08
  • Unfortunately I have never run into this problem myself, and don't know a solution off the top of my head. I am busy right now, but will try to look at it when I have time. – Healforgreen Feb 17 '16 at 13:12
  • @GenYDeveloper That's ok. Thank you anyway for your help. :) – Sayan Pal Feb 17 '16 at 13:37

0 Answers0