使用System.js处理css导入的React组件的Mocha测试

时间:2015-11-19 03:31:16

标签: unit-testing reactjs mocha systemjs

如何使用System.js处理所有require / import语句来运行mocha?我有一个React组件,它导入CSS文件。当在浏览器上运行时,System.js被加载并且plugin-css处理css文件的导入。但是当作为Mocha单元运行时,导入css文件会导致Mocha崩溃。

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

抛出错误 Error: Cannot find module '../../../dist/css/common/NavBar.css!'

是否有人有类似的测试设置并运行?

2 个答案:

答案 0 :(得分:1)

您可能需要开始使用Karma和System.js的插件

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

这里有karma.conf.js让你入门:

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

    });
};

并在test/bootstrap.js或许这样:

//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);

答案 1 :(得分:1)

我在nodejs本身工作了。我只需要将导入存根到css文件。剩下的东西巴贝尔处理。这是mocha使用的我需要的文件。

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': []
});