Karma+Mocha+React 无法将 Symbol 值转换为字符串
Karma+Mocha+React Cannot convert a Symbol value to a string
我将 webpack+babel 用于 React+Redux 应用程序,并将 Mocha+Karma 用于测试。 redux 测试用例得到正确执行。但是,当我尝试使用 react-addons-test-utils 进行 DOM 测试并使用 Karma 运行 进行测试时,它给出了这个错误
未捕获类型错误:无法将符号值转换为字符串
在 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;
您的测试可能发现比较 React 元素不匹配,但 Mocha 无法将内部符号转换为字符串 属性。
尝试在函数 canonicalize
的第 602 行附近编辑文件 node_modules/mocha/lib/utils.js
并替换:
default:
canonicalizedObj = value + '';
来自
default:
canonicalizedObj = String(value);
然后,运行 再次测试。这一次,摩卡应该可以告诉你哪里出了问题。
我只是 运行 使用完全相同的堆栈来解决这个问题。
如果您使用 TestUtils 的 shallowRenderer 并遵循 Redux 的文档示例,那么当您尝试记录输出时,您很可能会 运行 加入其中。将输出字符串化(JSON.stringify 等)以解决问题。
@Ricado Stuven 的回答:Mocha 已将该行更新为 value.toString()。至少是截至发帖日期的最新版本 (v2.3.4)。
Post 你的测试文件样本,如果不是这样,它就会失败。干杯。
确保您没有尝试 console.log
getRenderOutput
的结果。
这就是我的问题。
我将 webpack+babel 用于 React+Redux 应用程序,并将 Mocha+Karma 用于测试。 redux 测试用例得到正确执行。但是,当我尝试使用 react-addons-test-utils 进行 DOM 测试并使用 Karma 运行 进行测试时,它给出了这个错误
未捕获类型错误:无法将符号值转换为字符串 在 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;
您的测试可能发现比较 React 元素不匹配,但 Mocha 无法将内部符号转换为字符串 属性。
尝试在函数 canonicalize
的第 602 行附近编辑文件 node_modules/mocha/lib/utils.js
并替换:
default:
canonicalizedObj = value + '';
来自
default:
canonicalizedObj = String(value);
然后,运行 再次测试。这一次,摩卡应该可以告诉你哪里出了问题。
我只是 运行 使用完全相同的堆栈来解决这个问题。
如果您使用 TestUtils 的 shallowRenderer 并遵循 Redux 的文档示例,那么当您尝试记录输出时,您很可能会 运行 加入其中。将输出字符串化(JSON.stringify 等)以解决问题。
@Ricado Stuven 的回答:Mocha 已将该行更新为 value.toString()。至少是截至发帖日期的最新版本 (v2.3.4)。
Post 你的测试文件样本,如果不是这样,它就会失败。干杯。
确保您没有尝试 console.log
getRenderOutput
的结果。
这就是我的问题。