当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《Karma+Mocha+React无法将Symbol值转换为字符串》经验,为你挑选了2个好方法。

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

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

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

反应DOM测试的Karma错误

但是,当我不使用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;

Ricardo Stuv.. 5

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

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

default:
  canonicalizedObj = value + '';

通过

default:
  canonicalizedObj = String(value);

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



1> Ricardo Stuv..:

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

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

default:
  canonicalizedObj = value + '';

通过

default:
  canonicalizedObj = String(value);

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



2> Manu..:

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

推荐阅读
和谐啄木鸟
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有