Karma 的测试看起来很混乱

The test of Karma appear obfuscated

我不知道为什么,当我尝试在任何浏览器中使用 JavaScript 控制台调试业力测试时,代码被混淆了。

示例:

这是我的测试之一:

it("Test KO: authorization service is not configured",function(){
    try {
        debugger;
        var authService = new AuthResource.authService();
        var result = null;
        var error = false;
        var defer = $q.defer();
        var promise = defer.promise;

        promise.then(function(data){
            result = data;
        },function(data){
            error=true;
        });

        expect(authService.isResourceAllowed().then).toBeDefined();
        authService.isResourceAllowed().then(function(data){
            defer.reject();
        },function(data){
            defer.resolve(data);
        });

        $rootScope.$digest();
        expect(error).toBe(false);
        expect(result.message).toBe(thinErrors.AuthResource.SERVICE_NOT_READY);

    } catch (err){
        expect(false).toBe(true);
    }
});

这是浏览器显示给我的代码:

__cov_WMdMXGMJh6AhQw9bCxIddQ = __cov_WMdMXGMJh6AhQw9bCxIddQ['./thin/test/unit   
/authResourceTest.js'];
__cov_WMdMXGMJh6AhQw9bCxIddQ.s['1']++;describe('Test for the authorization  
service',function()
{__cov_WMdMXGMJh6AhQw9bCxIddQ.f['1']++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['2']++;var 
$httpBackend,AuthResource,$q,$rootScope,thinErrors;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['3']++;be    foreEach(module('thinModule'));__cov_WMdMXGMJh6AhQw9bCxIddQ.s['4']++;beforeEach(inject(function(_$httpBackend_,_AuthResource_,_$q_,_$rootScope_,_thinErrors_){__cov_WMdMXGMJh6AhQw9bCxIddQ.f['2']++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['5']++;$httpBackend=_$httpBackend_;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['6']++;AuthResource=_AuthResource_;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['7']++;$q=_$q_;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['8']++;$rootScope=_$rootScope_;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['9']++;thinErrors=_thinErrors_;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['10']++;$httpBackend.when('POST','/test1').respond(function(method,url,data,headers){__cov_WMdMXGMJh6AhQw9bCxIddQ.f['3']++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['11']++;data=JSON.parse(data);__cov_WMdMXGMJh6AhQw9bCxIddQ.s['12']++;var error=false;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['13']++;var response=null;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['14']++;if(data.resource!='test1'){__cov_WMdMXGMJh6AhQw9bCxIddQ.b['1'][0]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['15']++;error=true;}else{__cov_WMdMXGMJh6AhQw9bCxIddQ.b['1'][1]++;}__cov_WMdMXGMJh6AhQw9bCxIddQ.s['16']++;if((__cov_WMdMXGMJh6AhQw9bCxIddQ.b['3'][0]++,!error)&&(__cov_WMdMXGMJh6AhQw9bCxIddQ.b['3'][1]++,data.typeResource!='test2')){__cov_WMdMXGMJh6AhQw9bCxIddQ.b['2'][0]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['17']++;error=true;}else{__cov_WMdMXGMJh6AhQw9bCxIddQ.b['2'][1]++;}__cov_WMdMXGMJh6AhQw9bCxIddQ.s['18']++;if((__cov_WMdMXGMJh6AhQw9bCxIddQ.b['5'][0]++,error)&&(__cov_WMdMXGMJh6AhQw9bCxIddQ.b['5'][1]++,data.attributes.result=='error')){__cov_WMdMXGMJh6AhQw9bCxIddQ.b['4'][0]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['19']++;response=[200,{'success':'Ok'},{}];}else{__cov_WMdMXGMJh6AhQw9bCxIddQ.b['4'][1]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['20']++;if((__cov_WMdMXGMJh6AhQw9bCxIddQ.b['7'][0]++,error)&&(__cov_WMdMXGMJh6AhQw9bCxIddQ.b['7'][1]++,data.attributes.result=='OK')){__cov_WMdMXGMJh6AhQw9bCxIddQ.b['6'][0]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['21']++;response=[404,{},{}];}else{__cov_WMdMXGMJh6AhQw9bCxIddQ.b['6'][1]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['22']++;if((__cov_WMdMXGMJh6AhQw9bCxIddQ.b['9'][0]++,!error)&&(__cov_WMdMXGMJh6AhQw9bCxIddQ.b['9'][1]++,data.attributes.result=='error')){__cov_WMdMXGMJh6AhQw9bCxIddQ.b['8'][0]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['23']++;response=[404,{},{}];}else{__cov_WMdMXGMJh6AhQw9bCxIddQ.b['8'][1]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['24']++;if((__cov_WMdMXGMJh6AhQw9bCxIddQ.b['11'][0]++,!error)&&(__cov_WMdMXGMJh6AhQw9bCxIddQ.b['11'][1]++,data.attributes.result=='OK')){__cov_WMdMXGMJh6AhQw9bCxIddQ.b['10'][0]++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['25']++;response=[200,{'success':'Ok'},{}];}else{__cov_WMdMXGMJh6AhQw9bCxIddQ.b['10'][1]++;}}}}__cov_WMdMXGMJh6AhQw9bCxIddQ.s['26']++;return response;});}));__cov_WMdMXGMJh6AhQw9bCxIddQ.s['27']++;it('Test KO: authorization service is not configured',function(){__cov_WMdMXGMJh6AhQw9bCxIddQ.f['4']++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['28']++;try{__cov_WMdMXGMJh6AhQw9bCxIddQ.s['29']++;debugger;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['30']++;var authService=new AuthResource.authService();__cov_WMdMXGMJh6AhQw9bCxIddQ.s['31']++;var result=null;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['32']++;var error=false;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['33']++;var defer=$q.defer();__cov_WMdMXGMJh6AhQw9bCxIddQ.s['34']++;var promise=defer.promise;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['35']++;promise.then(function(data){__cov_WMdMXGMJh6AhQw9bCxIddQ.f['5']++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['36']++;result=data;},function(data){__cov_WMdMXGMJh6AhQw9bCxIddQ.f['6']++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['37']++;error=true;});__cov_WMdMXGMJh6AhQw9bCxIddQ.s['38']++;expect(authService.isResourceAllowed().then).toBeDefined();__cov_WMdMXGMJh6AhQw9bCxIddQ.s['39']++;authService.isResourceAllowed().then(function(data){__cov_WMdMXGMJh6AhQw9bCxIddQ.f['7']++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['40']++;defer.reject();},function(data){__cov_WMdMXGMJh6AhQw9bCxIddQ.f['8']++;__cov_WMdMXGMJh6AhQw9bCxIddQ.s['41']++;defer.resolve(data);});__cov_WMdMXGMJh6AhQw9bCxIddQ.s['42']++;$rootScope.$digest();__cov_WMdMXGMJh6AhQw9bCxIddQ.s['43']++;expect(error).toBe(false);__cov_WMdMXGMJh6AhQw9bCxIddQ.s['44']++;expect(result.message).toBe(thinErrors.AuthResource.SERVICE_NOT_READY);}catch(err){__cov_WMdMXGMJh6AhQw9bCxIddQ.s['45']++;expect(false).toBe(true);}});});

你们知道为什么会这样吗?

这是我的业力配置文件:

module.exports = function(config){
    config.set({
        basePath : '../../',

        reporters: ['junit','coverage'],
        preprocessors : {
        'thin/**/*.js': 'coverage'
    },  
    files : [
      'thin/lib/angular/angular.min.js',
      'thin/lib/angular/angular-*.js',
      'thin/lib/angular-translate/angular-translate.min.js',
      'thin/lib/angular-translate/angular-translate-loader-partial.min.js',
      'thin/test/lib/angular/angular-mocks.js',
      'thin/lib/jquery/*.js',
      'thin/lib/bootstrap/*.js',
      'thin/lib/log4js/*.js',
      'thin/lib/stacktrace/*.js',
      'thin/test/resources/thinConfiguration.js',
      'thin/test/resources/configuration.js',
      'thin/test/resources/thinStart.js',
      'thin/thinServices/*.js',
      'thin/thinFilters/*.js',
      'thin/filters/*.js',
      'thin/thinDirectives/*.js',
      'thin/services/*.js',
      'thin/test/unit/*.js'
    ],

    browserNoActivityTimeout: 300000,

    autoWatch : false,

    frameworks: ['jasmine'],

    browsers : ['Chrome'],

    singleRun : true,

    logLevel: config.LOG_DEBUG,

    plugins : [
        'karma-coverage',
        'karma-junit-reporter',
        'karma-chrome-launcher',
        'karma-firefox-launcher',
        'karma-phantomjs-launcher',
        'karma-jasmine'
     ],

     junitReporter : {
      outputFile: 'thin/test/out/TEST-karma.xml',
      suite: 'unit'
    },
    coverageReporter : {
        type: 'lcovonly',
        dir: 'thin/test/coverage/'
    }

});};

感谢您的宝贵时间

更新:

这很有趣。如果我将我的测试文件夹复制到上层目录,代码就很清楚了。我的意思是,我的测试在文件夹 workspace/project/Webcontent/thin/test 中,现在它们在 workspace/project/WebContent/test 中。这就是解决方案。但是,我仍然不知道为什么,如果我的测试在一个文件夹级别下,代码会出现混淆

有什么想法吗?

覆盖率插件会预处理您的源代码并生成适合跟踪覆盖率的版本 - 这就是为什么所有这些行中到处都有计数器 (random-variable++) 的原因。

将文件移动到覆盖率插件未映射的另一个目录中,您的源代码将保持原样。

如果您想在测试时调试源代码,请禁用覆盖率插件并在调试完成后重新激活它 -(也许使用 karma-wrapper?)