首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用gulp jasmine指定依赖项。

使用gulp jasmine指定依赖项。
EN

Stack Overflow用户
提问于 2015-01-29 01:47:00
回答 1查看 1.2K关注 0票数 9

我有下面的gulpfile.js

代码语言:javascript
复制
var gulp = require('gulp'),
    jasmine = require('gulp-jasmine');

gulp.task('default', function () {
    return gulp
        .src('spec/test.js')
        .pipe(jasmine());
});

然而,spec/test.js内部的代码使用了一个全局angular变量,并在运行GUPTaskdefault时抛出了一个关于它未定义的错误。假设angular是在spec/lib.js文件中定义的,在全局范围内。

我如何告诉jasmine(),在运行describe()test.js内部之前,它需要先加载到全局作用域中的依赖项?换句话说,在jasmine()运行测试之前,我如何告诉它先加载spec/lib.js

EN

回答 1

Stack Overflow用户

发布于 2016-03-22 22:06:19

你试过KARMA吗?

业力联系

如果你使用业力,茉莉花,和吞咽,我这样做,你可以像这样配置你的测试

gulpfile.js

代码语言:javascript
复制
var gulp = require('gulp');
var karma = require('karma').server; // Note the server property here.

gulp.task('karma', function (done) {
  karma.start({
    configFile: __dirname + '/karma.conf.js'
  }, done);
});

您需要为角模板导入ng-html2js。

代码语言:javascript
复制
npm install ng-html2js

karma.config.js中(必须创建)

代码语言:javascript
复制
module.exports = function(config) {

  config.set({

  // base path that will be used to resolve all patterns (eg. files, exclude)
  basePath: '',

  frameworks: ['jasmine'],

  files: setFilesUpForTesting(),

  // list of files to exclude during testing. CHANGE THIS TO YOURS!!!
  exclude: [
    'public/js/boot.js',
    'public/js/socket.io.js'
    // 'yourRootPath/folder/stuff.js
  ],

  // NOTE THE NG-HTML2JS preprocessor you will need via NPM.    
  preprocessors: {
    // CHANGE TO YOUR PATH for all HTML PARTIALS.
    'public/directives/**/partials/*.html': 'ng-html2js',
    'public/directives/**/*.js': ['coverage']
  },

  ngHtml2JsPreprocessor: {
    stripPrefix:'public',
    moduleName: 'templates'
  },

  reporters: ['progress', 'coverage'],

  coverageReporter: {
    type : 'html',
    dir : 'coverage/'
  },

  port: 9876,

  colors: true,

  // 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: true,

  // start these browsers
  // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
  browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

// ------------------------------------------------------------------ >
// Below code is for running either single or all test files.
// To run a single test, just pass in the test file name like so...

// If the test file name was rs-test-me-now_spec.js
// To spool up Karma you would enter

// gulp karma --single-test=rs-test-me-now  <note the proceeding double hyphen>
// To just run all tests
// gulp karma

// CHANGE THE FILE PATHS BELOW!!!!!

var fixedPath = [
  'public/js/jquery.js',
  'public/js/jquery-ui.min.js',
  'public/js/foundation.min.js',
  'public/js/angular.min.js',
  'public/js/angular-mocks.js',

];

var baseTestPath = 'public/test/'; // CHANGE THIS TO YOUR ROOT PATH!!!

function setFilesUpForTesting(){
  fixedPath.push( testPath());
  return fixedPath;
}

function testPath(){
  return singleTestPath() || fullTestPath();
}

function fullTestPath(){
  return  'public/test/**/*_spec.js'; // CHANGE THIS TO YOUR SPEC FOLDER!!!
  // like rootFolder/somethinghere/spec/**/*_spec.js
  // note the underscore that proceeds the spec.js. Get rid of if you file name does not have it.
}

function singleTestPath(){
  var passedArgument = process.argv.filter(function(item){ if(/--single-test=/.test(item)){return item; }});
  if( isEmpty( passedArgument )){ return false; }
  return baseTestPath + '**/*' + fileName( passedArgument ) + '_spec.js';
}

function fileName( argument ){
  if( !argument ){ return; }
  return argument[0].replace('--single-test=', '');
}

function isEmpty( array ){
  return array.length === 0;
}

------------------- END KARMA.CONFIG.js file ----------------------

现在运行测试

代码语言:javascript
复制
// To run all tests
gulp karma

// To run one single file test
gulp karma --single-test=rs-test-me-now

注意:,如果你不能让它马上开始工作,请告诉我,我们会告诉它的。也许我漏掉了什么。我的测试文件名为something_test.js,而不是something_spec.js。我更改了代码以创建_spec.js。此外,您还必须确保包含了运行角所需的所有文件。我在我的karma.config.js中提供了一些例子。我也是有角的。

预处理器(ng-html2js)编译指令的角模板。您必须将它安装在您的package.json中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28205696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档