angularjs - How does one test Angular 2 with Mocha? -
i've been banging head against few days, , can't anywhere.. i'm trying use mocha test angular 2 app (systemjs-based if matters), , can't figure out how instances of controllers.
i'm trying simplest case can come with;
import {bootstrap} 'angular2/platform/browser'; import {app} '../app/app'; import {type} 'angular2/core'; describe('login', () => { let app:app; beforeeach((done) => { console.log(bootstrap); bootstrap(<type>app) .then(result => result.instance) .then(instance => { app = instance; done(); }); }); it('test app exist', (done) => { console.log(app); done(); }); });
as best can tell, console.log(bootstrap)
fails somehow, gulp-mocha task dies (silently). commenting out bootstrap references dummy test;
import {bootstrap} 'angular2/platform/browser'; import {app} '../app/app'; import {type} 'angular2/core'; describe('login', () => { let app:app; beforeeach((done) => { done(); }); it('test app exist', (done) => { console.log(app); done(); }); });
logs undefined
expect. has managed work? goal here unit-testing controllers, i'm trying hard avoid phantomjs/webdriver/etc.
i think mocha can't used directly because runs on node (maybe possible angular2 universal when render html strings server side). being said, can use mochify mocha browserify used in background. working on example project setup.
test can this:
// import needed run angular (we're running in phantomjs defualt other browsers possible too) import "es6-shim"; import "es6-promise"; import "zone.js"; import "rxjs"; import "reflect-metadata"; import "../../typings/browser.d.ts"; import {injector, enableprodmode} "angular2/core"; import {http_providers} "angular2/http"; // import stuff need instantiate component import githubcomponent "./gihub-component"; import githubservice "./github-service"; import config "../config"; import * sinon "sinon"; enableprodmode(); describe("github-component", () => { let injector: injector; let component: any; let service: any; beforeeach(() => { // instantiate angular 2 di context injector = injector.resolveandcreate([ http_providers, githubcomponent, githubservice, config ]); component = injector.get(githubcomponent); service = injector.get(githubservice); sinon.spy(service, "getrepos"); }); aftereach(() => { service.getrepos.restore(); }); it("searches repository", () => { component.query.updatevalue("test"); return settimeout(() => { sinon.assert.calledonce(service.getrepos); sinon.assert.calledwith(service.getrepos, "test"); }, 300); }); });
Comments
Post a Comment