如何在节点中使用spec - grunt + karma + jasmine [英] How use require inside a spec - grunt + karma + jasmine in node

查看:177
本文介绍了如何在节点中使用spec - grunt + karma + jasmine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模糊问题


  • 如何在spec中使用require和grunt?
  • >


上下文

现在没有测试的节点项目,所以我读了一些并意识到使用业力和茉莉花。



我读了一些tutos(包括这些):






X遇到声明异常
ReferenceError:找不到变量e:require in file:///(...)-spec.js(第2行)(1)

行是这样的:

  var myHelper = require(...); 

但是,如果我通过终端node-jasmine test使用,它就像一个魅力...

我的项目结构:
$ b



  • 控制器/

  • 助手/

  • 模型/

  • node_modules /

  • 资源/ b / b>
  • 测试/

  • 测试/规格/
  • app.js

  • Gruntfile.js

  • package.json

在我的规范中(在test / spec /中)我使用require('../../ helpers / helper.js'),茉莉花,但没有与咕噜声。



节点 - 茉莉花测试:

lockquote>

。 ....



完成0.015秒5次测试,5次断言,0次失败,0次跳过

grunt:


运行jasmine:pivotal(jasmine)任务通过
测试jasmine规格PhantomJS




:无法找到变量:require在
app.js:1服务助手测试X遇到声明异常
ReferenceError:无法找到变量:require in file:///(...)/test/spec/serviceHelper-spec.js
(line 2)(1)


0.005s中的1个规格。


< blockquote>

1次失败警告:任务jasmine:pivotal失败。使用--force继续。


因警告而中止。


我将所有软件包安装到node_modules中(package.json中的依赖项中没有任何内容),我的Gruntfile.js是:

 'use strict'; 

module.exports =函数(grunt){
var $ srcFiles ='app.js';
var $ testFiles ='test / spec / * - spec.js';
var $ outputDir ='test / target'
var $ junitResults = $ outputDir +'/junit-test-results.xml';
var $ jasmineSpecRunner = $ outputDir +'/_SpecRunner.html';
var $ coverageOutputDir = $ outputDir +'/ coverage';


grunt.initConfig({
pkg:grunt.file.readJSON('package.json'),

// Jasmine test
jasmine:{
pivotal:{
src:$ srcFiles,
options:{
specs:$ testFiles,
outfile:$ jasmineSpecRunner,
keepRunner:'true'//保留SpecRunner / outfile文件
}
}
},

//使用Karma的覆盖范围
karma:{
连续:{
singleRun:'true',
浏览器:['PhantomJS']
},

选项:{
plugins:[
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-junit-reporter',
'karma-coverage'
],
框架:['jasmine'],
文件:[$ srcFiles,$ testFiles],
记者:['junit','coverage'],
junitReporter:{
outputFile :$ junitResults
},
预处理器:{
//源文件必须是文字字符串
'助手/ *。js':['coverage']
},
coverageReporter:{
类型:'lcov',
dir:$ coverageOutputDir
}
}
},

//将Karma覆盖率导出到SonarQube
karma_sonar:{
your_target:{
// SonarQube仪表板的属性
项目:{
key:'net.ahexample: ahexample-jasmine-karma-sonar',
名称:'Jasmine with Kar ma和SonarQube示例',
版本:'0.0.1'
}

//源属性在运行时设置(请参见下文)
}
},

clean:[$ outputDir]
});


/ *
*设置karma_sonar的sources属性的任务。
*这是必需的,因为karma(coverage)将其结果存储在名称使用浏览器的用户代理info
*(name / version和平台名称)的
*目录中。
*后者可能与操作系统名称不同,因此它需要
* OS给平台翻译器。
*例如,Apple Mac OS X的操作系统名称是Darwin。
* /
grunt.registerTask('set-karma-sonar-sources-property',function(){
var $ done = this.async();
var $ $ phantomjs = require('karma-phantomjs-launcher / node_modules / phantomjs');
var $ spawn = require('child_process')。spawn;
var $ phantomUserAgent = $ spawn($ phantomjs.path,
// phantomjs脚本来打印用户代理字符串
['lib / phantomjs-useragent.js']
);
$ b $ * b $ b *构造覆盖来自PhantomJS'
*用户代理字符串的LCOV文件路径,然后用它设置karma_sonar的
* sources属性。
* /
$ phantomUserAgent.stdout.on('data' ,函数(msg){
var $ useragent = require('karma / node_modules / useragent');
var $ agent = $ useragent.parse(msg);
// dirName是'PhantomJS 1.9.7(Mac OS X)'
var $ dirN ame = $ agent.toAgent()+'('+ $ agent.os +')';
var $ coverageResults = $ coverageOutputDir +'/'+ $ dirName +'/lcov.info';
var $ sonarSources = makeSonarSourceDirs($ srcFiles,$ coverageResults);
var $ karmaSonarConfig ='karma_sonar';
var $ ksConfig = grunt.config($ karmaSonarConfig);

grunt.log.writeln('coverage LCOV file:'+ $ coverageResults);
$ ksConfig ['your_target'] ['sources'] = $ sonarSources;
grunt.config($ karmaSonarConfig,$ ksConfig);

});

$ phantomUserAgent.on('close',function(exitCode){
$ done();
});


/ *
*为源文件模式的每个目录创建声纳源对象。
* /
函数makeSonarSourceDirs($ filesPattern,$ coverageResults){
var $ path = require('path');
var $ dirs = [];

grunt.file.expand(
{
filter:function($ filePath){
$ dirs.push({
path:$ path。 dirname($ filePath),
前缀:'。',// lcov.info中的路径前缀
coverageReport:$ coverageResults,
testReport:$ junitResults
});
}
},
$ filesPattern
);

返回$ dirs;
}
});


grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-karma-sonar');


grunt.registerTask('test',['jasmine','karma:continuous']);
grunt.registerTask('sonar-only',['set-karma-sonar-sources-property','karma_sonar']);
grunt.registerTask('sonar',['test','sonar-only']);
grunt.registerTask('default','test');

感谢您的关注。



这取决于:


  1. 如果您有一些应用程序代码需要根据浏览器进行测试(例如 Angular Backbone 等) - 使用 Karma 并且不要使用 require 。然后确保你的 helpers.js 文件在测试之前加载。

    c> // @file Gruntfile.js
    // https://github.com/karma-runner/grunt-karma
    grunt.initConfig({
    karma:{
    客户:{
    options:{'b $ b files:['client / *。js','helpers / *。js','test / *。js']
    }
    }
    }
    });
    $ b $ // @file helpers.js
    (function(){
    window.helpers = {
    foo:function(){
    return'bar ';
    }
    };
    })();

    // @file spec.js
    (函数(帮助者){

    它('做的东西',function(){
    expect (helpers.foo())。toBe('bar');
    });

    })(window.helpers);


  2. 如果您不需要针对浏览器运行测试测试 NodeJS 代码),您可以通过移除 Karma 并严格使用 Jasmine

      // @file Gruntfile.js 
    // https:// github。 com / gruntjs / grunt-contrib-jasmine
    grunt.initConfig({
    jasmine:{
    server:{
    src:'server / *。js',
    选项:{
    specs:'test / *。js',
    helpers:'helpers / *。js'
    }
    }
    }
    } );
    $ b $ // @file helpers.js
    (function(){
    module.exports = {
    foo:function(){
    return'bar ';
    }
    };
    })();

    // @file spec.js
    (function(){
    var helpers = require('helpers'); // require is available

    (it's''the thing',function(){
    expect(helpers.foo())。toBe('bar');
    });

    }) ();




为什么



require 不存在,因为您使用 Karma 来运行测试。 Karma 只需将文件加载到您选择的浏览器中,然后按您在 karma.conf.js 。它在内部使用您提供的测试框架(在这种情况下 Jasmine )对您提供的浏览器运行测试(在本例中为 PhantomJS )。



与所有JavaScript一样,变量上下文由其包含的闭包定义。


  • Jasmine 二进制内部使用 NodeJS ,其中模拟CommonJS require ,使得需要函数在节点应用程序的上下文中可用。

  • Karma runner与写作< script src =[path]> 标记到浏览器,然后将相应的文件加载到 PhantomJs 中。因此,您的JavaScript上下文是全局的,并且您的文件只能访问全局上下文。在浏览器中,全局上下文由附加到窗口对象的所有内容定义,并且 window.require 不会固有地存在。

Vague version question:

  • How can I use require inside a spec with grunt?

Context:

I'm working on an existent node project that has no tests yet, so I read some and realized to use karma and jasmine.

I read some tutos (including these):

So I'm trying to run my specs with grunt and getting this error:

X encountered a declaration exception ReferenceError: Can't find variable: require in file:///(...)-spec.js (line 2) (1)

The line is something like:

var myHelper = require(...);

But if I use via terminal "node-jasmine test" it works like a charm...

My project structure:

  • controllers/
  • helpers/
  • models/
  • node_modules/
  • resources/
  • test/
  • test/spec/
  • views/
  • app.js
  • Gruntfile.js
  • package.json

In my spec (inside test/spec/) I use a require('../../helpers/helper.js') and that's ok for node-jasmine, but no with grunt.

node-jasmine test:

.....

Finished in 0.015 seconds 5 tests, 5 assertions, 0 failures, 0 skipped

grunt:

Running "jasmine:pivotal" (jasmine) task Testing jasmine specs via PhantomJS

ReferenceError: Can't find variable: require at app.js:1 Service Helper Tests X encountered a declaration exception ReferenceError: Can't find variable: require in file:///(...)/test/spec/serviceHelper-spec.js (line 2) (1)

1 spec in 0.005s.

1 failures Warning: Task "jasmine:pivotal" failed. Use --force to continue.

Aborted due to warnings.

I have all packages installed into node_modules (nothing in dependencies in package.json) and my Gruntfile.js is:

'use strict';

module.exports = function(grunt) {
    var $srcFiles = 'app.js';
    var $testFiles = 'test/spec/*-spec.js';
    var $outputDir = 'test/target'
    var $junitResults = $outputDir + '/junit-test-results.xml';
    var $jasmineSpecRunner = $outputDir + '/_SpecRunner.html';
    var $coverageOutputDir = $outputDir + '/coverage';


    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        // Jasmine test
        jasmine: {
            pivotal: {
                src: $srcFiles,
                options: {
                    specs: $testFiles,
                    outfile: $jasmineSpecRunner,
                    keepRunner: 'true'  // keep SpecRunner/outfile file
                }
            }
        },

        // coverage using Karma
        karma: {
            continuous: {
                singleRun: 'true',
                browsers: [ 'PhantomJS' ]
            },

            options: {
                plugins: [
                    'karma-jasmine',
                    'karma-phantomjs-launcher',
                    'karma-junit-reporter',
                    'karma-coverage'
                ],
                frameworks: [ 'jasmine' ],
                files: [ $srcFiles, $testFiles ],
                reporters: [ 'junit', 'coverage' ],
                junitReporter: {
                  outputFile: $junitResults
                },
                preprocessors: {
                    // source files must be a literal string
                    'helpers/*.js': [ 'coverage' ]
                },
                coverageReporter: {
                    type: 'lcov',
                    dir: $coverageOutputDir
                }
            }
        },

        // export Karma coverage to SonarQube
        karma_sonar: {
            your_target: {
                // properties for SonarQube dashboard
                project: {
                    key: 'net.ahexample:ahexample-jasmine-karma-sonar',
                    name: 'Jasmine with Karma and SonarQube Example',
                    version: '0.0.1'
                }

                // sources property is set at runtime (see below)
            }
        },

        clean: [ $outputDir ]
    });


    /*
     * Task to set karma_sonar's sources property.
     * This is needed because karma (coverage) stores its results in a
     * directory whose name uses the browser's user agent info
     * (name/version and the platform name).
     * The latter may well he different to the OS name and so its needs an
     * OS to platform translator.
     * For example, OS name for Apple Mac OS X is Darwin.
     */
    grunt.registerTask('set-karma-sonar-sources-property', function() {
        var $done = this.async();
        var $phantomjs = require('karma-phantomjs-launcher/node_modules/phantomjs');
        var $spawn = require('child_process').spawn;
        var $phantomUserAgent = $spawn($phantomjs.path,
            // phantomjs script to print user agent string
            [ 'lib/phantomjs-useragent.js' ]
        );

        /*
         * Construct coverage LCOV file path from PhantomJS'
         * user agent string, then use it to set karma_sonar's
         * sources property.
         */
        $phantomUserAgent.stdout.on('data', function(msg) {
            var $useragent = require('karma/node_modules/useragent');
            var $agent = $useragent.parse(msg);
            // An example of dirName is 'PhantomJS 1.9.7 (Mac OS X)'
            var $dirName = $agent.toAgent() + ' (' + $agent.os + ')';
            var $coverageResults = $coverageOutputDir + '/' + $dirName + '/lcov.info';
            var $sonarSources = makeSonarSourceDirs($srcFiles, $coverageResults);
            var $karmaSonarConfig = 'karma_sonar';
            var $ksConfig = grunt.config($karmaSonarConfig);

            grunt.log.writeln('coverage LCOV file: ' + $coverageResults);
            $ksConfig['your_target']['sources'] = $sonarSources;
            grunt.config($karmaSonarConfig, $ksConfig);

        });

        $phantomUserAgent.on('close', function(exitCode) {
            $done();
        });


        /*
         * Create sonar source object for each directory of source file pattern.
         */
        function makeSonarSourceDirs($filesPattern, $coverageResults) {
            var $path = require('path');
            var $dirs = [];

            grunt.file.expand(
                {
                    filter: function($filePath) {
                        $dirs.push({
                            path: $path.dirname($filePath),
                            prefix: '.',    // path prefix in lcov.info
                            coverageReport: $coverageResults,
                            testReport: $junitResults
                        });
                    }
                },
                $filesPattern
            );

            return $dirs;
        }
    });


    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-jasmine');
    grunt.loadNpmTasks('grunt-karma');
    grunt.loadNpmTasks('grunt-karma-sonar');


    grunt.registerTask('test', [ 'jasmine', 'karma:continuous' ]);
    grunt.registerTask('sonar-only', [ 'set-karma-sonar-sources-property', 'karma_sonar' ]);
    grunt.registerTask('sonar', [ 'test', 'sonar-only' ]);
    grunt.registerTask('default', 'test');
}

Thank you for your attention.

解决方案

How

It depends:

  1. If you have some application code that you need to test against a browser (e.g. Angular, Backbone, etc.) - Use Karma and don't use require. Then just make sure your helpers.js file loads before the tests themselves.

    // @file Gruntfile.js
    // https://github.com/karma-runner/grunt-karma
    grunt.initConfig({        
      karma: {
        client: {
          options: {
            files: ['client/*.js', 'helpers/*.js', 'test/*.js']
          }
        }
      }
    });
    
    // @file helpers.js
    (function () {
      window.helpers = {
        foo: function () {
          return 'bar';
        }
      };
    })();
    
    // @file spec.js
    (function (helpers) {
    
      it('does the thing', function () {
        expect(helpers.foo()).toBe('bar');
      });
    
    })(window.helpers);
    

  2. If you don't need to run your tests against a browser (i.e. you are strictly testing NodeJS code), you can simplify your setup by removing Karma and strictly using Jasmine:

    // @file Gruntfile.js
    // https://github.com/gruntjs/grunt-contrib-jasmine
    grunt.initConfig({
      jasmine: {
        server: {
          src: 'server/*.js',
          options: {
            specs: 'test/*.js',
            helpers: 'helpers/*.js'
          }
        } 
      }
    });
    
    // @file helpers.js
    (function () {
      module.exports = {
        foo: function () {
          return 'bar';
        }
      };
    })();
    
    // @file spec.js
    (function () {
      var helpers = require('helpers'); // require is available
    
      it('does the thing', function () {
        expect(helpers.foo()).toBe('bar');
      });
    
    })();
    

Why

require doesn't exist because you are using Karma to run your tests. Karma simply loads files in a browser of your choosing and executes them in the order you provide in your karma.conf.js. It internally uses the test framework you provide (in this case Jasmine) to run tests against the browser(s) you provide (in this case PhantomJS).

As is true with all JavaScript, the variable context is defined by the closure it is contained within.

  • The Jasmine binary internally uses NodeJS, which emulates CommonJS require, making the require function available to you within the context of your node application.

  • The Karma runner does what is the equivalent of writing <script src="[path]"> tags to the browser, which then each load the corresponding file into PhantomJs. As a result, your javascript context is global, and your files only have access to the global context. In a browser, the global context is defined by everything attached to the window object, and window.require does not inherently exist.

这篇关于如何在节点中使用spec - grunt + karma + jasmine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆