茉莉花跑三次测试

时间:2016-02-29 22:21:12

标签: javascript angular unit-testing jasmine karma-runner

我在我的开发盒上运行Karma / Jasmine / Angular 2.0测试。就在最近,我的开发盒上的Jasmine决定开始运行我的测试三次。是的,每次都是三次。

第一次运行时,一切都按预期通过。但是,在第二次和第三次通过时,所有相同的事情都失败了。它总是承认有7个测试,但是运行21个,10个失败(一级数学窗外)????

这也是fails on Travis与SauceLabs。 (注意:通过3次测试链接到旧版本,但运行9次,5次失败???)

我有一个截图,karma.conf.js文件,以及一个启动整个事情的套件。非常感谢任何帮助。

Culprit [TypeScript](删除此问题并在我的开发框中解决问题):

Full source

describe('From the Conductor Service', () => {
    let arr: Array<ComponentStatusModel> = null;
    let svc: ConductorService = null;

    beforeEach(() => {  
        arr = [/* Inits the array*/];
        svc = new ConductorService();
    });

    describe('when it is handed a container to hold objects which need loaded', () => {
        // More passing tests...

        /// vvvvv The culprit !!!!!
        describe('then when you need to access the container', () => {
            beforeEach(() => {
                svc.loadedContainer = arr;
            });

            it('it should always be available', () => {
                assertIsLocalDataInTheService(arr, svc.loadedContainer);
            });
        });
        /// ^^^^^ End of culprit !!!!!
    });

    // More passing tests...
});

测试失败:

Tests are ran three times

浏览器屏幕截图:

不确定这是否相关,但在所有错误发生之前,Jasmine调用堆栈较小(左,观察滚动条)。错误开始后,只要重复调用相同的函数,堆栈就会变大(右键,观察滚动条)。

Jasmine Call Stack

套件堆栈错误:

在我的测试中,Nanobar和Conductor spec文件是完全独立的。但是,您可以看到套件阵列包含来自Nanobar和Conductor规范的内容。不知怎的,Jasmine将这两个spec文件混合在一起(在一切开始失败之后),并导致我的describe()语句在发布到控制台时没有任何意义。

Jasmine Suite Stack

简化karma.conf.js:

Full source

module.exports = function (config) {
    config.set({
        autoWatch: false,
        basePath: '.',
        browsers: ['Chrome'],
        colors: true,
        frameworks: ['jasmine'],
        logLevel: config.LOG_INFO,
        port: 9876,
        reporters: ['coverage', 'progress'],
        singleRun: true,

        coverageReporter: {
            // Code coverage config
        },

        files: [
            // Loads everything I need to work
        ],

        plugins: [
            'karma-chrome-launcher',
            'karma-coverage',
            'karma-jasmine'
        ],

        preprocessors: {
            'app/**/*.js': ['coverage']
        },

        proxies: {
            // Adjust the paths
        }
    })
}

3 个答案:

答案 0 :(得分:1)

您可以尝试在每个测试文件的第一个断言中刷新浏览器吗? 试试这个:

browser.restart();

我遇到了同样的问题,这为我解决了这个问题。

答案 1 :(得分:0)

首先是这些测试是随机运行的。如果您认为可以通过任何测试用例传递一些数据,那将是不可能的。

您必须在每个数据之前声明数据,以便所有测试用例都可以获取数据。 所有测试用例均独立运行。

如果使用数组或对象,则必须在深度克隆后使用它,因为数组和对象可在引用上工作。如果您操作任何值,它也会更改原始数组。

在大多数情况下,如果测试失败,则可能会导致您在测试案例中传递的数据出错。

答案 2 :(得分:0)

我会尝试调试此问题并查明确切原因。 通常在我正在测试的函数中具有重定向代码或任何重新加载代码时发生。

  • 您可以尝试在describe和它的前缀(即fdescribe和fit)上添加一个f。