2

How can I make mocha run with System.js handling all require/import statements? I have a React component which does an import of CSS file. When running on browser System.js gets loaded and it's plugin-css handles import of css files. But when running as a Mocha unit test import of a css file causes Mocha to crash.

import '../../../dist/css/common/NavBar.css!';

throws error Error: Cannot find module '../../../dist/css/common/NavBar.css!'

Does anyone have a similar test setup up and running ?

2 Answers 2

1

You're probably gonna need to start using Karma and a plugin for System.js

https://www.npmjs.com/package/karma-systemjs

Here's a karma.conf.js to get you started:

module.exports = function(config) {
    config.set({

        browsers: [ 'Chrome' ],

        singleRun: false,

        frameworks: ['mocha', 'sinon', 'systemjs'],

        plugins: [
            'karma-systemjs'
            'karma-chrome-launcher',
            'karma-chai',
            'karma-mocha',
            'karma-sourcemap-loader',
            'karma-spec-reporter',
            'karma-mocha-reporter',
            'karma-sinon'
        ],

        files: [
            'test/bootstrap.js'
        ],

        reporters: [ 'spec' ],

        systemjs: {
            // Path to your SystemJS configuration file 
            configFile: 'app/system.conf.js',

                // Patterns for files that you want Karma to make available, but not loaded until a module requests them. eg. Third-party libraries. 
                    serveFiles: [
                'lib/**/*.js'
            ],

                // SystemJS configuration specifically for tests, added after your config file. 
                // Good for adding test libraries and mock modules 
                    config: {
                paths: {
                    'angular-mocks': 'bower_components/angular-mocks/angular-mocks.js'
                }
            }
        }

    });
};

and in test/bootstrap.js perhaps this:

//test/bootstrap.js
//put this in your test directory (include it with your tests or test recursively)

// setup globals
chai = require('chai');
chai.should();
chai.use(require('chai-things'));
assert = require('chai').assert;
expect = require('chai').expect;
should = require('chai').should;
React = require('react/addons');
global.TestUtils = React.addons.TestUtils;
ReactTools = require('react-tools');
reactStub = React.createClass({
                        displayName: 'StubClass',
                        mixins: [],
                        render: function() {
                            return null;
                        }
                    });


// setup
before(function(){

});
beforeEach(function(){

});

// teardown
afterEach(function() {

});
after(function(){

});

// include all the tests
var context = require.context('./', true, /-test\.js$/);
context.keys().forEach(context);
Sign up to request clarification or add additional context in comments.

1 Comment

I had mocha running in nodejs itself, please checkout my answer. But I think in the longer run I will move to this karma setup. Seems like karma is better maintained and has good support for coverage and all.
1

I got it working in nodejs itself. I just had to stub out imports to css files. Rest of stuff babel handles. This is my require file which mocha uses.

process.env.BABEL_DISABLE_CACHE = 1;

require('babel-core/register')({
  'optional': [
    'es7.classProperties'
  ],
  'resolveModuleSource': function (source) {
    if (source.indexOf('dist/css') !== -1) {
      return '../../../test/css-dummy.js';
    }
    return source;
  },
  'blacklist': []
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.