Search code examples
angulartypescriptangular-cliangular-cli-v6zonejs

Test generated by angular CLI fails if in a specific folder


I've generated a component in my components folder using the angular CLI (v6.1.4) by running ng g c components/foo and have then run ng test. The test for the new component fails with the following errors:

Zone is needed for the async() test helper but could not be found. Please make sure that your environment includes zone.js/dist/zone.js thrown

Error: Illegal state: Could not load the summary for directive FooComponent.

Oddly, if I run ng g c foo (i.e. so it's not generated in the components folder), the test then passes. My tests are all working for other folders, but this is the first test in my components folder (and is also the first test for a component). None of the other tests are asynchronous.

I'm fairly new to ng test so I might be missing something very obvious. I wasn't expecting to need to set up anything with zone.js for the default test to run. Where should I configure this? Or is there something else I'm missing here, perhaps something specific to my components folder?


Solution

  • Adding the following to the top of foo.component.spec.ts fixed the issue, but I still don't understand why the tests worked if they weren't in the components folder. If someone has an explanation, or a solution that doesn't involve repeating this in every component's test file, I'd love to hear it!

    import 'zone.js';
    import 'zone.js/dist/async-test.js';
    import 'zone.js/dist/proxy.js';
    import 'zone.js/dist/sync-test';
    import 'zone.js/dist/jasmine-patch';