Angular 6 Unit Tests: An error was thrown in afterAll\nReferenceError: Can't find variable: $ thrown

前端 未结 10 1542
栀梦
栀梦 2020-12-23 09:25

When running my unit tests, from time to time, even if they pass, at the end of all the tests running, I will get the following error.

On my Jenkins CI build running

10条回答
  •  佛祖请我去吃肉
    2020-12-23 10:22

    My specific issue with this error was caused by not mocking sub-components of the component I was testing. In this case I had a homepage component with two sub components, which required declarations for the sub components, which I failed to mock.

    As a result the sub components had real dependencies which would intermittently cause the tests to fail in this non-obvious manner (it looks like different tests are randomly failing, but it is not the case).

    Mocking as follows works pretty well in this case:

    @Component({
        selector: 'app-exercise',
        template: '

    Mock Exercise Component

    ' }) class MockExerciseComponent { } @Component({ selector: 'app-user', template: '

    Mock User Component

    ' }) class MockUserComponent { } describe('HomepageComponent', () => { let component: HomepageComponent; let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ // note you need to mock sub components! declarations: [HomepageComponent, MockExerciseComponent, MockUserComponent],

提交回复
热议问题