gitlab管道上的角度测试失败

时间:2019-12-26 13:19:02

标签: angular typescript karma-jasmine gitlab-ci

由于前端单元测试在管道中没有成功,所以我的管道失败了,而在本地使用纱线测试命令我没有任何失败。

这是我的测试文件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { MyComponent } from './my.component';
import { SharedModule } from 'src/app/shared/shared.module';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

describe('BsGapRepoComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<BsGapRepoComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [MyComponent],
      imports: [SharedModule, NoopAnimationsModule]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });


  it('should create', () => {
    expect(component).toBeTruthy();
  });

});

我的组件文件:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-compo',
  templateUrl: './my.component.html',
  styleUrls: ['./my.component.css']
})
export class myComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

如您所见,它是一个新组件。

这是gitlab的日志错误:

PhantomJS 2.1.1 (Linux 0.0.0) MyComponent should create FAILED
TypeError: undefined is not an object (evaluating 'this.get_container_ul()[0].className') thrown
TypeError: undefined is not an object (evaluating 'this.get_container_ul()[0].className') thrown
PhantomJS 2.1.1 (Linux 0.0.0): Executed 112 of 185 (1 FAILED) (0 secs / 1 min 25.792 secs)
PhantomJS 2.1.1 (Linux 0.0.0) MyComponent should create FAILED
TypeError: undefined is not an object (evaluating 'this.get_container_ul()[0].className') thrown
TypeError: undefined is not an object (evaluating 'this.get_container_ul()[0].className') thrown
PhantomJS 2.1.1 (Linux 0.0.0) MyComponent should create FAILED
TypeError: undefined is not an object (evaluating 'this.get_container_ul()[0].className') thrown
TypeError: undefined is not an object (evaluating 'this.get_container_ul()[0].className') thrown

我正在使用Angular v8

谢谢

2 个答案:

答案 0 :(得分:0)

您无法在服务器上运行浏览器, Headless Chrome 是在不实际启动浏览器的环境中运行自动化测试的有用工具,因此请尝试添加在您的 karma.conf.js

中进行配置
browsers: ['Chrome_no_sandbox'],
customLaunchers: {
 Chrome_no_sandbox: {
 base: 'Chrome',
 flags: ['--no-sandbox', '--headless', '--disable-gpu', '--remote-debugging-port=9222', '--single-run']
 }
}

另一种可能性是使用Run PhantomJS

  • 安装 karma-phantomjs-launcher
  • 将PhantomJS添加到插件

    plugins: [
     require('karma-jasmine'),
     require('karma-chrome-launcher'),
     require('karma-phantomjs-launcher'), // here
     require('karma-jasmine-html-reporter'),
     require('karma-coverage-istanbul-reporter'),
     require('@angular/cli/plugins/karma')
    ],
    
  • 并使幻影成为浏览器

    browsers: ['PhantomJS'],
    singleRun: true
    

答案 1 :(得分:0)

尝试在项目目录中运行npm run build-prod,有时如果您修复了此命令引发的任何错误是否可以解决您的GitLab管道问题,因为ng serve命令仅尝试运行angular应用程序,如果有的话执行应用程序时会出现错误,但是ng build会检查所有Tslint错误,逻辑错误并将该错误抛出给您。