Karma + Mocha + React无法将Symbol值转换为字符串

时间:2016-01-08 12:13:28

标签: javascript reactjs mocha webpack karma-mocha

我使用webpack + babel作为React + Redux应用程序和Mocha + Karma进行测试。 redux测试用例正在正确执行。但是,当我尝试使用react-addons-test-utils进行DOM测试并使用Karma运行它时会出现此错误

未捕获的TypeError:无法将Symbol值转换为字符串   在http://localhost:9876/karma.js:339

为了正确调试,我在karma lib文件中放了几个记录器(我知道我不应该这样做)并且得到了这个

Karma Error for React DOM testing

然而,当我不使用KarmaJS并且只是尝试运行测试时,它看起来很好。这是我的karma.conf

"use strict";
let webpackConfig = require('./webpack.config');
const coverage = process.env.COVERAGE;

webpackConfig.externals = {};

getWebpackLoaders();

module.exports = function(config){
  config.set({
    basePath: '.',
    frameworks:['mocha'],
    autoWatchBatchDelay:500,
    browsers: ['Chrome'],
    customLaunchers: {
      Chrome_without_security: {
        base: 'Chrome',
        flags: ['--disable-web-security']
      }
    },
    preprocessors: {
      './test/**/*.js':['webpack']
    },
    reporters: getReporters(),
    coverageReporter: {
      reporters: [
        {type: 'lcov', dir: 'coverage/', subdir: '.'},
        {type: 'json', dir: 'coverage/', subdir: '.'},
        {type: 'text-summary'}
      ]
    },
    exclude:['node_modules'],
    port:9876,

    files: [
      'node_modules/react/dist/react-with-addons.js',
      'test/test.js'
    ],
    webpack:webpackConfig,
    plugins: [
      'karma-webpack',
      'karma-mocha',
      'karma-coverage',
      'karma-chrome-launcher'
    ]
  })
};

function getWebpackLoaders(){
  if(coverage){
    let loaders  = webpackConfig.module.loaders;
    let jsLoader = loaders[1];
    jsLoader.exclude = /node_modules|\.test\.js$/ //exclude both node_modules and test
    loaders.push({
      test:/\.test\.js$/,
      loaders:['babel-loader']
    });
    loaders.push({
      test: /\.js$/,
      loaders: ['isparta'],
      exclude: /node_modules|\.test.js$/ // exclude node_modules and test files
    })
  }
}

function getReporters() {
  var reps = ['progress'];
  if (coverage) {
    reps.push('coverage');
  }
  return reps;
}

编辑1.将webpack.config添加到此

var webpack = require('webpack');
var argv = require('minimist')(process.argv.slice(2));

var DEBUG = !argv.release;

var AUTOPREFIXER_LOADER = 'autoprefixer-loader?{browsers:[' +
  '"Android >= 4", "Chrome >= 20", "Firefox >= 24", ' +
  '"Explorer >= 9", "iOS >= 6", "Safari >= 6"]}';

var GLOBALS = {
  'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  '__DEV__': DEBUG
};

var config = {

  entry: './app.js',

  output: {
    filename: 'app.js',
    path: './build/',
    publicPath: './',
    sourcePrefix: '  '
  },

  externals: {
    react: 'React'
  },

  cache: DEBUG,
  debug: DEBUG,
  devtool: DEBUG ? '#inline-source-map' : false,

  stats: {
    colors: true,
    reasons: DEBUG
  },

  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.DefinePlugin(GLOBALS)
  ].concat(DEBUG ? [] : [
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin(),
    new webpack.optimize.AggressiveMergingPlugin()
  ]),

  resolve: {
    extensions: ['', '.webpack.js', '.js', '.jsx']
  },

  module: {
    preLoaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader'
      }
    ],

    loaders: [
      {
        test: /\.less$/,
        loader: 'style-loader!css-loader!' + AUTOPREFIXER_LOADER + '!less-loader'
      },
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.json$/,
        exclude: /node_modules/,
        loader: 'json-loader'
      }
    ]
  }
};

module.exports = config;

3 个答案:

答案 0 :(得分:5)

您的测试可能发现比较React元素时不匹配,但Mocha无法转换为字符串内部Symbol属性。

尝试在函数node_modules/mocha/lib/utils.js中围绕第602行编辑文件canonicalize并替换:

default:
  canonicalizedObj = value + '';

通过

default:
  canonicalizedObj = String(value);

然后,再次运行测试。这一次,摩卡应该能够告诉你出了什么问题。

答案 1 :(得分:5)

确保您没有尝试console.log getRenderOutput的结果。 这是我的问题。

答案 2 :(得分:3)

我刚刚使用完全相同的堆栈遇到了这个问题。

如果你使用TestUtils' shallowRenderer以及Redux的docs示例,当您尝试记录输出时,您很可能遇到此问题。字符串化输出,(JSON.stringify等)来解决问题。

@Ricado Stuven的回答:Mocha已将该行更新为value.toString()。截至本帖发布日期的最新版本(v2.3.4)。

将测试文件的样本发布到失败的地方,如果不是这样的话。欢呼声。