I'm working build an application on Angular2 beta version and when I'm running my test case, I randomly get the error:
The selector "#root0" did not match any elements' error.
The test code:
@Component({
template: '',
directives: [GroupBox, GroupBoxHeader]
})
class TestComponent {
}
describe('group-box control', () => {
it('should show the title', injectAsync([TestComponentBuilder], (tcb) => {
return tcb.overrideTemplate(TestComponent, '<group-box><group-box-header>Title Content</group-box-header></group-box>')
.createAsync(TestComponent).then( (fixture) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled).toContainText('Title Content');
});
}));
it('should show the body', injectAsync([TestComponentBuilder], (tcb) => {
return tcb.overrideTemplate(TestComponent, '<group-box><group-box-header>Title Content</group-box-header>Body Content</group-box>')
.createAsync(TestComponent).then( (fixture) => {
fixture.detectChanges();
let compiled = fixture.debugElement.nativeElement;
expect(compiled).toContainText('Body Content');
});
}));
});
Im using Karma with Jasmine, following the guidelines of https://github.com/juliemr/ng2-test-seed
The following repository reproduces the error: https://github.com/cangosta/ng2_testing_controls
StackTrace:
14 01 2016 11:40:32.073:INFO [karma]: Karma v0.13.19 server started at http://localhost:9876/ 14 01 2016 11:40:32.078:INFO [launcher]: Starting browser Chrome 14 01 2016 11:40:32.669:INFO [Chrome 47.0.2526 (Windows 8.1 0.0.0)]: Connected on socket /#9hKZqsNbKFPfYWzIAAAA with id 50428936 Chrome 47.0.2526 (Windows 8.1 0.0.0) group-box control should show the body FAILED Failed: The selector "#root0" did not match any elements Error: The selector "#root0" did not match any elements at new BaseException (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2.dev.js:8080:21) at DomRenderer_.createRootHostView (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2.dev.js:15248:15) at AppViewManager_.createRootHostView (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2.dev.js:11265:52) at C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2.dev.js:14531:46 at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) at zoneBoundFn (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) at lib$es6$promise$$internal$$tryCatch (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1511:16) at lib$es6$promise$$internal$$invokeCallback (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1523:17) at lib$es6$promise$$internal$$publish (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1494:11) at C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:243:5 at microtask (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2557:15) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) at zoneBoundFn (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) at lib$es6$promise$asap$$flush (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1305:9) Failed: Cannot read property 'hostView' of undefined TypeError: Cannot read property 'hostView' of undefined at new ComponentFixture_ (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2047:97) at C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2145:16 at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) at zoneBoundFn (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) at lib$es6$promise$$internal$$tryCatch (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1511:16) at lib$es6$promise$$internal$$invokeCallback (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1523:17) at lib$es6$promise$$internal$$publish (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1494:11) at C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:243:5 at microtask (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2557:15) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) at zoneBoundFn (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) at lib$es6$promise$asap$$flush (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1305:9) Failed: Cannot read property 'detectChanges' of undefined TypeError: Cannot read property 'detectChanges' of undefined at C:/Projectos/Fontes/Stratus-HTML/build/test/infrastructure/controls/group-box.test.js:52:20 at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) at zoneBoundFn (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) at lib$es6$promise$$internal$$tryCatch (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1511:16) at lib$es6$promise$$internal$$invokeCallback (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1523:17) at lib$es6$promise$$internal$$publish (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1494:11) at C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:243:5 at microtask (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2557:15) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:138:17) at Zone.run (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/testing.dev.js:2544:30) at zoneBoundFn (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:111:19) at lib$es6$promise$asap$$flush (C:/Projectos/Fontes/Stratus-HTML/build/assets/js/angular2-polyfills.js:1305:9) Chrome 47.0.2526 (Windows 8.1 0.0.0): Executed 59 of 60 (1 FAILED) (skipped 1) (0.519 secs / 0.51 secs) [11:40:33] 'test' errored after 2.01 s [11:40:33] Error: 1 at formatError (C:\Users\LSANTOS\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10) at Gulp. (C:\Users\LSANTOS\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15) at emitOne (events.js:77:13) at Gulp.emit (events.js:169:7) at Gulp.Orchestrator._emitTaskDone (C:\Projectos\Fontes\Stratus-HTML\node_modules\orchestrator\index.js:264:8) at C:\Projectos\Fontes\Stratus-HTML\node_modules\orchestrator\index.js:275:23 at finish (C:\Projectos\Fontes\Stratus-HTML\node_modules\orchestrator\lib\runTask.js:21:8) at cb (C:\Projectos\Fontes\Stratus-HTML\node_modules\orchestrator\lib\runTask.js:29:3) at removeAllListeners (C:\Projectos\Fontes\Stratus-HTML\node_modules\karma\lib\server.js:333:7) at Server. (C:\Projectos\Fontes\Stratus-HTML\node_modules\karma\lib\server.js:344:9) at Server.g (events.js:260:16) at emitNone (events.js:72:20) at Server.emit (events.js:166:7) at emitCloseNT (net.js:1518:8) at doNTCallback1 (node.js:418:9) at process._tickCallback (node.js:340:17)
This is fixed in Angular 2.0.0-beta.3
See https://github.com/angular/angular/issues/6483#issuecomment-179557485 for more details
Basically, what I had to do:
- Manually add typings for jasmine with tsd install jasmine -so and add
///<reference...
in the test files;- Add this in my imports:
import {setBaseTestProviders} from 'angular2/testing';
import {
TEST_BROWSER_PLATFORM_PROVIDERS,
TEST_BROWSER_APPLICATION_PROVIDERS
} from 'angular2/platform/testing/browser';
Add this before my Component tests:
setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With