2

My goal is to create a unit test using the Testbed provided by Angular Core. I happen to be using Jest Test Runner (syntax similar to Jasmine) for my unit testing.

Expected:
I expected that after the CLI generates a component and its associated unit test that the unit test would pass (see Component and Unit Test for the minimal reproducible example below).

Actual:
In actuality, I received two errors (see Errors below). These errors show me that the component is not being instantiated in time for the test.

Things I have tried:
My previous question shows a history of things I have tried thus far. That start with the first thing I tried which was adding the initiation of the test environment to the setup File. This just resulted in Can't resolve all parameters for ApplicationModule: (?). You can go to that ticket if you are interested in the riveting discussion coming from that. On to this attempt... 1. The second major attempt after the above leads to the below steps. I tried wrapping the beforeEach Testbed call in an async function.
2. This simply results in the error ProxyZoneSpec is needed for the async() test helper but could not be found. After that, you add import 'zone.js/dist/proxy.js'; to your setup file.
3. This results in the error Expected to be running in 'ProxyZone', but it was not found. After that, you find a few SO posts one of which suggests you use fakeAsync instead of async so you try that. The error doesn't change this time. The test is still failing.


This post could have also been named:

Using Testbed to do Angular Unit testing - BeforeEach wrapped in Async causes the error: Expected to be running in 'ProxyZone', but it was not found.
But I believe starting on solid ground with the MRE where the CLI leaves you is the best since that SHOULD work, but doesn't. Anyway, I am wondering where I went astray here. If you have anything I could try I am all ears. Thank you for your help. Please let me know if I am missing anything so I can amend this post.



Component

@Component({
  selector: 'app-geode-lib',
  template: `  `,
})
export class GeodeLibComponent implements OnInit {
  constructor() { }
  ngOnInit() { }
}

Unit test

describe('GeodeLibComponent', () => {
  let component: GeodeLibComponent;
  let fixture: ComponentFixture<GeodeLibComponent>;
  beforeEach(() => {
    TestBed.configureTestingModule({
      schemas: [NO_ERRORS_SCHEMA],
      declarations: [GeodeLibComponent]
    });
    fixture = TestBed.createComponent(GeodeLibComponent);
    component = fixture.componentInstance;
  });
  it('can load instance', () => {
    expect(component).toBeTruthy();
  });
});

ERROR1

 FAIL  projects/geode-lib/src/lib/geode-lib.component.spec.ts
  ● GeodeLibComponent › can load instance

    TypeError: Cannot read property 'getComponentFromError' of null

      at TestBedViewEngine._initIfNeeded (../../packages/core/testing/src/test_bed.ts:378:46)
      at TestBedViewEngine.createComponent (../../packages/core/testing/src/test_bed.ts:570:10)
      at Function.TestBedViewEngine.createComponent (../../packages/core/testing/src/test_bed.ts:232:36)
      at Object.beforeEach (projects/geode-lib/src/lib/geode-lib.component.spec.ts:12:23)

  ● GeodeLibComponent › can load instance

    expect(received).toBeTruthy()

    Received: undefined

      14 |   });
      15 |   it('can load instance', () => {
    > 16 |     expect(component).toBeTruthy();
         |                       ^
      17 |   });
      18 | });
      19 | 

      at Object.it (projects/geode-lib/src/lib/geode-lib.component.spec.ts:16:23)

ERROR2

// getTestBed().initTestEnvironment(
//     BrowserDynamicTestingModule,
//     platformBrowserDynamicTesting()
// );
imnickvaughn
  • 2,774
  • 9
  • 25
  • 42
  • there's nothing wrong with the code you show. Might you share how you configured jest? also version of angular you use. – satanTime May 14 '20 at 10:13

0 Answers0