我使用Karma和Chrome(甚至是Chrome_Headless)在本地计算机(Windows 7)上成功运行了angular2单元测试。
但是当我尝试用PhantomJs运行这些测试时,他们在我的本地机器和我的Jenkins CI(CentOs)上都失败了。我有11次失败的测试,85次通过,我不明白为什么。
所有失败测试的错误消息如下所示:
[1A[2KPhantomJS 2.1.1 (Linux 0.0.0) Apptopbar Inputs shoud be properly initialize FAILED
Error: Error in ./Apptopbar class Apptopbar - inline template:44:66 caused by: Attempted to assign to readonly property. in /var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts (line 131597)
setElementProperty@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:129737:59 <- webpack:///~/@angular/core/src/debug/debug_renderer.js:171:25
setElementProperty@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:129741:42 <- webpack:///~/@angular/core/src/debug/debug_renderer.js:175:0
detectChangesInternal
detectChanges@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:131372:35 <- webpack:///~/@angular/core/src/linker/view.js:425:0
detectChanges@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:131567:48 <- webpack:///~/@angular/core/src/linker/view.js:620:0
internalDetectChanges@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:131357:31 <- webpack:///~/@angular/core/src/linker/view.js:410:0
detectChangesInternal
detectChanges@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:131372:35 <- webpack:///~/@angular/core/src/linker/view.js:425:0
detectChanges@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:131567:48 <- webpack:///~/@angular/core/src/linker/view.js:620:0
detectChanges@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:86432:33 <- webpack:///~/@angular/core/src/linker/view_ref.js:170:0
_tick@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:12449:49 <- webpack:///~/@angular/core/bundles/core-testing.umd.js:196:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:12463:58 <- webpack:///~/@angular/core/bundles/core-testing.umd.js:210:45
invoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2204:31 <- webpack:///~/zone.js/dist/zone.js:334:0
onInvoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:192941:45 <- webpack:///~/zone.js/dist/proxy.js:79:0
invoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2203:40 <- webpack:///~/zone.js/dist/zone.js:333:0
onInvoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:41162:43 <- webpack:///~/@angular/core/src/zone/ng_zone.js:273:0
invoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2203:40 <- webpack:///~/zone.js/dist/zone.js:333:0
run@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:1996:49 <- webpack:///~/zone.js/dist/zone.js:126:0
run@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:41031:65 <- webpack:///~/@angular/core/src/zone/ng_zone.js:142:42
detectChanges@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:12463:32 <- webpack:///~/@angular/core/bundles/core-testing.umd.js:210:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:195771:30 <- webpack:///src/test/components/top-bar.component.spec.ts:72:25
invoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2204:31 <- webpack:///~/zone.js/dist/zone.js:334:0
onInvoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:192941:45 <- webpack:///~/zone.js/dist/proxy.js:79:0
invoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2203:40 <- webpack:///~/zone.js/dist/zone.js:333:0
run@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:1996:49 <- webpack:///~/zone.js/dist/zone.js:126:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:192641:37 <- webpack:///~/zone.js/dist/jasmine-patch.js:104:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:1962:19 <- webpack:///~/moment/moment.js:1421:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:1905:11 <- null:0:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:12349:35 <- webpack:///~/@angular/core/bundles/core-testing.umd.js:96:0
invoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2204:31 <- webpack:///~/zone.js/dist/zone.js:334:0
onInvoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:192236:45 <- webpack:///~/zone.js/dist/async-test.js:49:0
onInvoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:192938:47 <- webpack:///~/zone.js/dist/proxy.js:76:0
invoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2203:40 <- webpack:///~/zone.js/dist/zone.js:333:0
run@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:1996:49 <- webpack:///~/zone.js/dist/zone.js:126:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:12344:32 <- webpack:///~/@angular/core/bundles/core-testing.umd.js:91:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/test.ts:192225:46 <- webpack:///~/zone.js/dist/async-test.js:38:0
invokeTask@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2237:36 <- webpack:///~/zone.js/dist/zone.js:367:0
runTask@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2036:57 <- webpack:///~/zone.js/dist/zone.js:166:0
invoke@/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:2290:45 <- webpack:///~/zone.js/dist/zone.js:420:0
/var/jenkins_home/workspace/AttestationCockpitGUI_Guerrit/src/polyfills.ts:3451:34 <- webpack:///~/zone.js/dist/zone.js:1581:0
PhantomJS 2.1.1 (Linux 0.0.0): Executed 96 of 99 (7 FAILED) (0 secs / 10.446 secs)
[1A[2KLOG: 'WARNING: sanitizing unsafe style value [object Object] (see http://g.co/ng/security#xss).'
PhantomJS 2.1.1 (Linux 0.0.0): Executed 96 of 99 (7 FAILED) (0 secs / 10.446 secs)
[1A[2KLOG: 'WARNING: sanitizing unsafe style value [object Object] (see http://g.co/ng/security#xss).'
PhantomJS 2.1.1 (Linux 0.0.0): Executed 96 of 99 (7 FAILED) (0 secs / 10.446 secs)
&#13;
我在Github上看到很多类似的帖子(角度模拟的问题)但是解决方法对我没有用
编辑:我的Jenkins CI正在docker容器上运行。 这是我的test.js文件
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;
// Prevent Karma from running prematurely.
__karma__.loaded = function () {};
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./test/', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();
&#13;
以下是我的业力配置的相关部分:
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
concurrency: Infinity,
customLaunchers: {
'PhantomJS_custom': {
base: 'PhantomJS',
options: {
windowName: 'my-window',
settings: {
webSecurityEnabled: false
},
},
flags: ['--load-images=true'],
debug: true
},
ChromeHeadless: {
base: 'Chrome',
flags: [
'--no-sandbox',
'--headless',
'--disable-gpu',
// Without a remote debugging port, Google Chrome exits immediately.
'--remote-debugging-port=9222',
],
}
},
phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
},
browsers: [ 'ChromeHeadless'],
singleRun: true,
browserNoActivityTimeout: 500000
});
&#13;
我甚至尝试使用木偶戏,但它仍然无法在我的CI上工作。