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

标签 unit-testing reactjs mocha.js systemjs

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

导入 '../../../dist/css/common/NavBar.css!';

抛出错误 错误:找不到模块“../../../dist/css/common/NavBar.css!”

有人有类似的测试设置并正在运行吗?

最佳答案

您可能需要开始使用 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);

关于unit-testing - 使用 System.js 处理 css 导入的 React 组件的 Mocha 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33794781/

相关文章:

reactjs - 为什么在react-select中使用自定义样式时出现 "No overload matches this call"TypeScript错误?

ruby-on-rails - 当我运行功能测试时,如何在 Ruby on Rails 应用程序中禁用救援处理程序?

javascript - react : How to refresh the data in the table after button click

reactjs - 如何设置/重置属性以在运行时 react 元素

javascript - 在 React 组件内的 div 上模拟 ('scroll' )返回 `AssertionError: expected false to be true`

javascript - Chai-As-Promised 即使错误也会通过

javascript - Mocha JS : How to reference members from asynch before function in describe/it test functions

c# - ASP.NET Core 中的模拟 IHttpConnectionFeature

Java JUnit - 是否可以确定是否抛出异常以及抛出哪个异常?

unit-testing - 在 GoLang 的 sqlmock 中出错