jasmine-nodejs并使用done()完成异步测试

时间:2016-05-18 23:31:38

标签: javascript node.js jasmine jasmine-node

我似乎在设置NodeJS应用程序的单元测试时遇到问题,然后使用Jasmine运行它们。问题在于使用Jasmine的done功能。当我在测试中包含done函数时,我收到一条错误消息:Warning: Cannot read property 'clearTimeout' of undefined Use --force to continue.我非常确定我没有做正确的事情,但那件事情仍在继续逃避我。

我的Gruntfile.js:

module.exports = function(grunt){
    // Load grunt tasks automatically
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({
        // Make sure code styles are up to par and there are no obvious mistakes
        jshint: {
            options: {
                jshintrc: '.jshintrc'
            },
            all: [
                'Gruntfile.js',
                'app/scripts/Directives/**/*.js',
                'app/scripts/Services/**/*.js',
                'app/scripts/app.js'
            ]
        },

        // Empties folders to start fresh
        clean: {
            options: { force: true },
            dist: {
                files: [
                    {
                        dot: true,
                        src: [
                            '/dist/*'
                        ]
                    }
                ]
            },
            coverage: {
                files: [
                    {
                        dot: true,
                        src: [
                            '.coverage/'
                        ]
                    }
                ]
            }
        },

        // Copies remaining files to places other tasks can use
        copy: {
            dist: {
                files: [
                    {
                        expand: true,
                        dot: true,
                        cwd: '<%= yeoman.app %>',
                        dest: '<%= yeoman.dist %>',
                        src: [
                            '*.{ico,png,txt,json,js,shtml,template}',
                            'images/{,*/}*.{webp}',
                            'fonts/*',
                            'images/**/*.svg',
                            'api-documentation/**/**/*',
                            'api-explorer/**/*',
                            'styles/*.{ttf,woff,svg,css}',
                            'docs/**/*',
                            'template/**/*.{htm,html}',
                            'locale/*',
                            'config/*'
                        ],
                        rename: function(dest, src){

                            if(src.indexOf('customizations.html.template') !== -1 ||
                                src.indexOf('customizations.js.template') !== -1){
                                console.log( dest + '/' + src.replace('.template', ''));
                                return dest + '/' + src.replace('.template', '');
                            }

                            return dest + '/' + src;
                        }
                    },
                    {
                        expand: true,
                        cwd: '.tmp/images',
                        dest: '<%= yeoman.dist %>/images',
                        src: [
                            'generated/*'
                        ]
                    },
                    {
                        expand:true,
                        cwd: '.tmp/styles',
                        dest: '<%= yeoman.dist %>/styles',
                        src:['customer.css']
                    }
                ]
            }
        },

        // Test settings
        karma: {
            unit: {
                configFile: 'karma.conf.js',
                singleRun: true,
                reporters: ['dots']
            },
            coverage: {
                configFile: 'karma.coverage.conf.js',
                singleRun: true,
                reporters: ['spec', 'coverage'],
                coverageReporter: {
                    type: 'html',
                    dir: '.coverage/'
                }
            },
            node:{
                configFile:'karma.node.conf.js',
                singleRun:true,
                reporters:['spec', 'coverage'],
                coverageReporter: {
                    type: 'html',
                    dir: '.nodeCoverage/'
                }
            }
        },

        open : {
            coverage : {
                path: 'http://127.0.0.1:8888/src'
            },
            covReport: {
                path: 'http://localhost:63342/ClassroomLibrary/' + grunt.file.expand('.coverage/Phantom*/index.html')
            }
        },

        jasmine_nodejs:{
            options:{
                specNameSuffix:'node.spec.js',
                reporters:{
                    console:{
                        colors:true,
                        cleanStack:1,
                        verbosity:4,
                        listStyle:'indent',
                        activity:false
                    }
                }
            },
            all:{
                options:{
                    useHelpers:true
                },
                specs:[
                    'tests/Node/**'
                ]
            }
        },
        nodemon:{
            tests:{
                script:'app/index.js'
            }
        },
        concurrent:{
            nodeTests:['nodemon', 'jasmine_nodejs']
        },
        run_node:{
            start:{
                files:{src:['app/index.js']}
            }
        },
        stop_node:{
            stop:{
                files:{src:['app/index.js']}
            }
        }
    });

    grunt.loadNpmTasks('grunt-jasmine-nodejs');
    grunt.loadNpmTasks('grunt-nodemon');
    grunt.loadNpmTasks('grunt-concurrent');
    grunt.loadNpmTasks('grunt-run-node');

    grunt.registerTask('test', [
        'karma:unit'
    ]);

    grunt.registerTask('coverage', [
        'clean:coverage',
        'karma:coverage',
        'open:covReport'
    ]);

    grunt.registerTask('nodeTests', ['concurrent:nodeTests']);
    grunt.registerTask('testingNode', ['run_node', 'jasmine_nodejs', 'stop_node']);
    //grunt.registerTask('karmaNode', ['run_node', 'karma:node', 'stop_node']);
};

有问题的测试套件:

var request = require('http');
describe('Node server', function(){
    'use strict';

    //http://www.randomjavascript.com/2012/12/using-jasmine-node-to-test-your-node.html
    var serverUrl = 'http://127.0.0.1:2425';

    it('should respond to /', function(done){
        request.get(serverUrl, function(response){
            expect(response.statusCode).toBe(200);
            done();
        });
    });
});

显然,我已经尝试了几种不同的方法来实现这一目标。通过Karma运行它们比它的价值更麻烦(我把那个配置放在那里以显示我尝试过的东西。)虽然我对nodeMon和jasmine-nodejs的输出感到满意但我还是不能得到整个从前到后运行的东西,因为没有done函数的东西就像你期望的那样挂起。它应该只是在那里,但对我来说它不是。我的package.json包含:

{
  "name": "myProject",
  "version": "0.0.1",
  "dependencies":{},
  "devDependencies": {
    "karma": "~0.13",
    "karma-coverage": "~0.5",
    "karma-phantomjs-launcher": "~1.0",
    "karma-ng-html2js-preprocessor": "~0.1",
    "phantomjs-prebuilt": "~2.1",
    "grunt": "~1.0",
    "grunt-contrib-clean": "~0.7",
    "grunt-contrib-uglify": "~0.11",
    "grunt-contrib-concat": "~0.5",
    "grunt-contrib-copy": "~1.0",
    "grunt-concurrent":"~2.3",
    "grunt-jasmine-nodejs":"~1.5",
    "grunt-jasmine-node-coverage":"0.5.0",
    "grunt-open":"~0.2",
    "grunt-nodemon":"~0.4",
    "grunt-run-node":"~0.1",
    "grunt-karma": "~0.12",
    "jasmine-core": "~2.4",
    "jasmine-node":"~1.14",
    "karma-jasmine": "~0.3",
    "karma-jasmine-matchers": "~2.0",
    "karma-spec-reporter":"~0.0",
    "karma-requirejs":"~1.0",
    "load-grunt-tasks": "~3.4",
    "uglify-js": "~2.6",
    "grunt-sass": "~1.1",
    "jshint":"~2.9"
  },
  "engines": {
    "node": ">=0.12.0"
  },
  "scripts": {
    "global": "npm i karma-cli grunt-cli -g"
  }
}

有人可以告诉我这个过程缺少了什么吗?

1 个答案:

答案 0 :(得分:1)

有一种想法 - 你能否确认你实际使用的是你想要使用的匹配的Jasmine版本?我认为已经完成了&#39;回调是在2.0中添加的,所以我可以看到旧版本发生此错误...

编辑:您正在使用&#34; jasmine-node&#34;包版本为1.3.1版本,因此该功能无法使用。