2017-03-15 40 views
0

我需要從js專家的幫助。 我有一個js文件:Javascript - 測試讀取文件與業力+ PhantomJS

'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運行測試,如果我改變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' ] } ] ] 
     } 
    }) 
} 

我的package.json開發的DEP

"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" 
    }, 

回答

0

我解決了這個問題的變化變換值:

browserify: { debug: true, transform: [ [ 'stringify', { global: true, extensions: [ '.bpmn', '.xml', '.css' ] } ] ] }

browserify: { debug: true, transform: [ 'brfs' ] }