Javascript-使用karma+;幻影
我需要js专家的帮助。 我有一个js文件:Javascript-使用karma+;幻影,javascript,karma-mocha,Javascript,Karma Mocha,我需要js专家的帮助。 我有一个js文件: 'use strict'; var fs = require('fs'); var Conf = { read: read, }; function read() { try { var conf = JSON.parse(fs.readFileSync(__dirname + '/../resources/conf.json', 'utf-8')); return conf; }
'use strict';
var fs = require('fs');
var Conf = {
read: read,
};
function read() {
try {
var conf = JSON.parse(fs.readFileSync(__dirname + '/../resources/conf.json', 'utf-8'));
return conf;
}
catch (err) {
console.log('There has been an error parsing your JSON.');
console.log(err);
}
}
module.exports = Conf;
我想测试一下。我使用因果报应+摩卡。我创建了测试文件
'use strict';
var Conf = require('../../lib/Conf');
describe('My test', function() {
describe('json config', function() {
it('should display initially', function () {
console.log(Conf.read());
});
});
});
但当我使用PhantomJS 2.1.1运行测试时,我总是得到TypeError错误。如果我将PhantomJS更改为Chrome,我会得到未定义的错误
测试框架是否支持require('fs')代码>?如果是,请帮助我运行它
我的因果报应是
'use strict';
// Karma configuration
// Generated on Wed Mar 15 2017 00:21:08 GMT+0300 (RTZ 2 (зима))
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../../',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['browserify', 'mocha', 'chai', 'sinon-chai'],
// list of files / patterns to load in the browser
files: [
'test/spec/**/*Spec.js'
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {'test/spec/**/*Spec.js': [ 'browserify' ]},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['spec'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
browserify: {
debug: true,
transform: [ [ 'stringify', { global: true, extensions: [ '.bpmn', '.xml', '.css' ] } ] ]
}
})
}
my package.json dev deps
"devDependencies": {
"brfs": "^1.2.0",
"browserify": "^14.1.0",
"chai": "^3.5.0",
"grunt": "~1.0.1",
"grunt-browserify": "^5.0.0",
"grunt-contrib-connect": "~1.0.2",
"grunt-contrib-copy": "~1.0.0",
"grunt-contrib-jshint": "~1.0.0",
"grunt-contrib-less": "^1.0.1",
"grunt-contrib-watch": "~1.0.0",
"grunt-karma": "^2.0.0",
"karma": "^1.5.0",
"karma-browserify": "^5.1.1",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.1.0",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-sinon-chai": "^1.2.4",
"karma-spec-reporter": "0.0.30",
"load-grunt-tasks": "^3.5.2",
"mocha": "^3.2.0",
"sinon": "^1.17.7",
"sinon-chai": "^2.8.0",
"stringify": "^5.1.0"
},
我通过更改变换值修复了此问题:
browserify: {
debug: true,
transform: [
[
'stringify',
{
global: true,
extensions: [ '.bpmn', '.xml', '.css' ]
}
]
]
}
到
browserify: {
debug: true,
transform: [ 'brfs' ]
}