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

Karma+Mocha+React Cannot convert a Symbol value to a string

本文关键字:转换 字符串 Symbol Karma+Mocha+React      更新时间:2023-09-26

我正在使用webpack+babel作为React+Redux应用程序,并使用Mocha+Karma进行测试。redux测试用例正在正确执行。然而,当我尝试使用react addons测试实用程序进行DOM测试并使用Karma运行它时,会出现以下错误

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

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

React DOM测试的因果报应误差

然而,当我不使用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;

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

尝试编辑函数canonicalize中602行附近的文件node_modules/mocha/lib/utils.js,并替换:

default:
  canonicalizedObj = value + '';

通过

default:
  canonicalizedObj = String(value);

然后,再次运行测试。这一次,Mocha应该能够告诉你哪里出了问题。

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

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

如果你使用TestUtils的shallowRenderer并遵循Redux的文档示例,那么当你试图记录输出时,你很可能会遇到这种情况。对输出进行字符串化(JSON.Stringify等)以解决问题。

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

把你的测试文件的样本贴在失败的地方,如果不是的话。干杯。