0

我有一个围绕我的 JavaScript 应用程序代码构建的测试套件,其中包含大约 350 个测试。grunt test我的测试是通过从节点命令行运行来运行的。每次测试运行时,我的测试在停止之前都会运行不同数量的测试。它很少完成我所有的测试。以前有没有人发生过这种事情?关于可能导致它的任何想法?

当我运行测试时,我通常会得到一个看似随机的数字,从 270 到 350 测试完成。测试后,我没有收到任何错误或任何东西。它结束时说:

Finished in 1.332 secs / 0.823 secs

Summary:
√ 311 test completed

这是很少运行的测试之一的示例:

describe('util.js', function() {
    it('should create the global SeniorHomes object', function() {
        expect(sh).to.be.an('object');
    });
    describe('sh.cookies.create', function() {
        sh.cookies.create('name', 'value', 10);
        it('should create a cookie', function() {
            var cookies = document.cookie.split('; '),
                cookie;
            $.each(cookies, function(i, item) {
                if (item.split('=')[0] === 'name') {
                    cookie = {
                        name: item.split('=')[0],
                        value: item.split('=')[1]
                    };
                }
            });
            expect(cookie.name).to.equal('name');
            expect(cookie.value).to.equal('value');
        });
    });
    describe('sh.cookies.read', function() {
        sh.cookies.create('name2', 'value2', 10);
        var cookie = sh.cookies.read('name2');
        it('should read the value of a cookie', function() {
            expect(cookie).to.equal('value2');
        });
    });
    describe('sh.cookies.erase', function() {
        sh.cookies.create('name3', 'value3', 10);
        sh.cookies.erase('name3');
        it('should delete a cookie', function() {
            expect(sh.cookies.read('name3')).to.equal(null);
        });
    });
    describe('sh.format_time', function() {
        var date1 = new Date('01/31/2005 10:00'),
            date2 = new Date('01/31/2005 15:59'),
            output1 = sh.format_time(date1),
            output2 = sh.format_time(date2);
        it('should output a formatted time string', function() {
            expect(output1).to.equal('10:00 am');
            expect(output2).to.equal('3:59 pm');
        });
    });
    describe('sh.to_title_case', function() {
        var output = sh.to_title_case('test_string');
        it('should output a title formatted string', function() {
            expect(output).to.equal('Test String');
        });
    });
    describe('sh.to_snake_case', function() {
        var output = sh.to_snake_case('Test String');
        it('should ouput a snake cased string', function() {
            expect(output).to.equal('test_string');
        });
    });
    describe('copy_object', function() {
        var original = {
            test1: {
                test2: 'test3'
            }
        };
        var copy = sh.copy_object(original);
        it('should create a copy of the original object', function() {
            expect(copy.test1.test2).to.equal(original.test1.test2);
        });
    });

});

这是它正在测试的 JavaScript:

/**
 * Name: Util.js
 * Description: Contains javascript used on both frontend and backend
 * Note: jQuery Dependent
 */

/**
 * Global namespace for site functionality
 * @type {Object}
 */
if (!sh) {
    var Site = function() {},
        sh = new Site();
}

$(function() {
    //apply calendar datepicker (jquery ui) to input
    $('.datePicker').datepicker();
    $('.datestampPicker').datepicker({
        dateFormat: "yy-mm-dd 00:00:00"
    });

    //Alert confirm message for .confirm buttons
    $('.confirm').click(function(e) {
        var confirmation = confirm('Are you sure you want to do this?');
        if (!confirmation)
            e.preventDefault();
    });});

sh.environment = (function() {
    var subdomain = window.location.host.split('.')[0];
    if(subdomain === 'www') {
        return 'production';
    } else {
        return 'development';
    }
})();

sh.cookies = {};
/**
 * Create a cookie
 * @param  {string} name  name of cookie
 * @param  {string} value value of cookie
 * @param  {int} days  expiration length
 * @return {null}       no return value
 */
sh.cookies.create = function(name, value, days){
    var expires = '';
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }

    document.cookie = name + "=" + value + expires + "; path=/";
};

/**
 * Read the value of a cookie
 * @param  {string} name name of cookie
 * @return {string|null}      value of cookie
 */
sh.cookies.read = function(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
};

/**
 * Erase a cookie
 * @param  {string} name name of cookie
 * @return {null}      no return value
 */
sh.cookies.erase = function(name){
    sh.cookies.create(name, "", -1);
};

/**
 * Get a formatted time from a JavaScript date time object
 * @param  {object} time date time
 * @return {string}      formatted date time (hh:mm pm/am)
 */
sh.format_time = function(time){
    var minuteFormat = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
    var timeString =
        time.getHours() - 12 > 0 ?
        time.getHours() - 12 + ':' + minuteFormat + ' pm' : time.getHours() + ':' + minuteFormat + ' am';

    return timeString;
};

/**
 * Formats a string to capital letters with spaces
 * @param  {string} str string to be title-cased (test_string)
 * @return {string}     title case version of str (Test String)
 */
sh.to_title_case = function(str){
    return str.replace(/_/g, ' ').replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};

/**
 * Formats a string to lower case with underscores instead of spaces
 * @param  {string} str string to be snake cased (Test String)
 * @return {string}     snake case version of str (test_string)
 */
sh.to_snake_case = function(str){
    return str.toLowerCase().replace(/ /g,'_');
};

/**
 * Produces a copy of a javascript object
 * @param  {object} object object to be copied
 * @return {object}        copy
 */
sh.copy_object = function(object) {
    return JSON.parse(JSON.stringify(object));
};

//returns the version of ie as integer
sh.get_ie = (function() {
    var myNav = navigator.userAgent.toLowerCase();
    return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}());

/**
 * Various data structures
 * 
 * @type {Object}
 */
sh.structures = {};

/**
 * A general exception
 */
sh.structures.Exception = Error;
4

1 回答 1

0

当我作为单次运行运行测试时,它会说由于警告而中止。有一些关于重新加载整个页面的测试的警告。所以,我删除了两个函数的测试:

  • 请求桌面版本:添加 cookie 并重新加载页面以仅获取桌面样式。

  • 请求移动版本:删除 cookie 并重新加载以获取移动样式

删除这些后,我得到一致数量的测试完成,并显示“完成,没有错误”消息。

于 2015-04-07T15:58:51.330 回答