TypeError:JSON.stringify无法序列化循环结构(在karma,PhantomJS,angular-cli中)

时间:2018-04-04 00:01:08

标签: angular jasmine phantomjs angular-cli karma-jasmine

我正在尝试使用角度cli和PhantomJS(ng test --code-coverage --single-run)运行业力测试但是我得到的错误似乎导致我的33个单元测试中的24个失败。当我使用Chrome作为配置的浏览器运行测试时,所有测试都会通过。

PhantomJS 2.1.1 (Windows 7.0.0) AppService should be created FAILED
    TypeError: JSON.stringify cannot serialize cyclic structures. in http://localhost:9876/_karma_webpack_/polyfills.bundle.js (line 5424)
    stringify@[native code]
    stringify@http://localhost:9876/_karma_webpack_/polyfills.bundle.js:5424:28
    displayModal@http://localhost:9876/_karma_webpack_/scripts.bundle.js:4473:57
    createModalContent@http://localhost:9876/_karma_webpack_/scripts.bundle.js:4453:41
    BMS@http://localhost:9876/_karma_webpack_/scripts.bundle.js:7282:43
    BMSService@http://localhost:9876/_karma_webpack_/main.bundle.js:2095:25930
    _createClass@http://localhost:9876/_karma_webpack_/vendor.bundle.js:84475:34
    _createProviderInstance$1@http://localhost:9876/_karma_webpack_/vendor.bundle.js:84447:38
    resolveNgModuleDep@http://localhost:9876/_karma_webpack_/vendor.bundle.js:84432:42
    _createClass@http://localhost:9876/_karma_webpack_
    ...(there was more in the stack but I cropped it)

在人们对Karma和PhantomJS的其他问题中,有人建议取消注释polyfills.ts文件。我认为这可能是相关的,因为错误似乎来自polyfills捆绑文件,所以我尝试运行测试并将polyfills注释掉,并且还没有注释掉它们并且它没有任何区别。

以下是我的karma.conf.js文件:https://codepen.io/anon/pen/NYBypv

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      // require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage'),
      require('karma-phantomjs-launcher'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma')
    ],
    preprocessors: {
      'src/**/*.js': ['coverage']
    },
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageReporter: { type : 'html', dir : 'coverage/', 
      reporters: [ 
        { type: 'html', subdir: 'report-html' },
        { type: 'cobertura', subdir: '.', file: 'TESTS-xunit.xml' }
      ]  
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly', 'coverage' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev'
    },
    reporters: ['progress', 'kjhtml', 'coverage'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    browsers: ['PhantomJS'],
  });
};

1 个答案:

答案 0 :(得分:0)

我认为这是JSON.stringify方法(proof)的问题,而不是Karma / Phantom。 临时解决方案是在运行JSON.stringify

之前删除循环依赖项